I segfault, I don't know why, it doesn't even matter how hard you try

This commit is contained in:
2026-04-16 23:08:01 +01:00
parent 49dd53f8e4
commit a1c0b5e97f
6 changed files with 21 additions and 10 deletions

View File

@@ -46,7 +46,6 @@ 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::AFSpawner()); world.add_obj(new dxd::sim::AFSpawner());
SDL_Event event; SDL_Event event;

View File

@@ -94,7 +94,7 @@ public:
void diamond(vec2 center, float size) void diamond(vec2 center, float size)
{ {
size *= sqrtf(2); size *= M_SQRT2f;
SDL_FPoint points[5]; SDL_FPoint points[5];
vec2 p; vec2 p;

View File

@@ -2,12 +2,10 @@
void dxd::sim::AFSpawner::tick(float timestep, World *world) void dxd::sim::AFSpawner::tick(float timestep, World *world)
{ {
++_cur_tick; if (_next_spawn-- == 0)
if (_cur_tick < _next_spawn)
return; return;
_next_spawn = _cur_tick + 60 + SDL_rand(120); _next_spawn = 60 + SDL_rand(120);
//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};

View File

@@ -13,14 +13,13 @@ class AFSpawner : public WorldObject
{ {
private: private:
std::vector<Airfield*> _airfields; std::vector<Airfield*> _airfields;
int _cur_tick;
int _next_spawn; int _next_spawn;
public: public:
AFSpawner() AFSpawner()
{ {
_airfields = std::vector<Airfield*>(); _airfields = std::vector<Airfield*>();
_cur_tick = _next_spawn = 0; _next_spawn = 0;
} }
void tick(float timestep, World *world) override; void tick(float timestep, World *world) override;

View File

@@ -1,9 +1,20 @@
#include "airfield.hpp" #include "airfield.hpp"
#include "aircraft.hpp"
void dxd::sim::Airfield::tick(float timestep, World *world) void dxd::sim::Airfield::tick(float timestep, World *world)
{ {
if (--_ttl == 0) if (--_ttl == 0)
world->remove_obj(this); {
//world->remove_obj(this);
return;
}
if (--_next_spawn == 0)
{
Aircraft *af = new Aircraft(_position, SDL_randf() * M_PIf, 5.0f);
world->add_obj(af);
_next_spawn = 60 + SDL_rand(240);
}
} }
void dxd::sim::Airfield::draw(Renderer *rend) void dxd::sim::Airfield::draw(Renderer *rend)

View File

@@ -13,9 +13,13 @@ class Airfield : public WorldObject
private: private:
vec2 _position; vec2 _position;
int _ttl; int _ttl;
int _next_spawn;
public: public:
Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl) {} Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl)
{
_next_spawn = 30;
}
void tick(float timestep, World *world) override; void tick(float timestep, World *world) override;
void draw(Renderer *rend) override; void draw(Renderer *rend) override;