feat: Random airfield spawning
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "sim/world.hpp"
|
||||
#include "sim/aircraft.hpp"
|
||||
#include "sim/airfield.hpp"
|
||||
#include "sim/af_spawner.hpp"
|
||||
|
||||
static SDL_Window *window;
|
||||
static SDL_Renderer *sdl_renderer;
|
||||
@@ -46,7 +47,7 @@ int main(int argc, char *argv[])
|
||||
// World init
|
||||
dxd::sim::World world = dxd::sim::World({ .x = 10, .y = 10 });
|
||||
world.add_obj(new dxd::sim::Aircraft({ 0, 0 }, 1, 5));
|
||||
world.add_obj(new dxd::sim::Airfield({ 15, 0 }));
|
||||
world.add_obj(new dxd::sim::AFSpawner());
|
||||
|
||||
SDL_Event event;
|
||||
bool running = true;
|
||||
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height)
|
||||
{
|
||||
_center = { 0, 0 };
|
||||
_scale = 0.02;
|
||||
_scale = 0.01;
|
||||
}
|
||||
|
||||
void move_camera(vec2 delta)
|
||||
|
||||
17
src/sim/af_spawner.cpp
Normal file
17
src/sim/af_spawner.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "af_spawner.hpp"
|
||||
|
||||
void dxd::sim::AFSpawner::tick(float timestep, World *world)
|
||||
{
|
||||
++_cur_tick;
|
||||
|
||||
if (_cur_tick < _next_spawn)
|
||||
return;
|
||||
|
||||
_next_spawn = _cur_tick + 60 + SDL_rand(120);
|
||||
|
||||
//Pos should spawn in a grid, check if free, and offset random from grid coord to look natural
|
||||
vec2 pos = {(SDL_randf()-.5f)*2*60.0f, (SDL_randf()-.5f)*2*60.0f};
|
||||
Airfield *af = new Airfield(pos, 180 + SDL_rand(600));
|
||||
world->add_obj(af);
|
||||
_airfields.push_back(af);
|
||||
}
|
||||
30
src/sim/af_spawner.hpp
Normal file
30
src/sim/af_spawner.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "../math.hpp"
|
||||
#include "world_object.hpp"
|
||||
#include "airfield.hpp"
|
||||
|
||||
namespace dxd::sim
|
||||
{
|
||||
|
||||
class AFSpawner : public WorldObject
|
||||
{
|
||||
private:
|
||||
std::vector<Airfield*> _airfields;
|
||||
int _cur_tick;
|
||||
int _next_spawn;
|
||||
|
||||
public:
|
||||
AFSpawner()
|
||||
{
|
||||
_airfields = std::vector<Airfield*>();
|
||||
_cur_tick = _next_spawn = 0;
|
||||
}
|
||||
|
||||
void tick(float timestep, World *world) override;
|
||||
void draw(Renderer *rend) override {}
|
||||
};
|
||||
|
||||
} // namespace dxd::sim
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "aircraft.hpp"
|
||||
|
||||
void dxd::sim::Aircraft::tick(float timestep, vec2 bounds)
|
||||
void dxd::sim::Aircraft::tick(float timestep, World *world)
|
||||
{
|
||||
_position = _position + polar_to_vec2(_direction, _speed) * timestep;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public:
|
||||
Aircraft(vec2 position, float direction, float speed)
|
||||
: _position(position), _direction(direction), _speed(speed) {}
|
||||
|
||||
void tick(float timestep, vec2 bounds) override;
|
||||
void tick(float timestep, World *world) override;
|
||||
void draw(Renderer *rend) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "airfield.hpp"
|
||||
|
||||
void dxd::sim::Airfield::tick(float timestep, vec2 bounds)
|
||||
void dxd::sim::Airfield::tick(float timestep, World *world)
|
||||
{
|
||||
|
||||
if (--_ttl == 0)
|
||||
world->remove_obj(this);
|
||||
}
|
||||
|
||||
void dxd::sim::Airfield::draw(Renderer *rend)
|
||||
|
||||
@@ -12,11 +12,12 @@ class Airfield : public WorldObject
|
||||
{
|
||||
private:
|
||||
vec2 _position;
|
||||
int _ttl;
|
||||
|
||||
public:
|
||||
Airfield(vec2 position) : _position(position) {}
|
||||
Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl) {}
|
||||
|
||||
void tick(float timestep, vec2 bounds) override;
|
||||
void tick(float timestep, World *world) override;
|
||||
void draw(Renderer *rend) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ void dxd::sim::World::tick(float timestep)
|
||||
{
|
||||
for (auto obj : _objs)
|
||||
{
|
||||
obj->tick(timestep, _size);
|
||||
obj->tick(timestep, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
namespace dxd::sim
|
||||
{
|
||||
|
||||
class WorldObject;
|
||||
|
||||
class World
|
||||
{
|
||||
private:
|
||||
@@ -21,6 +23,17 @@ public:
|
||||
}
|
||||
|
||||
void add_obj(WorldObject *obj) { _objs.push_back(obj); }
|
||||
void remove_obj(WorldObject *obj)
|
||||
{
|
||||
for (auto it = _objs.begin(); it != _objs.end(); ++it)
|
||||
{
|
||||
if (*it == obj)
|
||||
{
|
||||
_objs.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tick(float timestep);
|
||||
void draw(Renderer *rend);
|
||||
|
||||
@@ -2,16 +2,19 @@
|
||||
|
||||
#include "../math.hpp"
|
||||
#include "../renderer.hpp"
|
||||
#include "world.hpp"
|
||||
|
||||
namespace dxd::sim
|
||||
{
|
||||
|
||||
class World;
|
||||
|
||||
class WorldObject
|
||||
{
|
||||
public:
|
||||
WorldObject() {}
|
||||
|
||||
virtual void tick(float timestep, vec2 bounds) = 0;
|
||||
virtual void tick(float timestep, World *world) = 0;
|
||||
virtual void draw(Renderer *rend) = 0;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user