From: Daniel Karbach Date: Mon, 4 Dec 2017 10:37:29 +0000 (+0100) Subject: fix double deletes on app exit X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=23f64a38a27866e88e87602cc6f8b11ef7173d6e;p=blobs.git fix double deletes on app exit --- diff --git a/src/creature/Creature.hpp b/src/creature/Creature.hpp index 498ebea..1c2556a 100644 --- a/src/creature/Creature.hpp +++ b/src/creature/Creature.hpp @@ -139,6 +139,7 @@ public: void Hurt(double d) noexcept; void Die() noexcept; + bool Dead() const noexcept; void OnDeath(Callback cb) noexcept { on_death = cb; } void Remove() noexcept; bool Removable() const noexcept { return removable; } diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index de82f86..3c03e57 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -92,7 +92,7 @@ Creature::Creature(world::Simulation &sim) , mass(1.0) , size(1.0) , birth(sim.Time()) -, death(0.0) +, death(-1.0) , on_death() , removable(false) , parents() @@ -230,6 +230,8 @@ void Creature::Hurt(double amount) noexcept { } void Creature::Die() noexcept { + if (Dead()) return; + sim.SetDead(this); death = sim.Time(); steering.Halt(); @@ -239,6 +241,10 @@ void Creature::Die() noexcept { Remove(); } +bool Creature::Dead() const noexcept { + return death > birth; +} + void Creature::Remove() noexcept { removable = true; } diff --git a/src/world/Body.hpp b/src/world/Body.hpp index 3cbb788..5ac4fa1 100644 --- a/src/world/Body.hpp +++ b/src/world/Body.hpp @@ -88,7 +88,6 @@ public: void Cache() noexcept; void CheckCollision() noexcept; - // body takes over ownership of given pointer void AddCreature(creature::Creature *); void RemoveCreature(creature::Creature *); std::vector &Creatures() noexcept { return creatures; }