~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/actorsprite.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  The ManaPlus Client
3
 
 *  Copyright (C) 2010  The Mana Developers
4
 
 *  Copyright (C) 2011-2013  The ManaPlus Developers
5
 
 *
6
 
 *  This file is part of The ManaPlus Client.
7
 
 *
8
 
 *  This program is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License as published by
10
 
 *  the Free Software Foundation; either version 2 of the License, or
11
 
 *  any later version.
12
 
 *
13
 
 *  This program is distributed in the hope that it will be useful,
14
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 *  GNU General Public License for more details.
17
 
 *
18
 
 *  You should have received a copy of the GNU General Public License
19
 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 */
21
 
 
22
 
#include "actorsprite.h"
23
 
 
24
 
#include "actorspritelistener.h"
25
 
#include "client.h"
26
 
#include "configuration.h"
27
 
#include "effectmanager.h"
28
 
#include "imagesprite.h"
29
 
#include "localplayer.h"
30
 
#include "simpleanimation.h"
31
 
#include "soundmanager.h"
32
 
#include "statuseffect.h"
33
 
 
34
 
#include "gui/theme.h"
35
 
 
36
 
#include "net/net.h"
37
 
 
38
 
#include "resources/imageset.h"
39
 
#include "resources/resourcemanager.h"
40
 
 
41
 
#include "utils/checkutils.h"
42
 
 
43
 
#include "debug.h"
44
 
 
45
 
AnimatedSprite *ActorSprite::targetCursor[2][NUM_TC];
46
 
bool ActorSprite::loaded = false;
47
 
 
48
 
ActorSprite::ActorSprite(const int id) :
49
 
    CompoundSprite(),
50
 
    Actor(),
51
 
    mStatusEffects(),
52
 
    mStunParticleEffects(),
53
 
    mStatusParticleEffects(&mStunParticleEffects, false),
54
 
    mChildParticleEffects(&mStatusParticleEffects, false),
55
 
    mId(id),
56
 
    mStunMode(0),
57
 
    mUsedTargetCursor(nullptr),
58
 
    mActorSpriteListeners(),
59
 
    mCursorPaddingX(0),
60
 
    mCursorPaddingY(0),
61
 
    mMustResetParticles(false)
62
 
{
63
 
}
64
 
 
65
 
ActorSprite::~ActorSprite()
66
 
{
67
 
    setMap(nullptr);
68
 
 
69
 
    mUsedTargetCursor = nullptr;
70
 
 
71
 
    if (player_node && player_node->getTarget() == this)
72
 
        player_node->setTarget(nullptr);
73
 
 
74
 
    // Notify listeners of the destruction.
75
 
    FOR_EACH (ActorSpriteListenerIterator, iter, mActorSpriteListeners)
76
 
    {
77
 
        if (reportFalse(*iter))
78
 
            (*iter)->actorSpriteDestroyed(*this);
79
 
    }
80
 
}
81
 
 
82
 
bool ActorSprite::draw(Graphics *const graphics,
83
 
                       const int offsetX, const int offsetY) const
84
 
{
85
 
    FUNC_BLOCK("ActorSprite::draw", 1)
86
 
    const int px = getPixelX() + offsetX - 16;
87
 
    // Temporary fix to the Y offset.
88
 
#ifdef MANASERV_SUPPORT
89
 
    const int py = getPixelY() + offsetY -
90
 
        ((Net::getNetworkType() == ServerInfo::MANASERV) ? 15 : 32);
91
 
#else
92
 
    const int py = getPixelY() + offsetY - 32;
93
 
#endif
94
 
 
95
 
    if (mUsedTargetCursor)
96
 
    {
97
 
        mUsedTargetCursor->update(tick_time * MILLISECONDS_IN_A_TICK);
98
 
        mUsedTargetCursor->draw(graphics,
99
 
            px + getTargetOffsetX() - mCursorPaddingX,
100
 
            py + getTargetOffsetY() - mCursorPaddingY);
101
 
    }
102
 
 
103
 
    return drawSpriteAt(graphics, px, py);
104
 
}
105
 
 
106
 
bool ActorSprite::drawSpriteAt(Graphics *const graphics,
107
 
                               const int x, const int y) const
108
 
{
109
 
    return CompoundSprite::draw(graphics, x, y);
110
 
}
111
 
 
112
 
void ActorSprite::logic()
113
 
{
114
 
    BLOCK_START("ActorSprite::logic")
115
 
    // Update sprite animations
116
 
    update(tick_time * MILLISECONDS_IN_A_TICK);
117
 
 
118
 
    // Restart status/particle effects, if needed
119
 
    if (mMustResetParticles)
120
 
    {
121
 
        mMustResetParticles = false;
122
 
        FOR_EACH (std::set<int>::const_iterator, it, mStatusEffects)
123
 
        {
124
 
            const StatusEffect *const effect
125
 
                = StatusEffect::getStatusEffect(*it, true);
126
 
            if (effect && effect->particleEffectIsPersistent())
127
 
                updateStatusEffect(*it, true);
128
 
        }
129
 
    }
130
 
 
131
 
    // Update particle effects
132
 
    mChildParticleEffects.moveTo(mPos.x, mPos.y);
133
 
    BLOCK_END("ActorSprite::logic")
134
 
}
135
 
 
136
 
