This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Affector.hpp
323 lines (248 loc) · 11.9 KB
/
Affector.hpp
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
/////////////////////////////////////////////////////////////////////////////////
//
// Thor C++ Library
// Copyright (c) 2011-2012 Jan Haller
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/////////////////////////////////////////////////////////////////////////////////
/// @file
/// @brief Concrete particle affector classes
#ifndef THOR_AFFECTOR_HPP
#define THOR_AFFECTOR_HPP
#include <Thor/Particles/ParticleInterfaces.hpp>
#include <Thor/Graphics/Colors.hpp>
#include <Thor/Config.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Vector2.hpp>
namespace thor
{
/// @addtogroup Particles
/// @{
/// @brief Applies a translational acceleration to particles over time.
/// @details Affector class that applies an acceleration vector to each particle. A popular use case is gravity.
class THOR_API ForceAffector : public Affector
{
// ---------------------------------------------------------------------------------------------------------------------------
// Public types
public:
/// @brief Shared pointer type referring to ForceAffector objects
///
typedef std::shared_ptr<ForceAffector> Ptr;
// ---------------------------------------------------------------------------------------------------------------------------
// Public static member functions
public:
/// @brief Creates an affector that accelerates particles continuously.
/// @copydetails ForceAffector::ForceAffector()
static Ptr create(sf::Vector2f acceleration);
// ---------------------------------------------------------------------------------------------------------------------------
// Public member functions
public:
/// @brief Constructor
/// @param acceleration The acceleration vector. The particle's velocity changes by this vector each second.
explicit ForceAffector(sf::Vector2f acceleration);
virtual void affect(Particle& particle, sf::Time dt);
/// @brief Sets the linear acceleration applied to the particles.
///
void setAcceleration(sf::Vector2f acceleration);
/// @brief Returns the linear acceleration applied to the particles.
///
sf::Vector2f getAcceleration() const;
// ---------------------------------------------------------------------------------------------------------------------------
// Private variables
private:
sf::Vector2f mAcceleration;
};
/// @brief Applies a rotational acceleration to particles over time.
/// @details Affector class that applies a scalar angular acceleration value to each particle.
class THOR_API TorqueAffector : public Affector
{
// ---------------------------------------------------------------------------------------------------------------------------
// Public types
public:
/// @brief Shared pointer type referring to TorqueAffector objects
///
typedef std::shared_ptr<TorqueAffector> Ptr;
// ---------------------------------------------------------------------------------------------------------------------------
// Public static member functions
public:
/// @brief Creates an affector that applies an angular acceleration to particles.
/// @copydetails TorqueAffector::TorqueAffector()
static Ptr create(float angularAcceleration);
// ---------------------------------------------------------------------------------------------------------------------------
// Public member functions
public:
/// @brief Constructor
/// @param angularAcceleration The angular acceleration, in degrees. The particle's rotation speed changes by
/// this value each second.
explicit TorqueAffector(float angularAcceleration);
virtual void affect(Particle& particle, sf::Time dt);
/// @brief sets the angular acceleration applied to the particles (in degrees).
///
void setAngularAcceleration(float angularAcceleration);
/// @brief Returns the angular acceleration applied to the particles (in degrees).
///
float getAngularAcceleration() const;
// ---------------------------------------------------------------------------------------------------------------------------
// Private variables
private:
float mAngularAcceleration;
};
/// @brief Scales particles over time.
/// @details
class THOR_API ScaleAffector : public Affector
{
// ---------------------------------------------------------------------------------------------------------------------------
// Public types
public:
/// @brief Shared pointer type referring to ScaleAffector objects
///
typedef std::shared_ptr<ScaleAffector> Ptr;
// ---------------------------------------------------------------------------------------------------------------------------
// Public static member functions
public:
/// @brief Creates an affector that continuously scales particles by a given factor.
/// @copydetails ScaleAffector::ScaleAffector()
static Ptr create(sf::Vector2f scaleFactor);
// ---------------------------------------------------------------------------------------------------------------------------
// Public member functions
public:
/// @brief Constructor
/// @param scaleFactor Factor by which particles are scaled every second.
explicit ScaleAffector(sf::Vector2f scaleFactor);
virtual void affect(Particle& particle, sf::Time dt);
/// @brief Sets the factor by which particles are scaled every second.
///
void setScaleFactor(sf::Vector2f scaleFactor);
/// @brief Returns the factor by which particles are scaled every second.
///
sf::Vector2f getScaleFactor() const;
// ---------------------------------------------------------------------------------------------------------------------------
// Private variables
private:
sf::Vector2f mScaleFactor;
};
/// @brief Fades particles in over time.
/// @details Affector class that constantly increases the alpha color value of a particle.
class THOR_API FadeInAffector : public Affector
{
// ---------------------------------------------------------------------------------------------------------------------------
// Public types
public:
/// @brief Shared pointer type referring to FadeInAffector objects
///
typedef std::shared_ptr<FadeInAffector> Ptr;
// ---------------------------------------------------------------------------------------------------------------------------
// Public static member functions
public:
/// @brief Creates an affector that increases the alpha value from the beginning of the particle lifetime.
/// @copydetails FadeInAffector::FadeInAffector()
static Ptr create(float timeRatio = 1.f);
// ---------------------------------------------------------------------------------------------------------------------------
// Public member functions
public:
/// @brief Constructor
/// @param timeRatio How long the fade-in time is in comparison to the total particle lifetime. For example,
/// 1.f means over the whole lifetime, and 0.25f means to fade in the particle only in the first quarter.
explicit FadeInAffector(float timeRatio = 1.f);
virtual void affect(Particle& particle, sf::Time dt);
/// @brief sets the time ratio (how long the fade-out time is compared to the particle lifetime).
///
void setTimeRatio(float timeRatio);
/// @brief Returns the time ratio (how long the fade-out time is compared to the particle lifetime).
///
float getTimeRatio() const;
// ---------------------------------------------------------------------------------------------------------------------------
// Private variables
private:
float mTimeRatio;
};
/// @brief Fades particles out over time.
/// @details Affector class that constantly decreases the alpha color value of a particle.
class THOR_API FadeOutAffector : public Affector
{
// ---------------------------------------------------------------------------------------------------------------------------
// Public types
public:
/// @brief Shared pointer type referring to FadeOutAffector objects
///
typedef std::shared_ptr<FadeOutAffector> Ptr;
// ---------------------------------------------------------------------------------------------------------------------------
// Public static member functions
public:
/// @brief Creates an affector that decreases the alpha value towards the end of the particle lifetime.
/// @copydetails FadeOutAffector::FadeOutAffector()
static Ptr create(float timeRatio = 1.f);
// ---------------------------------------------------------------------------------------------------------------------------
// Public member functions
public:
/// @brief Constructor
/// @param timeRatio How long the fade-out time is in comparison to the total particle lifetime. For example,
/// 1.f means over the whole lifetime, and 0.25f means to fade out the particle only in the last quarter.
explicit FadeOutAffector(float timeRatio = 1.f);
virtual void affect(Particle& particle, sf::Time dt);
/// @brief sets the time ratio (how long the fade-out time is compared to the particle lifetime).
///
void setTimeRatio(float timeRatio);
/// @brief Returns the time ratio (how long the fade-out time is compared to the particle lifetime).
///
float getTimeRatio() const;
// ---------------------------------------------------------------------------------------------------------------------------
// Private variables
private:
float mTimeRatio;
};
/// @brief Applies a color gradient to particles.
/// @details Affector class that changes the color of a particle smoothly from one to another.
class THOR_API ColorAffector : public Affector
{
// ---------------------------------------------------------------------------------------------------------------------------
// Public types
public:
/// @brief Shared pointer type referring to ColorAffector objects
///
typedef std::shared_ptr<ColorAffector> Ptr;
// ---------------------------------------------------------------------------------------------------------------------------
// Public static member functions
public:
/// @brief Creates an affector that colorizes particles according to a given gradient.
/// @copydetails ColorAffector::ColorAffector(const ColorGradient&)
static Ptr create(const ColorGradient& gradient);
// ---------------------------------------------------------------------------------------------------------------------------
// Public member functions
public:
/// @brief Constructor
/// @details Creates a color gradient over the whole lifetime of the particles.
/// @param gradient The color gradient affecting the particles. Can also be a single sf::Color.
explicit ColorAffector(const ColorGradient& gradient);
virtual void affect(Particle& particle, sf::Time dt);
/// @brief Sets the color gradient used to colorize the particles.
///
void setGradient(const ColorGradient& gradient);
/// @brief Returns the color gradient used to colorize the particles.
///
const ColorGradient& getGradient() const;
// ---------------------------------------------------------------------------------------------------------------------------
// Private variables
private:
ColorGradient mGradient;
};
/// @}
} // namespace thor
#endif // THOR_AFFECTOR_HPP