summaryrefslogtreecommitdiff
path: root/src/effectmanager.cpp
blob: e995280ffa6213ded3662bf6ed304fa8e838d001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  The ManaPlus Client
 *  Copyright (C) 2008  Fate <fate.tmw@googlemail.com>
 *  Copyright (C) 2008  Chuck Miller <shadowmil@gmail.com>
 *  Copyright (C) 2011-2018  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "effectmanager.h"

#include "configuration.h"
#include "soundmanager.h"

#include "being/being.h"

#include "utils/checkutils.h"

#include "particle/particle.h"

#include "resources/beingcommon.h"

#include "debug.h"

EffectManager *effectManager = nullptr;

EffectManager::EffectManager() :
    mEffects(),
    mTimers()
{
    logger->log1("Effects are now loading");
    loadXmlFile(paths.getStringValue("effectsFile"), SkipError_false);
    loadXmlFile(paths.getStringValue("effectsPatchFile"), SkipError_true);
    loadXmlDir("effectsPatchDir", loadXmlFile);
}

void EffectManager::loadXmlFile(const std::string &fileName,
                                const SkipError skipError)
{
    XML::Document doc(fileName, UseVirtFs_true, skipError);
    XmlNodeConstPtrConst root = doc.rootNode();

    if ((root == nullptr) ||
        !xmlNameEqual(root, "being-effects"))
    {
        logger->log("Error loading being effects file: " + fileName);
        return;
    }

    for_each_xml_child_node(node, root)
    {
        if (xmlNameEqual(node, "include"))
        {
            const std::string name = XML::getProperty(node, "name", "");
            if (!name.empty())
                loadXmlFile(name, skipError);
            continue;
        }
        else if (xmlNameEqual(node, "effect"))
        {
            mEffects.push_back(EffectDescription(
                XML::getProperty(node, "id", -1),
                XML::getProperty(node, "particle", ""),
                XML::getProperty(node, "audio", ""),
                XML::getProperty(node, "sprite", "")));
        }
    }
}

EffectManager::~EffectManager()
{
}

bool EffectManager::triggerDirection(const int id,
                                     Being *const being,
                                     const SpriteDirection::Type &direction)
{
    int rotation;
    switch (direction)
    {
        case SpriteDirection::DOWN:
        case SpriteDirection::DOWNRIGHT:
        case SpriteDirection::DOWNLEFT:
        case SpriteDirection::DEFAULT:
        case SpriteDirection::INVALID:
        default:
            rotation = 0;
            break;
        case SpriteDirection::LEFT:
            rotation = 90;
            break;
        case SpriteDirection::UP:
        case SpriteDirection::UPRIGHT:
        case SpriteDirection::UPLEFT:
            rotation = 180;
            break;
        case SpriteDirection::RIGHT:
            rotation = 270;
            break;
    }

    return trigger(id, being, rotation);
}

bool EffectManager::trigger(const int id,
                            Being *const being,
                            const int rotation)
{
    if ((being == nullptr) || (particleEngine == nullptr) || id == -1)
        return false;

    BLOCK_START("EffectManager::trigger")
    bool rValue = false;
    FOR_EACH (STD_VECTOR<EffectDescription>::const_iterator, i, mEffects)
    {
        const EffectDescription &effect = *i;
        if (effect.id == id)
        {
            rValue = true;
            if (!effect.gfx.empty())
            {
                Particle *const selfFX = particleEngine->addEffect(
                    effect.gfx, 0, 0, rotation);
                being->controlAutoParticle(selfFX);
            }
            if (!effect.sfx.empty())
                soundManager.playSfx(effect.sfx, 0, 0);
            if (!effect.sprite.empty())
                being->addEffect(effect.sprite);
            return rValue;
        }
    }
    reportAlways("Missing effect %d", id);
    BLOCK_END("EffectManager::trigger")
    return rValue;
}

Particle *EffectManager::triggerReturn(const int id,
                                       Being *const being,
                                       const int rotation)
{
    if ((being == nullptr) || (particleEngine == nullptr) || id == -1)
        return nullptr;

    Particle *rValue = nullptr;
    FOR_EACH (STD_VECTOR<EffectDescription>::const_iterator, i, mEffects)
    {
        const EffectDescription &effect = *i;
        if (effect.id == id)
        {
            if (!effect.gfx.empty())
            {
                rValue = particleEngine->addEffect(
                    effect.gfx, 0, 0, rotation);
                being->controlCustomParticle(rValue);
            }
            if (!effect.sfx.empty())
                soundManager.playSfx(effect.sfx, 0, 0);
            if (!effect.sprite.empty())
                being->addEffect(effect.sprite);
            return rValue;
        }
    }
    reportAlways("Missing effect %d", id);
    return rValue;
}

bool EffectManager::trigger(const int id,
                            const int x, const int y,
                            const time_t endTime,
                            const int rotation)
{
    if ((particleEngine == nullptr) || id == -1)
        return false;

    bool rValue = false;
    FOR_EACH (STD_VECTOR<EffectDescription>::const_iterator, i, mEffects)
    {
        const EffectDescription &effect = *i;
        if (effect.id == id)
        {
            rValue = true;
            if (!effect.gfx.empty())
            {
                Particle *const particle = particleEngine->addEffect(
                    effect.gfx,
                    x, y,
                    rotation);
                if (particle != nullptr)
                    mTimers.push_back(ParticleTimer(particle, endTime));
            }
            if (!effect.sfx.empty())
                soundManager.playSfx(effect.sfx, 0, 0);
            return rValue;
        }
    }
    reportAlways("Missing effect %d", id);
    return rValue;
}

void EffectManager::triggerDefault(int effectId,
                                   Being *const being,
                                   const int defaultEffectId)
{
    if (effectId == -1)
        effectId = defaultEffectId;
    if (effectId == -1)
        return;
    trigger(effectId, being, 0);
}

void EffectManager::triggerDefault(int effectId,
                                   const int x,
                                   const int y,
                                   const time_t endTime,
                                   const int defaultEffectId)
{
    if (effectId == -1)
        effectId = defaultEffectId;
    if (effectId == -1)
        return;
    trigger(effectId, x, y, endTime, 0);
}

void EffectManager::logic()
{
    const time_t time = cur_time;
    bool found(true);
    while (found)
    {
        found = false;
        FOR_EACH (std::list<ParticleTimer>::iterator, it, mTimers)
        {
            const ParticleTimer &timer = *it;
            if (timer.endTime < time)
            {
                found = true;
                Particle *const particle = timer.particle;
                if (particle != nullptr)
                    particle->kill();
                mTimers.erase(it);
                break;
            }
        }
    }
}

void EffectManager::clear()
{
    FOR_EACH (std::list<ParticleTimer>::iterator, it, mTimers)
    {
        Particle *const particle = (*it).particle;
        if (particle != nullptr)
            particle->kill();
    }
    mTimers.clear();
}