void ActorSprite::actorLogic()
137
 
{
138
 
}
139
 
 
140
 
void ActorSprite::setMap(Map *const map)
141
 
{
142
 
    Actor::setMap(map);
143
 
 
144
 
    // Clear particle effect list because child particles became invalid
145
 
    mChildParticleEffects.clear();
146
 
    mMustResetParticles = true;  // Reset status particles on next redraw
147
 
}
148
 
 
149
 
void ActorSprite::controlParticle(Particle *const particle)
150
 
{
151
 
    mChildParticleEffects.addLocally(particle);
152
 
}
153
 
 
154
 
void ActorSprite::setTargetType(const TargetCursorType type)
155
 
{
156
 
    static const int targetWidths[ActorSprite::NUM_TC] = {0, 0, 0};
157
 
    static const int targetHeights[ActorSprite::NUM_TC] = {-16, -16, -32};
158
 
 
159
 
    if (type == TCT_NONE)
160
 
    {
161
 
        untarget();
162
 
    }
163
 
    else
164
 
    {
165
 
        const TargetCursorSize sz = getTargetCursorSize();
166
 
        mUsedTargetCursor = targetCursor[type][sz];
167
 
        if (mUsedTargetCursor)
168
 
        {
169
 
            mCursorPaddingX = targetWidths[sz];
170
 
            mCursorPaddingY = targetHeights[sz];
171
 
        }
172
 
    }
173
 
}
174
 
 
175
 
struct EffectDescription final
176
 
{
177
 
    std::string mGFXEffect;
178
 
    std::string mSFXEffect;
179
 
};
180
 
 
181
 
void ActorSprite::setStatusEffect(const int index, const bool active)
182
 
{
183
 
    const bool wasActive = mStatusEffects.find(index) != mStatusEffects.end();
184
 
 
185
 
    if (active != wasActive)
186
 
    {
187
 
        updateStatusEffect(index, active);
188
 
        if (active)
189
 
            mStatusEffects.insert(index);
190
 
        else
191
 
            mStatusEffects.erase(index);
192
 
    }
193
 
}
194
 
 
195
 
void ActorSprite::setStatusEffectBlock(const int offset,
196
 
                                       const uint16_t newEffects)
197
 
{
198
 
    for (unsigned i = 0; i < STATUS_EFFECTS; i++)
199
 
    {
200
 
        const int index = StatusEffect::blockEffectIndexToEffectIndex(
201
 
            offset + i);
202
 
 
203
 
        if (index != -1)
204
 
            setStatusEffect(index, (newEffects & (1 << i)) > 0);
205
 
    }
206
 
}
207
 
 
208
 
void ActorSprite::updateStunMode(const int oldMode, const int newMode)
209
 
{
210
 
    handleStatusEffect(StatusEffect::getStatusEffect(oldMode, false), -1);
211
 
    handleStatusEffect(StatusEffect::getStatusEffect(newMode, true), -1);
212
 
}
213
 
 
214
 
void ActorSprite::updateStatusEffect(const int index, const bool newStatus)
215
 
{
216
 
    handleStatusEffect(StatusEffect::getStatusEffect(index, newStatus), index);
217
 
}
218
 
 
219
 
void ActorSprite::handleStatusEffect(StatusEffect *const effect,
220
 
                                     const int effectId)
221
 
{
222
 
    if (!effect)
223
 
        return;
224
 
 
225
 
    Particle *const particle = effect->getParticle();
226
 
 
227
 
    if (effectId >= 0)
228
 
    {
229
 
        mStatusParticleEffects.setLocally(effectId, particle);
230
 
    }
231
 
    else
232
 
    {
233
 
        mStunParticleEffects.clearLocally();
234
 
        if (particle)
235
 
            mStunParticleEffects.addLocally(particle);
236
 
    }
237
 
}
238
 
 
239
 
void ActorSprite::setupSpriteDisplay(const SpriteDisplay &display,
240
 
                                     const bool forceDisplay,
241
 
                                     const int imageType,
242
 
                                     const std::string &color)
243
 
