aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/particles/particle.h
blob: 081ad2601ace886456a66c4ac1879288fc4dc25a (plain)
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
#ifndef __JE_PARTICLE_H__
#define __JE_PARTICLE_H__

#include "../../game/gameobject.h"
#include "../../math/ranged_value.h"
#include "../../math/transform.h"
#include "../../math/vector2.hpp"
#include "../../math/percentage.h"

#include "../color.h"
#include "../sprite.h"

namespace JinEngine
{
    namespace Graphics
    {
        namespace Particles
        {

            class ParticleEmitter;

            class ScaledRangedValue : public Math::RangedValue
            {
            public:
                ScaledRangedValue() {};
                ScaledRangedValue(Math::Percentage* points, uint n);
                void set(Math::Percentage* points, uint n);
                
            };

            class GradientColorValue
            {
            public:
                GradientColorValue();

                void addColor(Color col, Math::Percentage time);
                Color getColor(Math::Percentage time);
                void insertColor(uint i, Color col, Math::Percentage time);
                void removeColor(uint i);

            private:
                std::vector<Color> mColors;
                std::vector<Math::Percentage> mTimeline;
                uint mCount;

            };

            ///
            /// 
            ///
            struct LifeTimeDef
            {
                bool enableRandom = false;
                Struct(life,
                    struct
                    {
                        float floor, ceil;
                    } random;
                    float life = 1.0f;
                );
            };

            struct ScaleDef
            {
                float scale = 1;
                Struct(overTime,
                    bool enable = false;
                    ScaledRangedValue value;
                );
            };

            struct ColorDef
            {
                Color color = Color::WHITE;
                Struct(overTime,
                    bool enable = false;
                    GradientColorValue value;
                );
            };

            struct TransparencyDef
            {
                Math::Percentage transparency = 1.f;
                Struct(overTime,
                    bool enable = false;
                    ScaledRangedValue value;
                );
            };

            struct linearAccelarationDef
            {
                Math::Vector2<float> linearAccelaration;
            };

            struct RadialAccelarationDef
            {
                float radialAccelaration = 0.f;
            };

            struct AngularSpeedDef
            {
                bool enableRandom = false;
                Struct(angularSpeed,
                    struct
                {
                    float floor = 0;
                    float ceil = 0;
                } random;
                float angularSpeed = 0;
                );
            };

            enum SpriteMode
            {
                SINGLE = 1,
                RANDOM = 2,
                ANIMATED = 3,
            };

            struct SpritesDef
            {
                SpriteMode mode = SpriteMode::SINGLE;
                std::vector<const Sprite*> sprites;
            };

            struct BlendDef
            {
                bool additive = false;
            };

            ///
            /// 
            ///
            struct ParticleDef
            {
            public:
                // Basic definitions.
                LifeTimeDef lifeTimeDef;                     ///< 
                linearAccelarationDef linearAccelarationDef; ///<
                RadialAccelarationDef radialAccelarationDef; ///< 
                AngularSpeedDef angularSpeedDef;             ///<
                SpritesDef spritesDef;                       ///<
                BlendDef blendDef;                           ///<
                // Optional definitions.
                ScaleDef scaleDef;                           ///< 
                ColorDef colorDef;                           ///< 
                TransparencyDef transparencyDef;             ///<

            private:
                friend class ParticleEmitter;

            };

            ///
            /// A single particle contains various properties of particle, such as position, accelaration, color 
            /// and other attributes changed over time.
            ///
            class Particle : public Renderable, public Game::GameObject
            {
            public:
                enum ParticleUpdateMask
                {
                    NONE = 0,
                    UPDATE_COLOR = 1 << 0,
                    UPDATE_SCALE = 1 << 1,
                    UPDATE_POSITION = 1 << 2,
                    UPDATE_ROTATION = 1 << 3,
                    UPDATE_SPRITE = 1 << 4,
                    UPDATE_VELOCITY = 1 << 5,
                };

                ///
                /// Default constructor.
                ///
                Particle();
                
                ///
                /// Reset to default.
                ///
                void reset();

                ///
                ///
                ///
                void update(float dt);

                ///
                ///
                ///
                void render();

                // Methods end.

                ParticleDef* def;

                uint updateFlags = ParticleUpdateMask::NONE;

                float lifeTime = 1.0f;

                float life = 0.0f;

                int spriteIndex = 0;

                Color color = Color::WHITE;

                Math::Transform transform;

                Math::Vector2<float> velocity;
                Math::Vector2<float> linearAcceleration;

                float angularSpeed;
                float radialAcceleration = 0;

                bool alive = true;

            };

        } // namespace Particles
    } // namespace Graphics
} // namespace JinEngine

#endif