From: Daniel Karbach Date: Thu, 27 Aug 2015 14:13:41 +0000 (+0200) Subject: reference count entities for safer removal X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=5304d1e3c1a1e90d474307f4f7eea812a61e483c;p=blank.git reference count entities for safer removal --- diff --git a/src/ai/Spawner.cpp b/src/ai/Spawner.cpp index 6d5549f..5b54c3e 100644 --- a/src/ai/Spawner.cpp +++ b/src/ai/Spawner.cpp @@ -68,9 +68,14 @@ void Spawner::CheckDespawn() noexcept { const Entity &reference = world.Player(); for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) { Entity &e = (*iter)->Controlled(); + if (e.Dead()) { + delete *iter; + iter = controllers.erase(iter); + continue; + } glm::vec3 diff(reference.AbsoluteDifference(e)); if (dot(diff, diff) > despawn_range) { - e.Remove(); + e.Kill(); delete *iter; iter = controllers.erase(iter); } else { diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index fd03ebb..e0fb955 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -20,11 +20,11 @@ Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept , flee_speed(-0.005f) , stop_dist(10) , flee_dist(5) { - + tgt.Ref(); } Chaser::~Chaser() { - + tgt.UnRef(); } void Chaser::Update(int dt) { @@ -53,11 +53,11 @@ void Chaser::Update(int dt) { Controller::Controller(Entity &e) noexcept : entity(e) { - + entity.Ref(); } Controller::~Controller() { - + entity.UnRef(); } diff --git a/src/app/FPSController.cpp b/src/app/FPSController.cpp index 7f8c2c1..87591aa 100644 --- a/src/app/FPSController.cpp +++ b/src/app/FPSController.cpp @@ -10,7 +10,11 @@ FPSController::FPSController(Entity &entity) noexcept : entity(entity) , pitch(0) , yaw(0) { + entity.Ref(); +} +FPSController::~FPSController() { + entity.UnRef(); } diff --git a/src/app/FPSController.hpp b/src/app/FPSController.hpp index a7b11a4..ee10691 100644 --- a/src/app/FPSController.hpp +++ b/src/app/FPSController.hpp @@ -17,6 +17,7 @@ class FPSController { public: explicit FPSController(Entity &) noexcept; + ~FPSController(); Entity &Controlled() noexcept { return entity; } const Entity &Controlled() const noexcept { return entity; } diff --git a/src/world/Entity.cpp b/src/world/Entity.cpp index fb62f27..f116153 100644 --- a/src/world/Entity.cpp +++ b/src/world/Entity.cpp @@ -21,8 +21,9 @@ Entity::Entity() noexcept , velocity(0, 0, 0) , chunk(0, 0, 0) , angular_velocity(0.0f) +, ref_count(0) , world_collision(false) -, remove(false) { +, dead(false) { } diff --git a/src/world/Entity.hpp b/src/world/Entity.hpp index ae14297..ef5e4e3 100644 --- a/src/world/Entity.hpp +++ b/src/world/Entity.hpp @@ -61,8 +61,12 @@ public: glm::mat4 Transform(const Chunk::Pos &chunk_offset) const noexcept; Ray Aim(const Chunk::Pos &chunk_offset) const noexcept; - void Remove() noexcept { remove = true; } - bool CanRemove() const noexcept { return remove; } + void Ref() noexcept { ++ref_count; } + void UnRef() noexcept { --ref_count; } + void Kill() noexcept { dead = true; } + bool Referenced() const noexcept { return ref_count > 0; } + bool Dead() const noexcept { return dead; } + bool CanRemove() const noexcept { return dead && ref_count <= 0; } void Update(int dt) noexcept; @@ -82,8 +86,10 @@ private: glm::vec3 angular_velocity; + int ref_count; + bool world_collision; - bool remove; + bool dead; };