Skip to content

Commit

Permalink
Use lambda for removing VisualFX
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Gawin authored and danhedron committed Sep 19, 2018
1 parent 5951b97 commit 3821244
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions rwengine/src/engine/GameWorld.cpp
Expand Up @@ -48,6 +48,18 @@
constexpr float kMaxTrafficSpawnRadius = 100.f;
constexpr float kMaxTrafficCleanupRadius = kMaxTrafficSpawnRadius * 1.25f;

namespace {
template <typename T>
bool shouldEffectBeRemoved(const T& effect, float gameTime) {
if (effect->getType() != Particle) {
return false;
}
auto particle = static_cast<ParticleFX*>(effect.get());
return particle->lifetime >= 0.f &&
gameTime >= particle->starttime + particle->lifetime;
}
} // namespace

class WorldCollisionDispatcher : public btCollisionDispatcher {
public:
WorldCollisionDispatcher(btCollisionConfiguration* collisionConfiguration)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -863,17 +872,12 @@ bool GameWorld::isPaused() const {
}

void GameWorld::updateEffects() {
for (int i = 0; i < static_cast<int>(effects.size()); ++i) {
auto& effect = effects[i];
if (effect->getType() == Particle) {
auto particle = static_cast<ParticleFX*>(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) {
Expand Down

0 comments on commit 3821244

Please sign in to comment.