From 656d74376b1458cd32c97a217d22b8e0e7e6fea7 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 23 Feb 2025 14:07:20 +0100 Subject: Some code cleanups in EffectManager Use a std::map to ease the lookup of triggered effects. --- src/effectmanager.cpp | 75 ++++++++++++++++++++++++++------------------------- src/effectmanager.h | 11 ++++---- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/effectmanager.cpp b/src/effectmanager.cpp index 903f08b2..32280555 100644 --- a/src/effectmanager.cpp +++ b/src/effectmanager.cpp @@ -49,56 +49,57 @@ EffectManager::EffectManager() for (auto node : root.children()) { - if (node.name() == "effect") + int effectId; + + if (node.name() == "effect" && node.attribute("id", effectId)) { - EffectDescription &ed = mEffects.emplace_back(); - ed.id = node.getProperty("id", -1); - ed.GFX = node.getProperty("particle", ""); - ed.SFX = node.getProperty("audio", ""); + EffectDescription &ed = mEffects[effectId]; + node.attribute("particle", ed.particle); + node.attribute("audio", ed.sfx); } } } -EffectManager::~EffectManager() -{ -} +EffectManager::~EffectManager() = default; -bool EffectManager::trigger(int id, Being* being, int rotation) +bool EffectManager::trigger(int id, Being *being, int rotation) { - bool rValue = false; - for (auto &effect : mEffects) + auto it = mEffects.find(id); + if (it == mEffects.end()) { - if (effect.id == id) - { - rValue = true; - if (!effect.GFX.empty()) - { - Particle *selfFX; - selfFX = particleEngine->addEffect(effect.GFX, 0, 0, rotation); - being->controlParticle(selfFX); - } - if (!effect.SFX.empty()) - sound.playSfx(effect.SFX); - break; - } + logger->log("EffectManager::trigger: effect %d not found", id); + return false; + } + + EffectDescription &effect = it->second; + + if (!effect.particle.empty()) + { + if (Particle *selfFX = particleEngine->addEffect(effect.particle, 0, 0, rotation)) + being->controlParticle(selfFX); } - return rValue; + + if (!effect.sfx.empty()) + sound.playSfx(effect.sfx); + + return true; } bool EffectManager::trigger(int id, int x, int y, int rotation) { - bool rValue = false; - for (auto &effect : mEffects) + auto it = mEffects.find(id); + if (it == mEffects.end()) { - if (effect.id == id) - { - rValue = true; - if (!effect.GFX.empty()) - particleEngine->addEffect(effect.GFX, x, y, rotation); - if (!effect.SFX.empty()) - sound.playSfx(effect.SFX); - break; - } + logger->log("EffectManager::trigger: effect %d not found", id); + return false; } - return rValue; + + EffectDescription &effect = it->second; + + if (!effect.particle.empty()) + particleEngine->addEffect(effect.particle, x, y, rotation); + if (!effect.sfx.empty()) + sound.playSfx(effect.sfx); + + return true; } diff --git a/src/effectmanager.h b/src/effectmanager.h index 8482e15b..fdc92418 100644 --- a/src/effectmanager.h +++ b/src/effectmanager.h @@ -22,7 +22,7 @@ #pragma once -#include +#include #include class Being; @@ -32,9 +32,8 @@ class EffectManager public: struct EffectDescription { - int id; - std::string GFX; - std::string SFX; + std::string particle; + std::string sfx; }; EffectManager(); @@ -46,7 +45,7 @@ class EffectManager * and with the given rotation in degree: * 0° = Down, 90° = left, ... */ - bool trigger(int id, Being* being, int rotation = 0); + bool trigger(int id, Being *being, int rotation = 0); /** * Triggers a effect with the id, at @@ -57,7 +56,7 @@ class EffectManager bool trigger(int id, int x, int y, int rotation = 0); private: - std::list mEffects; + std::map mEffects; }; extern EffectManager *effectManager; -- cgit v1.2.3-70-g09d2