{
244
 
    clear();
245
 
 
246
 
    FOR_EACH (SpriteRefs, it, display.sprites)
247
 
    {
248
 
        if (!*it)
249
 
            continue;
250
 
        const std::string file = paths.getStringValue("sprites").append(
251
 
            combineDye3((*it)->sprite, color));
252
 
 
253
 
        const int variant = (*it)->variant;
254
 
        addSprite(AnimatedSprite::delayedLoad(file, variant));
255
 
    }
256
 
 
257
 
    // Ensure that something is shown, if desired
258
 
    if (empty() && forceDisplay)
259
 
    {
260
 
        if (display.image.empty())
261
 
        {
262
 
            addSprite(AnimatedSprite::delayedLoad(
263
 
                paths.getStringValue("sprites").append(
264
 
                paths.getStringValue("spriteErrorFile"))));
265
 
        }
266
 
        else
267
 
        {
268
 
            ResourceManager *const resman = ResourceManager::getInstance();
269
 
            std::string imagePath;
270
 
            switch (imageType)
271
 
            {
272
 
                case 0:
273
 
                default:
274
 
                    imagePath = paths.getStringValue("itemIcons").append(
275
 
                        display.image);
276
 
                    break;
277
 
                case 1:
278
 
                    imagePath = paths.getStringValue("itemIcons").append(
279
 
                        display.floor);
280
 
                    break;
281
 
            }
282
 
            imagePath = combineDye2(imagePath, color);
283
 
 
284
 
            Image *img = resman->getImage(imagePath);
285
 
 
286
 
            if (!img)
287
 
                img = Theme::getImageFromTheme("unknown-item.png");
288
 
 
289
 
            addSprite(new ImageSprite(img));
290
 
            if (img)
291
 
                img->decRef();
292
 
        }
293
 
    }
294
 
 
295
 
    mChildParticleEffects.clear();
296
 
 
297
 
    // setup particle effects
298
 
    if (Particle::enabled && particleEngine)
299
 
    {
300
 
        FOR_EACH (StringVectCIter, itr, display.particles)
301
 
        {
302
 
            Particle *const p = particleEngine->addEffect(*itr, 0, 0);
303
 
            controlParticle(p);
304
 
        }
305
 
    }
306
 
 
307
 
    mMustResetParticles = true;
308
 
}
309
 
 
310
 
void ActorSprite::load()
311
 
{
312
 
    if (loaded)
313
 
        unload();
314
 
 
315
 
    initTargetCursor();
316
 
 
317
 
    loaded = true;
318
 
}
319
 
 
320
 
void ActorSprite::unload()
321
 
{
322
 
    if (reportTrue(!loaded))
323
 
        return;
324
 
 
325
 
    cleanupTargetCursors();
326
 
    loaded = false;
327
 
}
328
 
 
329
 
void ActorSprite::addActorSpriteListener(ActorSpriteListener *const listener)
330
 
{
331
 
    mActorSpriteListeners.push_front(listener);
332
 
}
333
 
 
334
 
void ActorSprite::removeActorSpriteListener(ActorSpriteListener *const
335
 
                                            listener)
336
 
{
337
 
    mActorSpriteListeners.remove(listener);
338
 
}
339
 
 
340
 
static const char *cursorType(const int type)
341
 
{
342
 
    switch (type)
343
 
    {
344
 
        case ActorSprite::TCT_IN_RANGE:
345
 
            return "in-range";
346
 
        default:
347
 
        case ActorSprite::TCT_NORMAL:
348
 
            return "normal";
349
 
    }
350
 
}
351
 
 
352
 
static const char *cursorSize(const int size)
353
 
{
354
 
    switch (size)
355
 
    {
356
 
        case ActorSprite::TC_LARGE:
357
 
            return "l";
358
 
        case ActorSprite::TC_MEDIUM:
359
 
            return "m";
360
 
        default:
361
 
        case ActorSprite::TC_SMALL:
362
 
            return "s";
363
 
    }
364
 
}
365
 
 
366
 
void ActorSprite::initTargetCursor()
367
 
{
368
 
    static const std::string targetCursorFile = "%s/target-cursor-%s-%s.xml";
369
 
 
370
 
    const std::string path = branding.getStringValue("guiPath");
371
 
 
372
 
    // Load target cursors
373
 
    for (int size = TC_SMALL; size < NUM_TC; size++)
374
 
    {
375
 
        for (int type = TCT_NORMAL; type < NUM_TCT; type++)
376
 
        {
377
 
            targetCursor[type][size] = AnimatedSprite::load(strprintf(
378
 
                targetCursorFile.c_str(), path.c_str(), cursorType(type),
379
 
                cursorSize(size)));
380
 
        }
381
 
    }
382
 
}
383
 
 
384
 
void ActorSprite::cleanupTargetCursors()
385
 
{
386
 
    for (int size = TC_SMALL; size < NUM_TC; size++)
387
 
    {
388
 
        for (int type = TCT_NORMAL; type < NUM_TCT; type++)
389
 
        {
390
 
            if (targetCursor[type][size])
391
 
            {
392
 
                delete targetCursor[type][size];
393
 
                targetCursor[type][size] = nullptr;
394
 
            }
395
 
        }
396
 
    }
397
 
}