summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-02-23 14:07:20 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-02-25 18:28:11 +0100
commit656d74376b1458cd32c97a217d22b8e0e7e6fea7 (patch)
tree4fd4bf4275e9dc2d538865226ee5afeccfa3ae3b /src
parent6cfb5140c94dc1a8350f14a4be88d2e0e7956b4c (diff)
downloadmana-656d74376b1458cd32c97a217d22b8e0e7e6fea7.tar.gz
mana-656d74376b1458cd32c97a217d22b8e0e7e6fea7.tar.bz2
mana-656d74376b1458cd32c97a217d22b8e0e7e6fea7.tar.xz
mana-656d74376b1458cd32c97a217d22b8e0e7e6fea7.zip
Some code cleanups in EffectManager
Use a std::map to ease the lookup of triggered effects.
Diffstat (limited to 'src')
-rw-r--r--src/effectmanager.cpp75
-rw-r--r--src/effectmanager.h11
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 <list>
+#include <map>
#include <string>
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<EffectDescription> mEffects;
+ std::map<int, EffectDescription> mEffects;
};
extern EffectManager *effectManager;