summaryrefslogtreecommitdiff
path: root/src/particle/particle.h
blob: a7ef630bcc5b86cb1dd00435c87239d3323fe067 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *  The ManaPlus Client
 *  Copyright (C) 2006-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2020  The ManaPlus Developers
 *  Copyright (C) 2020-2023  The ManaVerse 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/>.
 */

#ifndef PARTICLE_PARTICLE_H
#define PARTICLE_PARTICLE_H

#include "being/actor.h"

#include "enums/particle/alivestatus.h"
#include "enums/particle/particletype.h"

#include "enums/simpletypes/beingid.h"

#include "particle/particleengine.h"

#include "localconsts.h"

class Image;
class ParticleEmitter;
class SimpleAnimation;

/**
 * A particle spawned by a ParticleEmitter.
 */
class Particle notfinal : public Actor
{
    public:
        friend class ParticleEngine;

        Particle();

        A_DELETE_COPY(Particle)

        /**
         * Destructor.
         */
        ~Particle() override;

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

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

        /**
         * Draws the particle image.
         */
        void draw(Graphics *restrict const graphics,
                  const int offsetX,
                  const int offsetY) const restrict2 override A_NONNULL(2);

        /**
         * Necessary for sorting with the other sprites.
         */
        int getPixelY() const restrict2 override A_WARN_UNUSED
        { return CAST_S32(mPos.y) - 16; }

        /**
         * Necessary for sorting with the other sprites for sorting only.
         */
        int getSortPixelY() const restrict2 override A_WARN_UNUSED
        { return CAST_S32(mPos.y) - 16; }

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

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

        /**
         * Sets the position in 3 dimensional space in pixels relative to map.
         */
        void moveTo(const Vector &restrict pos) restrict2
        { moveBy(pos - mPos); }

        /**
         * Sets the position in 2 dimensional space in pixels relative to map.
         */
        void moveTo(const float x, const float y) restrict2;

        /**
         * Changes the particle position relative
         */
        void moveBy(const Vector &restrict change) restrict2;

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

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

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

        /**
         * Sets the current velocity in 3 dimensional space.
         */
        void setVelocity(const float x,
                         const float y,
                         const float z) restrict2 noexcept2
        { mVelocity.x = x; mVelocity.y = y; mVelocity.z = z; }

        /**
         * Sets the downward acceleration.
         */
        void setGravity(const float gravity) restrict2 noexcept2
        { mGravity = gravity; }

        /**
         * Sets the ammount of random vector changes
         */
        void setRandomness(const int r) restrict2 noexcept2
        { mRandomness = r; }

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

        /**
         * Sets the flag if the particle is supposed to be moved by its parent
         */
        void setFollow(const bool follow) restrict2 noexcept2
        { mFollow = follow; }

        /**
         * Gets the flag if the particle is supposed to be moved by its parent
         */
        bool doesFollow() const restrict2 noexcept2 A_WARN_UNUSED
        { return mFollow; }

        /**
         * Makes the particle move toward another particle with a
         * given acceleration and momentum
         */
        void setDestination(Particle *restrict const target,
                            const float accel,
                            const float moment) restrict2 noexcept2
                            A_NONNULL(2)
        { 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(const float dist) restrict2
        { mInvDieDistance = 1.0F / dist; }

        /**
         * Changes the size of the emitters so that the effect fills a
         * rectangle of this size
         */
        void adjustEmitterSize(const int w, const int h) restrict2;

        void setAllowSizeAdjust(const bool adjust) restrict2 noexcept2
        { mAllowSizeAdjust = adjust; }

        bool isAlive() const restrict2 noexcept2 A_WARN_UNUSED
        { return mAlive == AliveStatus::ALIVE; }

        void prepareToDie() restrict2;

        /**
         * Determines whether the particle and its children are all dead
         */
        bool isExtinct() const restrict2 noexcept2 A_WARN_UNUSED
        { return !isAlive() && mChildParticles.empty(); }

        /**
         * Manually marks the particle for deletion.
         */
        void kill() restrict2 noexcept2
        { mAlive = AliveStatus::DEAD_OTHER; mAutoDelete = true; }

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

        /** We consider particles (at least for now) to be one layer-sprites */
        int getNumberOfLayers() const restrict2 override final
        { return 1; }

        float getAlpha() const restrict2 override final
        { return 1.0F; }

        void setAlpha(const float alpha A_UNUSED) restrict2 override
        { }

        virtual void setDeathEffect(const std::string &restrict effectFile,
                                    const signed char conditions) restrict2
        { mDeathEffect = effectFile; mDeathEffectConditions = conditions; }

        void setActor(const BeingId actor)
        { mActor = actor; }

    protected:
        void updateSelf() restrict2;

        // Opacity of the graphical representation of the particle
        float mAlpha;

        // Lifetime left in game ticks
        int mLifetimeLeft;

        // Age of the particle in game ticks
        int mLifetimePast;

        // Lifetime in game ticks left where fading out begins
        int mFadeOut;

        // Age in game ticks where fading in is finished
        int mFadeIn;

        // Speed in pixels per game-tick.
        Vector mVelocity;

        // Is the particle supposed to be drawn and updated?
        AliveStatusT mAlive;

        ParticleTypeT mType;

        /**< Used animation for this particle */
        SimpleAnimation *restrict mAnimation;

        /**< The image used for this particle. */
        Image *restrict mImage;

        BeingId mActor;

    private:
        // List of child emitters.
        Emitters mChildEmitters;

        // List of particles controlled by this particle
        Particles mChildParticles;

        Particles mChildMoveParticles;

        // Particle effect file to be spawned when the particle dies
        std::string mDeathEffect;

        // dynamic particle
        // Downward acceleration in pixels per game-tick.
        float mGravity;

        // How much the particle bounces off when hitting the ground
        float mBounce;

        // Acceleration towards the target particle in pixels per game-tick
        float mAcceleration;

        // Distance in pixels from the target particle that causes
        // the destruction of the particle
        float mInvDieDistance;

        // How much speed the particle retains after each game tick
        float mMomentum;

        // The particle that attracts this particle
        Particle *restrict mTarget;

        // Ammount of random vector change
        int mRandomness;

        // Bitfield of death conditions which trigger spawning
        // of the death particle
        signed char mDeathEffectConditions;

        // May the particle request its deletion by the parent particle?
        bool mAutoDelete;

        // Can the effect size be adjusted by the object props in the map file?
        bool mAllowSizeAdjust;

        // is this particle moved when its parent particle moves?
        bool mFollow;
};

#endif  // PARTICLE_PARTICLE_H