From 3821244c95b8e6dc625cf791c3f1bcc0c7d6664c Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Sun, 2 Sep 2018 15:45:25 +0200 Subject: [PATCH] Use lambda for removing VisualFX --- rwengine/src/engine/GameWorld.cpp | 40 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/rwengine/src/engine/GameWorld.cpp b/rwengine/src/engine/GameWorld.cpp index c59746ca..6e734af2 100644 --- a/rwengine/src/engine/GameWorld.cpp +++ b/rwengine/src/engine/GameWorld.cpp @@ -48,6 +48,18 @@ constexpr float kMaxTrafficSpawnRadius = 100.f; constexpr float kMaxTrafficCleanupRadius = kMaxTrafficSpawnRadius * 1.25f; +namespace { +template +bool shouldEffectBeRemoved(const T& effect, float gameTime) { + if (effect->getType() != Particle) { + return false; + } + auto particle = static_cast(effect.get()); + return particle->lifetime >= 0.f && + gameTime >= particle->starttime + particle->lifetime; +} +} // namespace + class WorldCollisionDispatcher : public btCollisionDispatcher { public: WorldCollisionDispatcher(btCollisionConfiguration* collisionConfiguration) @@ -526,13 +538,10 @@ TrailFX& GameWorld::createTrailEffect() { } void GameWorld::destroyEffect(VisualFX& effect) { - for (auto it = effects.begin(); it != effects.end();) { - if (it->get() == &effect) { - it = effects.erase(it); - } else { - it++; - } - } + effects.erase( + std::remove_if(effects.begin(), effects.end(), + [&effect](auto& ef) { return ef.get() == &effect; }), + effects.end()); } void GameWorld::doWeaponScan(const WeaponScan& scan) { @@ -863,17 +872,12 @@ bool GameWorld::isPaused() const { } void GameWorld::updateEffects() { - for (int i = 0; i < static_cast(effects.size()); ++i) { - auto& effect = effects[i]; - if (effect->getType() == Particle) { - auto particle = static_cast(effect.get()); - if (particle->lifetime < 0.f) continue; - if (getGameTime() >= particle->starttime + particle->lifetime) { - destroyEffect(*particle); - --i; - } - } - } + effects.erase(std::remove_if(effects.begin(), effects.end(), + [gameTime = getGameTime()](auto& effect) { + return shouldEffectBeRemoved(effect, + gameTime); + }), + effects.end()); } VehicleObject* GameWorld::tryToSpawnVehicle(VehicleGenerator& gen) {