summaryrefslogtreecommitdiff
path: root/src/particle.h
blob: 4073abdd5ea0ba8fb33eddac8d9cdeff92772ca5 (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
271
272
273
274
275
276
277
278
279
/*
 *  The Mana World
 *  Copyright 2006 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef _PARTICLE_H
#define _PARTICLE_H

#include <list>
#include <string>

#include <guichan/color.hpp>

#include "guichanfwd.h"
#include "sprite.h"


class Map;
class Particle;
class ParticleEmitter;

typedef std::list<Particle *> Particles;
typedef Particles::iterator ParticleIterator;
typedef std::list<ParticleEmitter *> Emitters;
typedef Emitters::iterator EmitterIterator;

/**
 * a particle spawned by a ParticleEmitter
 */
class Particle : public Sprite
{
    public:
        static const float PARTICLE_SKY;    /**< maximum Z position of particles */
        static int fastPhysics;             /**< mode of squareroot calculation */
        static int particleCount;           /**< current number of particles*/
        static int maxCount;                /**< maximum number of particles*/
        static int emitterSkip;             /**< duration of pause between two emitter updates in ticks */

        Particle(Map *map);

        ~Particle();

        /**
         * Deletes all child particles and emitters
         */
        void
        clear();

        /**
         * Gives a particle the properties of an engine root particle and loads
         * the particle-related config settings
         */
        void
        setupEngine();

        /**
         * Updates particle position, returns false when the particle should
         * be deleted
         */
        virtual bool
        update();

        /**
         * Draws the particle image
         */
        virtual void
        draw(Graphics *graphics, int offsetX, int offsetY) const;

        /**
         * Necessary for sorting with the other sprites
         */
        virtual int
        getPixelY() const
        {
            return (int)(mPosY + mPosZ) - 64;
        };

        /*
            Basic Particle properites:
         */

        /**
         * Sets the map the particle is on
         */
        void setMap(Map *map);

        /**
         * Creates a child particle that hosts some emitters described in the
         * particleEffectFile
         */
        Particle*
        addEffect (std::string particleEffectFile, int pixelX, int pixelY);

        /**
         * Creates a standalone text particle
         */
        Particle*
        addTextSplashEffect(std::string text, int colorR, int colorG, int colorB,
                            gcn::Font *font, int x, int y);

        /**
         * Adds an emitter to the particle
         */
        void
        addEmitter (ParticleEmitter* emitter)
        { mChildEmitters.push_back(emitter);};

        /**
         * Sets the position in 3 dimensional space in pixels relative to map
         */
        void
        setPosition(float x, float y, float z)
        { mPosX = x; mPosY = y; mPosZ = z; };

        /**
         * Sets the position in 2 dimensional space in pixels relative to map
         */
        void
        setPosition(float x, float y)
        { mPosX = x; mPosY = y; };

        float getPosX() const
        { return mPosX; };

        float getPosY() const
        { return mPosY; };

        float getPosZ() const
        { return mPosZ; };

        /**
         * Changes the particle position relative
         */
        void
        moveBy(float x, float y, float z)
        { mPosX += x; mPosY += y; mPosZ += z; };

        /**
         * Sets the time in game ticks until the particle is destroyed.
         */
        void
        setLifetime(int lifetime)
        { mLifetimeLeft = lifetime; mLifetimePast = 0; };

        /**
         * Sets the age of the pixel in game ticks where the particle has
         * faded in completely
         */
        void
        setFadeOut (int fadeOut)
        { mFadeOut = fadeOut; };

        /**
         * Sets the remaining particle lifetime where the particle starts to
         * fade out
         */
        void
        setFadeIn (int fadeIn)
        { mFadeIn = fadeIn; };

        /**
         * Sets the sprite iterator of the particle on the current map to make
         * it easier to remove the particle from the map when it is destroyed
         */
        void
        setSpriteIterator(std::list<Sprite*>::iterator spriteIterator)
        { mSpriteIterator = spriteIterator; };

        /**
         * Gets the sprite iterator of the particle on the current map
         */
        std::list<Sprite*>::iterator
        getSpriteIterator() const
        { return mSpriteIterator; };

        /**
         * Sets the current velocity in 3 dimensional space
         */
        void
        setVector(float x, float y, float z)
        { mVectorX = x; mVectorY = y; mVectorZ = z; };

        /**
         * Sets the downward acceleration
         */
        void
        setGravity(float g)
        { mGravity = g; };

        /**
         * Sets the ammount of random vector changes
         */
        void
        setRandomnes(int r)
        { mRandomnes = r; };

        /**
         * Sets the ammount of velocity particles retain after
         * hitting the ground.
         */
        void
        setBounce(float bouncieness)
        { mBounce = bouncieness; };

        /**
         * Makes the particle move toward another particle with a
         * given acceleration and momentum
         */
        void setDestination(Particle *target, float accel, float moment)
        { mTarget = target; mAcceleration = accel; mMomentum = moment; };

        /**
         * Sets the distance in pixel the particle can come near the target
         * particle before it is destroyed. Does only make sense after a
         * target particle has been set using setDestination.
         */
        void setDieDistance(float dist)
        { mInvDieDistance = 1.0f / dist; };

        /**
         * Manually marks the particle for deletion
         */
        void kill()
        { mAlive = false; mAutoDelete = true; };

        /**
         * After calling this function the particle will only request
         * deletion when kill() is called
         */
        void disableAutoDelete()
        { mAutoDelete = false; };

    protected:
        bool mAlive;                /**< Is the particle supposed to be drawn and updated?*/
        float mPosX, mPosY, mPosZ;  /**< Position in 3 dimensonal space - pixel based relative to map */
        int mLifetimeLeft;          /**< Lifetime left in game ticks*/
        int mLifetimePast;          /**< Age of the particle in game ticks*/
        int mFadeOut;               /**< Lifetime in game ticks left where fading out begins*/
        int mFadeIn;                /**< Age in game ticks where fading in is finished*/

    private:
        // generic properties
        bool mAutoDelete;           /**< May the particle request its deletion by the parent particle?*/
        Map *mMap;                  /**< Map the particle is on*/
        std::list<Sprite*>::iterator mSpriteIterator;   /**< iterator of the particle on the current map */
        Emitters mChildEmitters;    /**< List of child emitters*/
        Particles mChildParticles;  /**< List of particles controlled by this particle*/
        //dynamic particle
        float mVectorX, mVectorY, mVectorZ; /**< Speed in 3 dimensional space in pixels per game-tick */
        float mGravity;             /**< Downward acceleration in pixels per game-tick�*/
        int mRandomnes;             /**< Ammount of random vector change*/
        float mBounce;              /**< How much the particle bounces off when hitting the ground*/
        //follow-point particles
        Particle *mTarget;          /**< The particle that attracts this particle*/
        float mAcceleration;        /**< Acceleration towards the target particle in pixels per game-tick�*/
        float mInvDieDistance;      /**< Distance in pixels from the target particle that causes the destruction of the particle*/
        float mMomentum;            /**< How much speed the particle retains after each game tick*/
};

extern Particle *particleEngine;

#endif