diff --git a/source/World.cpp b/source/World.cpp index 47c2bb5..851ac86 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -31,20 +31,6 @@ World::insert(std::shared_ptr drawable) { mDrawables[drawable->getCategory()].push_back(drawable); } -/** - * Removes a drawable from the group. - */ -void -World::remove(std::shared_ptr drawable) { - for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { - auto item = std::find(v->second.begin(), v->second.end(), drawable); - if (item != v->second.end()) { - v->second.erase(item); - break; - } - } -} - /** * Inserts a character into the world. A character can only be inserted once. * Also calls insert(character); @@ -59,18 +45,6 @@ World::insertCharacter(std::shared_ptr character) { insert(character); } -/** - * Removes a character from the world. - * Also calls remove(character); - */ -void -World::removeCharacter(std::shared_ptr character) { - auto item = std::find(mCharacters.begin(), mCharacters.end(), character); - if (item != mCharacters.end()) - mCharacters.erase(item); - remove(character); -} - /** * Runs the A* path finding algorithm with areas as nodes and portals as edges. * @@ -235,11 +209,11 @@ World::step(int elapsed) { auto& spriteA = *it; sf::Vector2f speed = spriteA->getSpeed() * (elapsed / 1000.0f); if (spriteA->getDelete()) { - remove(spriteA); + v->second.erase(it); it--; } // Apply movement for movable sprites. - else if ((*it)->getSpeed() != sf::Vector2f()) { + else if (spriteA->getSpeed() != sf::Vector2f()) { bool overlap = false; for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) { for (auto& spriteB : w->second) { @@ -264,15 +238,18 @@ World::step(int elapsed) { /** * Calls Character::onThink for each character. Must be called - * before step (so Characters get removed asap). + * before step so Characters get removed correctly. * * @param elapsed Time since last call. */ void World::think(int elapsed) { for (auto it = mCharacters.begin(); it != mCharacters.end(); ) { - if ((*it)->getDelete()) - removeCharacter(*it); + if ((*it)->getDelete()) { + mCharacters.erase(it); + auto& d = mDrawables[Sprite::CATEGORY_ACTOR]; + d.erase(std::find(d.begin(), d.end(), *it)); + } else { (*it)->onThink(elapsed); it++; diff --git a/source/World.h b/source/World.h index 860979f..a7d3b85 100755 --- a/source/World.h +++ b/source/World.h @@ -23,9 +23,7 @@ class Sprite; class World : public sf::Drawable { public: void insert(std::shared_ptr drawable); - void remove(std::shared_ptr drawable); void insertCharacter(std::shared_ptr character); - void removeCharacter(std::shared_ptr character); void step(int elapsed); void think(int elapsed); void insertArea(const sf::IntRect& rect);