From: Daniel Karbach Date: Tue, 4 Aug 2015 16:29:04 +0000 (+0200) Subject: make entities removable X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=12744a2f68b3c9496076d6d7b6fb3581ff78472a;p=blank.git make entities removable --- diff --git a/src/world/Entity.cpp b/src/world/Entity.cpp index 79a09ea..b06dbc3 100644 --- a/src/world/Entity.cpp +++ b/src/world/Entity.cpp @@ -23,7 +23,8 @@ Entity::Entity() noexcept , chunk(0, 0, 0) , angular_velocity(1.0f, 0.0f, 0.0f, 0.0f) , rotation(1.0f) -, world_collision(false) { +, world_collision(false) +, remove(false) { } diff --git a/src/world/Entity.hpp b/src/world/Entity.hpp index 05d795f..48646e8 100644 --- a/src/world/Entity.hpp +++ b/src/world/Entity.hpp @@ -57,6 +57,9 @@ 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 Update(int dt) noexcept; void Draw() noexcept; @@ -77,6 +80,7 @@ private: glm::mat4 rotation; bool world_collision; + bool remove; }; diff --git a/src/world/World.cpp b/src/world/World.cpp index 97dcc08..dc07818 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -257,6 +257,13 @@ void World::Update(int dt) { Resolve(entity, col); } } + for (auto iter = entities.begin(), end = entities.end(); iter != end;) { + if (iter->CanRemove()) { + iter = entities.erase(iter); + } else { + ++iter; + } + } chunks.Rebase(player->ChunkCoords()); chunks.Update(dt); }