feat: Aircraft trails

This commit is contained in:
2026-04-17 17:24:56 +01:00
parent 7ad3d437f8
commit 593a056696
8 changed files with 69 additions and 7 deletions

View File

@@ -4,14 +4,14 @@ void dxd::sim::AFSpawner::tick(float timestep, World *world)
{ {
(void)timestep; (void)timestep;
if (_next_spawn-- != 0) if (--_next_spawn != 0)
return; return;
_next_spawn = 60 + SDL_rand(120); _next_spawn = _MIN_SPAWN_DELAY + SDL_rand(_MAX_SPAWN_DELAY);
//Pos should spawn in a grid, check if free, and offset random from grid coord to look natural //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}; vec2 pos = {(SDL_randf()-.5f)*2*60.0f, (SDL_randf()-.5f)*2*60.0f};
Airfield *af = new Airfield(pos, 180 + SDL_rand(600)); Airfield *af = new Airfield(pos, _MIN_AF_TTL + SDL_rand(_MAX_AF_TTL));
world->add_obj(af); world->add_obj(af);
_airfields.push_back(af); _airfields.push_back(af);
} }

View File

@@ -15,11 +15,16 @@ private:
std::vector<Airfield*> _airfields; std::vector<Airfield*> _airfields;
int _next_spawn; int _next_spawn;
const int _MIN_SPAWN_DELAY = 450;
const int _MAX_SPAWN_DELAY = 3000;
const int _MIN_AF_TTL = 1200;
const int _MAX_AF_TTL = 6000;
public: public:
AFSpawner() AFSpawner()
{ {
_airfields = std::vector<Airfield*>(); _airfields = std::vector<Airfield*>();
_next_spawn = 0; _next_spawn = 1;
} }
void tick(float timestep, World *world) override; void tick(float timestep, World *world) override;

View File

@@ -1,10 +1,17 @@
#include "aircraft.hpp" #include "aircraft.hpp"
#include "aircraft_trail.hpp"
void dxd::sim::Aircraft::tick(float timestep, World *world) void dxd::sim::Aircraft::tick(float timestep, World *world)
{ {
(void)world; (void)world;
_position = _position + polar_to_vec2(_direction, _speed) * timestep; _position = _position + polar_to_vec2(_direction, _speed) * timestep;
if (--_trail_cooldown == 0)
{
world->add_obj(new AircraftTrail(_position, _TRAIL_DURATION));
_trail_cooldown = _TRAIL_DELAY;
}
} }
void dxd::sim::Aircraft::draw(Renderer *rend) void dxd::sim::Aircraft::draw(Renderer *rend)

View File

@@ -12,10 +12,17 @@ private:
vec2 _position; vec2 _position;
float _direction; float _direction;
float _speed; float _speed;
int _trail_cooldown;
const int _TRAIL_DELAY = 60;
const int _TRAIL_DURATION = 420;
public: 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)
{
_trail_cooldown = _TRAIL_DELAY;
}
void tick(float timestep, World *world) override; void tick(float timestep, World *world) override;
void draw(Renderer *rend) override; void draw(Renderer *rend) override;

View File

@@ -0,0 +1,18 @@
#include "aircraft_trail.hpp"
void dxd::sim::AircraftTrail::tick(float timestep, World *world)
{
(void)timestep;
if (--_ttl == 0)
{
world->remove_obj(this);
return;
}
}
void dxd::sim::AircraftTrail::draw(Renderer *rend)
{
rend->color(128, 128, 128, 255);
rend->box(_position, .5);
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "../math.hpp"
#include "world_object.hpp"
namespace dxd::sim
{
class AircraftTrail : public WorldObject
{
private:
vec2 _position;
int _ttl;
public:
AircraftTrail(vec2 position, int ttl) : _position(position), _ttl(ttl) {}
void tick(float timestep, World *world) override;
void draw(Renderer *rend) override;
};
} // namespace dxd::sim

View File

@@ -13,9 +13,9 @@ void dxd::sim::Airfield::tick(float timestep, World *world)
if (--_next_spawn == 0) if (--_next_spawn == 0)
{ {
Aircraft *af = new Aircraft(_position, SDL_randf() * M_PIf, 5.0f); Aircraft *af = new Aircraft(_position, SDL_randf() * 2 * M_PIf, 5.0f);
world->add_obj(af); world->add_obj(af);
_next_spawn = 60 + SDL_rand(240); _next_spawn = _MIN_TAKEOFF_DELAY + SDL_rand(_MAX_TAKEOFF_DELAY - _MIN_TAKEOFF_DELAY);
} }
} }

View File

@@ -15,6 +15,9 @@ private:
int _ttl; int _ttl;
int _next_spawn; int _next_spawn;
const int _MIN_TAKEOFF_DELAY = 240;
const int _MAX_TAKEOFF_DELAY = 600;
public: public:
Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl) Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl)
{ {