Mana
Loading...
Searching...
No Matches
actorsprite.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2010-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "actorsprite.h"
22
23#include "configuration.h"
24#include "event.h"
25#include "localplayer.h"
26#include "log.h"
27#include "particle.h"
28#include "simpleanimation.h"
29#include "sprite.h"
30
31#include "resources/animation.h"
32#include "resources/imageset.h"
34#include "resources/theme.h"
35
36#include "utils/time.h"
37
38#include <algorithm>
39#include <cassert>
40
41#define EFFECTS_FILE "effects.xml"
42
45bool ActorSprite::loaded = false;
46
48 : mId(id)
49{}
50
52{
53 // Notify listeners of the destruction.
55 event.setActor("source", this);
56 event.trigger(Event::ActorSpriteChannel);
57}
58
60{
61 return Actor::getDrawOrder() + paths.getIntValue("spriteOffsetY");
62}
63
64bool ActorSprite::draw(Graphics *graphics, int offsetX, int offsetY) const
65{
66 int px = getPixelX() + offsetX;
67 int py = getPixelY() + offsetY;
68
71
72 return drawSpriteAt(graphics, px, py);
73}
74
76{
77 y += paths.getIntValue("spriteOffsetY");
78 return mSprites.draw(graphics, x, y);
79}
80
82{
83 // Update sprite animations
85
88
89 // Erase all extinct particle effects
90 mChildParticleEffects.erase(std::remove_if(mChildParticleEffects.begin(),
92 [](const Particle *p) { return p->isExtinct(); }),
94
95 // Move the remaining
96 const float py = mPos.y + paths.getIntValue("spriteOffsetY");
98 p->moveTo(mPos.x, py);
99}
100
102{
103 Actor::setMap(map);
104
105 // Clear particle effect list because child particles became invalid
106 mChildParticleEffects.clear();
107}
108
110{
111 mChildParticleEffects.emplace_back(particle);
112}
113
115{
116 if (type == TCT_NONE)
117 untarget();
118 else
120}
121
123 bool forceDisplay)
124{
125 mSprites.clear();
126
127 for (const auto &sprite : display.sprites)
128 {
129 std::string file = paths.getStringValue("sprites") + sprite.sprite;
130 mSprites.add(Sprite::load(file, sprite.variant));
131 }
132
133 // Ensure that something is shown, if desired
134 if (mSprites.size() == 0 && forceDisplay)
135 {
137 + paths.getStringValue("spriteErrorFile")));
138 }
139
140 mChildParticleEffects.clear();
141
142 //setup particle effects
144 {
145 for (const auto &particle : display.particles)
146 controlParticle(particleEngine->addEffect(particle, 0, 0));
147 }
148}
149
151{
152 if (loaded)
153 unload();
154
156
157 loaded = true;
158}
159
161{
162 if (!loaded)
163 return;
164
166 loaded = false;
167}
168
170{
171 static const std::string targetCursor = "graphics/target-cursor-%s-%s.png";
172 static const char * const cursorTypeStr[NUM_TCT] = {
173 "normal",
174 "in-range"
175 };
176 static const int targetWidths[NUM_TC] = { 44, 62, 82 };
177 static const int targetHeights[NUM_TC] = { 35, 44, 60 };
178 static const char * const cursorSizeStr[NUM_TC] = { "s", "m", "l" };
179
180 // Load target cursors
181 for (int size = 0; size < NUM_TC; size++)
182 {
183 for (int type = 0; type < NUM_TCT; type++)
184 {
186 cursorTypeStr[type],
187 cursorSizeStr[size]),
188 targetWidths[size],
189 targetHeights[size], type, size);
190 }
191 }
192}
193
195{
196 for (int size = 0; size < NUM_TC; size++)
197 {
198 for (int type = 0; type < NUM_TCT; type++)
199 {
200 delete targetCursor[type][size];
201 targetCursorImages[type][size] = nullptr;
202 }
203 }
204}
205
206void ActorSprite::loadTargetCursor(const std::string &filename,
207 int width, int height, int type, int size)
208{
209 assert(size > -1);
210 assert(size < 3);
211
213 auto currentImageSet = resman->getImageSet(filename, width, height);
214 if (!currentImageSet)
215 {
216 Log::info("Error loading target cursor: %s", filename.c_str());
217 return;
218 }
219
220 Animation anim;
221
222 for (unsigned int i = 0; i < currentImageSet->size(); ++i)
223 {
224 anim.addFrame(currentImageSet->get(i), DEFAULT_FRAME_DELAY,
225 -(currentImageSet->getWidth() / 2),
226 -(currentImageSet->getHeight() / 2));
227 }
228
229 auto *currentCursor = new SimpleAnimation(std::move(anim));
230
231 targetCursorImages[type][size] = currentImageSet;
232 targetCursor[type][size] = currentCursor;
233}
bool draw(Graphics *graphics, int offsetX, int offsetY) const override
Draws the Actor to the given graphics context.
virtual void logic()
~ActorSprite() override
static void loadTargetCursor(const std::string &filename, int width, int height, int type, int size)
Helper function for loading target cursors.
static ResourceRef< ImageSet > targetCursorImages[NUM_TCT][NUM_TC]
Images of the target cursor.
void controlParticle(Particle *particle)
Take control of a particle.
void setTargetType(TargetCursorType type)
Sets the target animation for this actor.
static void initTargetCursor()
Load the target cursors into memory.
virtual TargetCursorSize getTargetCursorSize() const
Returns the required size of a target cursor for this being.
Definition actorsprite.h:97
static void cleanupTargetCursors()
Remove the target cursors from memory.
void setMap(Map *map) override
bool drawSpriteAt(Graphics *graphics, int x, int y) const
static SimpleAnimation * targetCursor[NUM_TCT][NUM_TC]
Animated target cursors.
void setupSpriteDisplay(const SpriteDisplay &display, bool forceDisplay=true)
void untarget()
Untargets the actor.
static bool loaded
SimpleAnimation * mUsedTargetCursor
Target cursor being used.
static void load()
CompoundSprite mSprites
static void unload()
ActorSprite(int id)
int getDrawOrder() const override
Returns the pixel Y coordinate that the actor should be drawn at.
std::vector< ParticleHandle > mChildParticleEffects
virtual void setMap(Map *map)
Definition actor.cpp:32
int getPixelX() const
Returns the pixels X coordinate of the actor.
Definition actor.h:65
int getPixelY() const
Returns the pixel Y coordinate of the actor.
Definition actor.h:71
virtual int getDrawOrder() const
Returns the pixel Y coordinate that the actor should be drawn at.
Definition actor.h:77
Vector mPos
Position in pixels relative to map.
Definition actor.h:114
An animation consists of several frames, each with their own delay and offset.
Definition animation.h:47
void addFrame(Image *image, int delay, int offsetX, int offsetY)
Appends a new animation at the end of the sequence.
Definition animation.cpp:29
void add(Sprite *sprite)
bool draw(Graphics *graphics, int posX, int posY) const
size_t size() const
bool update(int time)
std::string getStringValue(const std::string &key) const
int getIntValue(const std::string &key) const
returns a value corresponding to the given key.
Definition event.h:42
@ Destroyed
Definition event.h:71
@ ActorSpriteChannel
Definition event.h:46
A central point of control for graphics.
Definition graphics.h:78
A tile map.
Definition map.h:147
A particle spawned by a ParticleEmitter.
Definition particle.h:42
void moveTo(const Vector &pos)
Sets the position in 3 dimensional space in pixels relative to map.
Definition particle.h:134
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
static bool enabled
true when non-crucial particle effects are disabled
Definition particle.h:59
A class for loading and managing resources.
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
ResourceRef< ImageSet > getImageSet(const std::string &imagePath, int w, int h)
Loads a image set based on the image referenced by the given path and the supplied sprite sizes.
Automatically counting Resource reference.
Definition resource.h:74
This class is a leightweight alternative to the Sprite class.
void update(int dt)
bool draw(Graphics *graphics, int posX, int posY) const
static Sprite * load(const std::string &filename, int variant=0)
An helper function, which will request the sprite to animate from the resource manager.
Definition sprite.cpp:42
float y
Definition vector.h:172
float x
Definition vector.h:171
Configuration paths
XML default paths information reader.
Definition client.cpp:99
Graphics * graphics
Definition client.cpp:104
Particle * particleEngine
Definition game.cpp:113
void info(const char *log_text,...) LOG_PRINTF_ATTR
unsigned deltaTimeMs()
The time in milliseconds since the last frame, but never more than 1000.
Definition time.cpp:39
const int DEFAULT_FRAME_DELAY
Definition sprite.h:31
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::vector< SpriteReference > sprites
Definition spritedef.h:44
std::vector< std::string > particles
Definition spritedef.h:45