feat: Random airfield spawning

This commit is contained in:
2026-04-16 22:50:27 +01:00
parent 965c0f393c
commit 49dd53f8e4
11 changed files with 76 additions and 10 deletions

View File

@@ -9,6 +9,7 @@
#include "sim/world.hpp" #include "sim/world.hpp"
#include "sim/aircraft.hpp" #include "sim/aircraft.hpp"
#include "sim/airfield.hpp" #include "sim/airfield.hpp"
#include "sim/af_spawner.hpp"
static SDL_Window *window; static SDL_Window *window;
static SDL_Renderer *sdl_renderer; static SDL_Renderer *sdl_renderer;
@@ -46,7 +47,7 @@ int main(int argc, char *argv[])
// World init // World init
dxd::sim::World world = dxd::sim::World({ .x = 10, .y = 10 }); 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::Aircraft({ 0, 0 }, 1, 5));
world.add_obj(new dxd::sim::Airfield({ 15, 0 })); world.add_obj(new dxd::sim::AFSpawner());
SDL_Event event; SDL_Event event;
bool running = true; bool running = true;

View File

@@ -47,7 +47,7 @@ public:
Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height) Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height)
{ {
_center = { 0, 0 }; _center = { 0, 0 };
_scale = 0.02; _scale = 0.01;
} }
void move_camera(vec2 delta) void move_camera(vec2 delta)

17
src/sim/af_spawner.cpp Normal file
View 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
View 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

View File

@@ -1,6 +1,6 @@
#include "aircraft.hpp" #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; _position = _position + polar_to_vec2(_direction, _speed) * timestep;
} }

View File

@@ -17,7 +17,7 @@ public:
Aircraft(vec2 position, float direction, float speed) Aircraft(vec2 position, float direction, float speed)
: _position(position), _direction(direction), _speed(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; void draw(Renderer *rend) override;
}; };

View File

@@ -1,8 +1,9 @@
#include "airfield.hpp" #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) void dxd::sim::Airfield::draw(Renderer *rend)

View File

@@ -12,11 +12,12 @@ class Airfield : public WorldObject
{ {
private: private:
vec2 _position; vec2 _position;
int _ttl;
public: 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; void draw(Renderer *rend) override;
}; };

View File

@@ -4,7 +4,7 @@ void dxd::sim::World::tick(float timestep)
{ {
for (auto obj : _objs) for (auto obj : _objs)
{ {
obj->tick(timestep, _size); obj->tick(timestep, this);
} }
} }

View File

@@ -8,6 +8,8 @@
namespace dxd::sim namespace dxd::sim
{ {
class WorldObject;
class World class World
{ {
private: private:
@@ -21,6 +23,17 @@ public:
} }
void add_obj(WorldObject *obj) { _objs.push_back(obj); } 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 tick(float timestep);
void draw(Renderer *rend); void draw(Renderer *rend);

View File

@@ -2,16 +2,19 @@
#include "../math.hpp" #include "../math.hpp"
#include "../renderer.hpp" #include "../renderer.hpp"
#include "world.hpp"
namespace dxd::sim namespace dxd::sim
{ {
class World;
class WorldObject class WorldObject
{ {
public: public:
WorldObject() {} WorldObject() {}
virtual void tick(float timestep, vec2 bounds) = 0; virtual void tick(float timestep, World *world) = 0;
virtual void draw(Renderer *rend) = 0; virtual void draw(Renderer *rend) = 0;
}; };