diff --git a/src/main.cpp b/src/main.cpp index 1c4dd49..48a2588 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/renderer.hpp b/src/renderer.hpp index 54edf5c..ee1938d 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -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) diff --git a/src/sim/af_spawner.cpp b/src/sim/af_spawner.cpp new file mode 100644 index 0000000..c0501c6 --- /dev/null +++ b/src/sim/af_spawner.cpp @@ -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); +} diff --git a/src/sim/af_spawner.hpp b/src/sim/af_spawner.hpp new file mode 100644 index 0000000..bb1fb4a --- /dev/null +++ b/src/sim/af_spawner.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "../math.hpp" +#include "world_object.hpp" +#include "airfield.hpp" + +namespace dxd::sim +{ + +class AFSpawner : public WorldObject +{ +private: + std::vector _airfields; + int _cur_tick; + int _next_spawn; + +public: + AFSpawner() + { + _airfields = std::vector(); + _cur_tick = _next_spawn = 0; + } + + void tick(float timestep, World *world) override; + void draw(Renderer *rend) override {} +}; + +} // namespace dxd::sim diff --git a/src/sim/aircraft.cpp b/src/sim/aircraft.cpp index 259fab7..a372e26 100644 --- a/src/sim/aircraft.cpp +++ b/src/sim/aircraft.cpp @@ -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; } diff --git a/src/sim/aircraft.hpp b/src/sim/aircraft.hpp index 9effcf2..f0cbdd7 100644 --- a/src/sim/aircraft.hpp +++ b/src/sim/aircraft.hpp @@ -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; }; diff --git a/src/sim/airfield.cpp b/src/sim/airfield.cpp index 6194405..e372053 100644 --- a/src/sim/airfield.cpp +++ b/src/sim/airfield.cpp @@ -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) diff --git a/src/sim/airfield.hpp b/src/sim/airfield.hpp index d0f76e7..e42304b 100644 --- a/src/sim/airfield.hpp +++ b/src/sim/airfield.hpp @@ -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; }; diff --git a/src/sim/world.cpp b/src/sim/world.cpp index 610c616..f2c48c5 100644 --- a/src/sim/world.cpp +++ b/src/sim/world.cpp @@ -4,7 +4,7 @@ void dxd::sim::World::tick(float timestep) { for (auto obj : _objs) { - obj->tick(timestep, _size); + obj->tick(timestep, this); } } diff --git a/src/sim/world.hpp b/src/sim/world.hpp index d16e323..cf88080 100644 --- a/src/sim/world.hpp +++ b/src/sim/world.hpp @@ -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); diff --git a/src/sim/world_object.hpp b/src/sim/world_object.hpp index 1412b1b..c43fe1c 100644 --- a/src/sim/world_object.hpp +++ b/src/sim/world_object.hpp @@ -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; };