From 593a056696369f0fa7a83acffcf2cb29c5dcc3dc Mon Sep 17 00:00:00 2001 From: Diogo Cruz Diniz Date: Fri, 17 Apr 2026 17:24:56 +0100 Subject: [PATCH] feat: Aircraft trails --- src/sim/af_spawner.cpp | 6 +++--- src/sim/af_spawner.hpp | 7 ++++++- src/sim/aircraft.cpp | 7 +++++++ src/sim/aircraft.hpp | 9 ++++++++- src/sim/aircraft_trail.cpp | 18 ++++++++++++++++++ src/sim/aircraft_trail.hpp | 22 ++++++++++++++++++++++ src/sim/airfield.cpp | 4 ++-- src/sim/airfield.hpp | 3 +++ 8 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 src/sim/aircraft_trail.cpp create mode 100644 src/sim/aircraft_trail.hpp diff --git a/src/sim/af_spawner.cpp b/src/sim/af_spawner.cpp index 6ec9179..0ca6f1d 100644 --- a/src/sim/af_spawner.cpp +++ b/src/sim/af_spawner.cpp @@ -4,14 +4,14 @@ void dxd::sim::AFSpawner::tick(float timestep, World *world) { (void)timestep; - if (_next_spawn-- != 0) + if (--_next_spawn != 0) 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 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); _airfields.push_back(af); } diff --git a/src/sim/af_spawner.hpp b/src/sim/af_spawner.hpp index f49f6aa..22f00a3 100644 --- a/src/sim/af_spawner.hpp +++ b/src/sim/af_spawner.hpp @@ -15,11 +15,16 @@ private: std::vector _airfields; 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: AFSpawner() { _airfields = std::vector(); - _next_spawn = 0; + _next_spawn = 1; } void tick(float timestep, World *world) override; diff --git a/src/sim/aircraft.cpp b/src/sim/aircraft.cpp index 3b383db..5b91b48 100644 --- a/src/sim/aircraft.cpp +++ b/src/sim/aircraft.cpp @@ -1,10 +1,17 @@ #include "aircraft.hpp" +#include "aircraft_trail.hpp" void dxd::sim::Aircraft::tick(float timestep, World *world) { (void)world; _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) diff --git a/src/sim/aircraft.hpp b/src/sim/aircraft.hpp index f0cbdd7..a4fbba1 100644 --- a/src/sim/aircraft.hpp +++ b/src/sim/aircraft.hpp @@ -12,10 +12,17 @@ private: vec2 _position; float _direction; float _speed; + int _trail_cooldown; + + const int _TRAIL_DELAY = 60; + const int _TRAIL_DURATION = 420; public: 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 draw(Renderer *rend) override; diff --git a/src/sim/aircraft_trail.cpp b/src/sim/aircraft_trail.cpp new file mode 100644 index 0000000..54a6a2b --- /dev/null +++ b/src/sim/aircraft_trail.cpp @@ -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); +} diff --git a/src/sim/aircraft_trail.hpp b/src/sim/aircraft_trail.hpp new file mode 100644 index 0000000..2290a1c --- /dev/null +++ b/src/sim/aircraft_trail.hpp @@ -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 diff --git a/src/sim/airfield.cpp b/src/sim/airfield.cpp index 9292c5a..7bb1622 100644 --- a/src/sim/airfield.cpp +++ b/src/sim/airfield.cpp @@ -13,9 +13,9 @@ void dxd::sim::Airfield::tick(float timestep, World *world) 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); - _next_spawn = 60 + SDL_rand(240); + _next_spawn = _MIN_TAKEOFF_DELAY + SDL_rand(_MAX_TAKEOFF_DELAY - _MIN_TAKEOFF_DELAY); } } diff --git a/src/sim/airfield.hpp b/src/sim/airfield.hpp index de2309b..5a827f1 100644 --- a/src/sim/airfield.hpp +++ b/src/sim/airfield.hpp @@ -15,6 +15,9 @@ private: int _ttl; int _next_spawn; + const int _MIN_TAKEOFF_DELAY = 240; + const int _MAX_TAKEOFF_DELAY = 600; + public: Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl) {