]> git.localhorst.tv Git - blank.git/commitdiff
adjust player index if entity is removed
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 5 Sep 2015 12:56:49 +0000 (14:56 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 5 Sep 2015 12:56:49 +0000 (14:56 +0200)
src/world/World.cpp
src/world/World.hpp

index 6ad30ff0761b445a19dc6757203019b6b727d662..7b9095401dcecc62444a3e7cf0de53761519c940 100644 (file)
@@ -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<WorldCollision> &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();
index 9ae0faa57217e629816a5e7f0744e4b2de88b0df..9739e21f18415eff4df70010fe9b588d82ba332b 100644 (file)
@@ -87,6 +87,10 @@ public:
 
        void Render(Viewport &);
 
+private:
+       using EntityHandle = std::list<Entity>::iterator;
+       EntityHandle RemoveEntity(EntityHandle &);
+
 private:
        Config config;