Mana
Loading...
Searching...
No Matches
particle.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2006-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana 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#pragma once
23
24#include "actor.h"
25#include "guichanfwd.h"
26#include "vector.h"
27
28#include <list>
29#include <string>
30
31class Map;
32class Particle;
33class ParticleEmitter;
34
35using Particles = std::list<Particle *>;
36using Emitters = std::list<ParticleEmitter *>;
37
41class Particle : public Actor
42{
43 public:
54 static const float PARTICLE_SKY;
55 static int fastPhysics;
56 static int particleCount;
57 static int maxCount;
58 static int emitterSkip;
59 static bool enabled;
66 Particle(Map *map);
67
68 ~Particle() override;
69
73 void clear();
74
79 static void setupEngine();
80
85 virtual bool update();
86
90 bool draw(Graphics *graphics, int offsetX, int offsetY) const override;
91
95 bool drawnWhenBehind() const override
96 { return false; }
97
103
108 Particle *addEffect(const std::string &particleEffectFile,
109 int pixelX, int pixelY, int rotation = 0);
110
114 Particle *addTextSplashEffect(const std::string &text, int x, int y,
115 const gcn::Color *color, gcn::Font *font,
116 bool outline = false);
117
121 Particle *addTextRiseFadeOutEffect(const std::string &text,
122 int x, int y, const gcn::Color *color, gcn::Font *font,
123 bool outline = false);
124
129 { mChildEmitters.push_back(emitter); }
130
134 void moveTo(const Vector &pos)
135 { moveBy (pos - mPos);}
136
140 void moveTo(float x, float y);
141
145 void moveBy (const Vector &change);
146
150 void setLifetime(int lifetime)
151 { mLifetimeLeft = lifetime; mLifetimePast = 0; }
152
157 void setFadeOut(int fadeOut)
158 { mFadeOut = fadeOut; }
159
164 void setFadeIn(int fadeIn)
165 { mFadeIn = fadeIn; }
166
170 void setVelocity(float x, float y, float z)
171 { mVelocity.x = x; mVelocity.y = y; mVelocity.z = z; }
172
176 void setGravity(float gravity)
177 { mGravity = gravity; }
178
182 void setRandomness(int r)
183 { mRandomness = r; }
184
189 void setBounce(float bouncieness)
190 { mBounce = bouncieness; }
191
195 void setFollow(bool follow)
196 { mFollow = follow; }
197
201 bool doesFollow() const
202 { return mFollow; }
203
208 void setDestination(Particle *target, float accel, float moment)
209 { mTarget = target; mAcceleration = accel; mMomentum = moment; }
210
216 void setDieDistance(float dist)
217 { mInvDieDistance = 1.0f / dist; }
218
223 void adjustEmitterSize(int w, int h);
224
225 void setAllowSizeAdjust(bool adjust)
226 { mAllowSizeAdjust = adjust; }
227
228 bool isAlive() const
229 { return mAlive == ALIVE; }
230
234 bool isExtinct() const
235 { return !isAlive() && mChildParticles.empty(); }
236
240 void kill()
241 { mAlive = DEAD_OTHER; mAutoDelete = true; }
242
248 { mAutoDelete = false; }
249
250 float getAlpha() const override
251 { return 1.0f; }
252
253 void setAlpha(float alpha) override {}
254
255 virtual void setDeathEffect(const std::string &effectFile, char conditions)
256 { mDeathEffect = effectFile; mDeathEffectConditions = conditions; }
257
258 protected:
260 float mAlpha = 1.0f;
261
263 float getCurrentAlpha() const;
264
265 int mLifetimeLeft = -1;
267 int mFadeOut = 0;
268 int mFadeIn = 0;
271 private:
273 // generic properties
274 bool mAutoDelete = true;
277 bool mAllowSizeAdjust = false;
278 std::string mDeathEffect;
281 // dynamic particle
282 float mGravity = 0.0f;
283 int mRandomness = 0;
284 float mBounce = 0.0f;
285 bool mFollow = false;
287 // follow-point particles
288 Particle *mTarget = nullptr;
289 float mAcceleration = 0.0f;
290 float mInvDieDistance = -1.0f;
291 float mMomentum = 1.0f;
292};
293
299{
300 public:
301 explicit ParticleHandle(Particle *particle = nullptr):
302 mParticle(particle)
303 {
304 if (mParticle)
306 }
307
309
311 mParticle(other.mParticle)
312 {
313 other.mParticle = nullptr;
314 }
315
317 {
318 if (mParticle)
319 mParticle->kill();
320 }
321
323
325 {
326 if (this != &other)
327 {
328 if (mParticle)
329 mParticle->kill();
330 mParticle = other.mParticle;
331 other.mParticle = nullptr;
332 }
333 return *this;
334 }
335
336 Particle *operator->() const { return mParticle; }
337 operator Particle *() const { return mParticle; }
338
339 private:
341};
342
Definition actor.h:35
Vector mPos
Position in pixels relative to map.
Definition actor.h:114
A central point of control for graphics.
Definition graphics.h:78
A tile map.
Definition map.h:147
Every Particle can have one or more particle emitters that create new particles when they are updated...
A handle on a particle.
Definition particle.h:299
Particle * operator->() const
Definition particle.h:336
Particle * mParticle
Definition particle.h:340
ParticleHandle(const ParticleHandle &)=delete
ParticleHandle(ParticleHandle &&other)
Definition particle.h:310
ParticleHandle & operator=(ParticleHandle &&other)
Definition particle.h:324
ParticleHandle & operator=(const ParticleHandle &)=delete
ParticleHandle(Particle *particle=nullptr)
Definition particle.h:301
A particle spawned by a ParticleEmitter.
Definition particle.h:42
Particles mChildParticles
List of particles controlled by this particle.
Definition particle.h:276
void kill()
Manually marks the particle for deletion.
Definition particle.h:240
float mBounce
How much the particle bounces off when hitting the ground.
Definition particle.h:284
~Particle() override
Definition particle.cpp:63
float getCurrentAlpha() const
Calculates the current alpha transparency taking current fade status into account.
Definition particle.cpp:410
void setAllowSizeAdjust(bool adjust)
Definition particle.h:225
void setVelocity(float x, float y, float z)
Sets the current velocity in 3 dimensional space.
Definition particle.h:170
float mMomentum
How much speed the particle retains after each game tick.
Definition particle.h:291
void setRandomness(int r)
Sets the ammount of random vector changes.
Definition particle.h:182
float mGravity
Downward acceleration in pixels per game-tick.
Definition particle.h:282
void setAlpha(float alpha) override
Sets the alpha value used to draw the actor.
Definition particle.h:253
bool doesFollow() const
Gets the flag if the particle is supposed to be moved by its parent.
Definition particle.h:201
virtual void setDeathEffect(const std::string &effectFile, char conditions)
Definition particle.h:255
AliveStatus mAlive
Is the particle supposed to be drawn and updated?
Definition particle.h:272
int mRandomness
Ammount of random vector change.
Definition particle.h:283
static void setupEngine()
Gives a particle the properties of an engine root particle and loads the particle-related config sett...
Definition particle.cpp:71
static const float PARTICLE_SKY
Maximum Z position of particles.
Definition particle.h:54
static int particleCount
Current number of particles.
Definition particle.h:56
static int maxCount
Maximum number of particles.
Definition particle.h:57
void moveBy(const Vector &change)
Changes the particle position relative.
Definition particle.cpp:228
void addEmitter(ParticleEmitter *emitter)
Adds an emitter to the particle.
Definition particle.h:128
int mFadeIn
Age in game ticks where fading in is finished.
Definition particle.h:268
bool isExtinct() const
Determines whether the particle and its children are all dead.
Definition particle.h:234
void setFadeIn(int fadeIn)
Sets the remaining particle lifetime where the particle starts to fade out.
Definition particle.h:164
void disableAutoDelete()
After calling this function the particle will only request deletion when kill() is called.
Definition particle.h:247
float mAcceleration
Acceleration towards the target particle in pixels per game-tick.
Definition particle.h:289
bool draw(Graphics *graphics, int offsetX, int offsetY) const override
Draws the particle image.
Definition particle.cpp:80
@ DEAD_TIMEOUT
Definition particle.h:47
@ DEAD_IMPACT
Definition particle.h:50
@ DEAD_LONG_AGO
Definition particle.h:52
@ DEAD_OTHER
Definition particle.h:51
@ DEAD_SKY
Definition particle.h:49
@ DEAD_FLOOR
Definition particle.h:48
Particle * mTarget
The particle that attracts this particle.
Definition particle.h:288
bool mAutoDelete
May the particle request its deletion by the parent particle?
Definition particle.h:274
Particle * createChild()
Creates a blank particle as a child of the current particle Useful for creating target particles.
Definition particle.cpp:242
Particle * addTextRiseFadeOutEffect(const std::string &text, int x, int y, const gcn::Color *color, gcn::Font *font, bool outline=false)
Creates a standalone text particle.
Definition particle.cpp:383
std::string mDeathEffect
Particle effect file to be spawned when the particle dies.
Definition particle.h:278
float mInvDieDistance
Distance in pixels from the target particle that causes the destruction of the particle.
Definition particle.h:290
int mLifetimePast
Age of the particle in game ticks.
Definition particle.h:266
float mAlpha
Opacity of the graphical representation of the particle.
Definition particle.h:260
void setGravity(float gravity)
Sets the downward acceleration.
Definition particle.h:176
int mLifetimeLeft
Lifetime left in game ticks.
Definition particle.h:265
static int fastPhysics
Mode of squareroot calculation.
Definition particle.h:55
virtual bool update()
Updates particle position, returns false when the particle should be deleted.
Definition particle.cpp:85
void moveTo(const Vector &pos)
Sets the position in 3 dimensional space in pixels relative to map.
Definition particle.h:134
static int emitterSkip
Duration of pause between two emitter updates in ticks.
Definition particle.h:58
bool drawnWhenBehind() const override
Do not draw particles when behind other objects.
Definition particle.h:95
void setDieDistance(float dist)
Sets the distance in pixel the particle can come near the target particle before it is destroyed.
Definition particle.h:216
void setLifetime(int lifetime)
Sets the time in game ticks until the particle is destroyed.
Definition particle.h:150
void clear()
Deletes all child particles and emitters.
Definition particle.cpp:423
bool mAllowSizeAdjust
Can the effect size be adjusted by the object props in the map file?
Definition particle.h:277
void setDestination(Particle *target, float accel, float moment)
Makes the particle move toward another particle with a given acceleration and momentum.
Definition particle.h:208
bool mFollow
is this particle moved when its parent particle moves?
Definition particle.h:285
char mDeathEffectConditions
Bitfield of death conditions which trigger spawning of the death particle.
Definition particle.h:279
int mFadeOut
Lifetime in game ticks left where fading out begins.
Definition particle.h:267
Vector mVelocity
Speed in pixels per game-tick.
Definition particle.h:269
float getAlpha() const override
Returns the current alpha value used to draw the actor.
Definition particle.h:250
void setBounce(float bouncieness)
Sets the ammount of velocity particles retain after hitting the ground.
Definition particle.h:189
bool isAlive() const
Definition particle.h:228
Particle * addTextSplashEffect(const std::string &text, int x, int y, const gcn::Color *color, gcn::Font *font, bool outline=false)
Creates a standalone text particle.
Definition particle.cpp:364
Emitters mChildEmitters
List of child emitters.
Definition particle.h:275
void setFadeOut(int fadeOut)
Sets the age of the pixel in game ticks where the particle has faded in completely.
Definition particle.h:157
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 adjustEmitterSize(int w, int h)
Changes the size of the emitters so that the effect fills a rectangle of this size.
Definition particle.cpp:401
static bool enabled
true when non-crucial particle effects are disabled
Definition particle.h:59
void setFollow(bool follow)
Sets the flag if the particle is supposed to be moved by its parent.
Definition particle.h:195
Vector class.
Definition vector.h:33
float z
Definition vector.h:173
float y
Definition vector.h:172
float x
Definition vector.h:171
Graphics * graphics
Definition client.cpp:104
Particle * particleEngine
Definition game.cpp:113
std::list< Particle * > Particles
Definition particle.h:35
std::list< ParticleEmitter * > Emitters
Definition particle.h:36