Mana
Loading...
Searching...
No Matches
effectmanager.cpp
Go to the documentation of this file.
1/*
2 * An effects manager
3 * Copyright (C) 2008 Fate <fate.tmw@googlemail.com>
4 * Copyright (C) 2008 Chuck Miller <shadowmil@gmail.com>
5 * Copyright (C) 2009-2012 The Mana Developers
6 *
7 * This file is part of The Mana Client.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "being.h"
24#include "effectmanager.h"
25#include "log.h"
26#include "particle.h"
27#include "sound.h"
28
29#include "utils/xml.h"
30
32{
33 XML::Document doc("effects.xml");
34 XML::Node root = doc.rootNode();
35
36 if (!root || root.name() != "effects")
37 {
38 // Handle old naming until the 0.5.x versions are obsolete.
39 if (!root || root.name() != "being-effects")
40 {
41 Log::info("Error loading being effects file: effects.xml");
42 return;
43 }
44 }
45 else
46 {
47 Log::info("Effects are now loading");
48 }
49
50 for (auto node : root.children())
51 {
52 int effectId;
53
54 if (node.name() == "effect" && node.attribute("id", effectId))
55 {
56 EffectDescription &ed = mEffects[effectId];
57 node.attribute("particle", ed.particle);
58 node.attribute("audio", ed.sfx);
59 }
60 }
61}
62
64
65bool EffectManager::trigger(int id, Being *being, int rotation)
66{
67 auto it = mEffects.find(id);
68 if (it == mEffects.end())
69 {
70 Log::warn("EffectManager::trigger: effect %d not found", id);
71 return false;
72 }
73
74 EffectDescription &effect = it->second;
75
76 if (!effect.particle.empty())
77 {
78 if (Particle *selfFX = particleEngine->addEffect(effect.particle, 0, 0, rotation))
79 being->controlParticle(selfFX);
80 }
81
82 if (!effect.sfx.empty())
83 sound.playSfx(effect.sfx);
84
85 return true;
86}
87
88bool EffectManager::trigger(int id, int x, int y, int rotation)
89{
90 auto it = mEffects.find(id);
91 if (it == mEffects.end())
92 {
93 Log::warn("EffectManager::trigger: effect %d not found", id);
94 return false;
95 }
96
97 EffectDescription &effect = it->second;
98
99 if (!effect.particle.empty())
100 particleEngine->addEffect(effect.particle, x, y, rotation);
101 if (!effect.sfx.empty())
102 sound.playSfx(effect.sfx);
103
104 return true;
105}
void controlParticle(Particle *particle)
Take control of a particle.
Definition being.h:65
bool trigger(int id, Being *being, int rotation=0)
Triggers a effect with the id, at the specified being, and with the given rotation in degree: 0° = Do...
std::map< int, EffectDescription > mEffects
A particle spawned by a ParticleEmitter.
Definition particle.h:42
Particle * addEffect(const std::string &particleEffectFile, int pixelX, int pixelY, int rotation=0)
Creates a child particle that hosts some emitters described in the particleEffectFile.
Definition particle.cpp:249
void playSfx(const std::string &path, int x=0, int y=0)
Plays a sound at the specified location.
Definition sound.cpp:248
A helper class for parsing an XML document, which also cleans it up again (RAII).
Definition xml.h:190
Node rootNode() const
Returns the root node of the document (or NULL if there was a load error).
Definition xml.h:213
std::string_view name() const
Definition xml.h:46
Children children() const
Definition xml.h:97
Sound sound
Definition client.cpp:109
Particle * particleEngine
Definition game.cpp:113
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR