From: Daniel Karbach Date: Sat, 5 Sep 2015 12:56:49 +0000 (+0200) Subject: adjust player index if entity is removed X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=416f96590d7e09433549fb4cc35c21b1b437ac4c;p=blank.git adjust player index if entity is removed --- diff --git a/src/world/World.cpp b/src/world/World.cpp index 6ad30ff..7b90954 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -227,7 +227,7 @@ void World::Update(int dt) { } for (auto iter = entities.begin(), end = entities.end(); iter != end;) { if (iter->CanRemove()) { - iter = entities.erase(iter); + iter = RemoveEntity(iter); } else { ++iter; } @@ -269,6 +269,15 @@ void World::Resolve(Entity &e, std::vector &col) { e.Move(final_disp); } +World::EntityHandle World::RemoveEntity(EntityHandle &eh) { + // check for player + auto player = std::find(players.begin(), players.end(), &*eh); + if (player != players.end()) { + players.erase(player); + } + return entities.erase(eh); +} + void World::Render(Viewport &viewport) { DirectionalLighting &entity_prog = viewport.EntityProgram(); diff --git a/src/world/World.hpp b/src/world/World.hpp index 9ae0faa..9739e21 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -87,6 +87,10 @@ public: void Render(Viewport &); +private: + using EntityHandle = std::list::iterator; + EntityHandle RemoveEntity(EntityHandle &); + private: Config config;