Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Activities.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_ACTIVITIES_H
22 #define GF_ACTIVITIES_H
23 
24 #include <functional>
25 #include <vector>
26 
27 #include "Activity.h"
28 #include "Portability.h"
29 #include "Tween.h"
30 #include "Vector.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  /**
38  * @ingroup game
39  * @brief An activity for a simple float value
40  *
41  * @sa gf::Tween
42  */
43  class GF_API ValueActivity : public Activity {
44  public:
45  /**
46  * @brief Constructor
47  *
48  * @param origin The origin value
49  * @param target The target value
50  * @param value A reference on the value
51  * @param duration The duration of the tween
52  * @param easing The easing for the interpolation
53  */
54  ValueActivity(float origin, float target, float& value, Time duration, Easing easing = Ease::linear);
55 
56  /**
57  * @brief Change the origin of the activity
58  *
59  * @param origin The new origin
60  */
61  void setOrigin(float origin) {
62  m_tween.setOrigin(origin);
63  }
64 
65  /**
66  * @brief Get the origin of the activity
67  *
68  * @returns The current origin
69  */
70  float getOrigin() const noexcept {
71  return m_tween.getOrigin();
72  }
73 
74  /**
75  * @brief Change the target of the activity
76  *
77  * @param target The new target
78  */
79  void setTarget(float target) {
80  m_tween.setTarget(target);
81  }
82 
83  /**
84  * @brief Get the target of the activity
85  *
86  * @returns The current target
87  */
88  float getTarget() const noexcept {
89  return m_tween.getTarget();
90  }
91 
92  /**
93  * @brief Change the duration of the activity
94  *
95  * @param duration The new duration
96  */
97  void setDuration(Time duration) {
98  m_tween.setDuration(duration);
99  }
100 
101  /**
102  * @brief Get the duration of the activity
103  *
104  * @returns The current duration
105  */
106  Time getDuration() const noexcept {
107  return m_tween.getDuration();
108  }
109 
110  virtual ActivityStatus run(Time time) override;
111  virtual void restart() override;
112 
113  private:
114  Tween<float> m_tween;
115  };
116 
117 
118  /**
119  * @ingroup game
120  * @brief An activity for a change of angle
121  *
122  * The activity ensures that the change is no more that @f$ \pi @f$ radians.
123  *
124  * @sa gf::Tween
125  */
127  public:
128  /**
129  * @brief Constructor
130  *
131  * @param origin The origin value
132  * @param target The target value
133  * @param angle A reference on the value
134  * @param duration The duration of the tween
135  * @param easing The easing for the interpolation
136  */
137  RotateToActivity(float origin, float target, float& angle, Time duration, Easing easing = Ease::linear);
138 
139  /**
140  * @brief Change the origin of the activity
141  *
142  * @param origin The new origin
143  */
144  void setOrigin(float origin) {
145  m_tween.setOrigin(origin);
146  }
147 
148  /**
149  * @brief Get the origin of the activity
150  *
151  * @returns The current origin
152  */
153  float getOrigin() const noexcept {
154  return m_tween.getOrigin();
155  }
156 
157  /**
158  * @brief Change the target of the activity
159  *
160  * @param target The new target
161  */
162  void setTarget(float target) {
163  m_tween.setTarget(target);
164  }
165 
166  /**
167  * @brief Get the target of the activity
168  *
169  * @returns The current target
170  */
171  float getTarget() const noexcept {
172  return m_tween.getTarget();
173  }
174 
175  /**
176  * @brief Change the duration of the activity
177  *
178  * @param duration The new duration
179  */
180  void setDuration(Time duration) {
181  m_tween.setDuration(duration);
182  }
183 
184  /**
185  * @brief Get the duration of the activity
186  *
187  * @returns The current duration
188  */
189  Time getDuration() const noexcept {
190  return m_tween.getDuration();
191  }
192 
193  virtual ActivityStatus run(Time time) override;
194  virtual void restart() override;
195 
196  private:
197  Tween<float> m_tween;
198  };
199 
200 
201  /**
202  * @ingroup game
203  * @brief An activity for a change of position
204  *
205  * @sa gf::Tween
206  */
207  class GF_API MoveToActivity : public Activity {
208  public:
209  /**
210  * @brief Constructor
211  *
212  * @param origin The origin value
213  * @param target The target value
214  * @param position A reference on the value
215  * @param duration The duration of the tween
216  * @param easing The easing for the interpolation
217  */
218  MoveToActivity(Vector2f origin, Vector2f target, Vector2f& position, Time duration, Easing easing = Ease::linear);
219 
220  /**
221  * @brief Change the origin of the activity
222  *
223  * @param origin The new origin
224  */
225  void setOrigin(Vector2f origin) {
226  m_tween.setOrigin(origin);
227  }
228 
229  /**
230  * @brief Get the origin of the activity
231  *
232  * @returns The current origin
233  */
234  Vector2f getOrigin() const noexcept {
235  return m_tween.getOrigin();
236  }
237 
238  /**
239  * @brief Change the target of the activity
240  *
241  * @param target The new target
242  */
243  void setTarget(Vector2f target) {
244  m_tween.setTarget(target);
245  }
246 
247  /**
248  * @brief Get the target of the activity
249  *
250  * @returns The current target
251  */
252  Vector2f getTarget() const noexcept {
253  return m_tween.getTarget();
254  }
255 
256  /**
257  * @brief Change the duration of the activity
258  *
259  * @param duration The new duration
260  */
261  void setDuration(Time duration) {
262  m_tween.setDuration(duration);
263  }
264 
265  /**
266  * @brief Get the duration of the activity
267  *
268  * @returns The current duration
269  */
270  Time getDuration() const noexcept {
271  return m_tween.getDuration();
272  }
273 
274  virtual ActivityStatus run(Time time) override;
275  virtual void restart() override;
276 
277  private:
278  Tween<Vector2f> m_tween;
279  };
280 
281 
282  /**
283  * @ingroup game
284  * @brief An activity for a change of color
285  *
286  * @sa gf::Tween
287  */
288  class GF_API ColorActivity : public Activity {
289  public:
290  /**
291  * @brief Constructor
292  *
293  * @param origin The origin value
294  * @param target The target value
295  * @param color A reference on the value
296  * @param duration The duration of the tween
297  * @param easing The easing for the interpolation
298  */
299  ColorActivity(Color4f origin, Color4f target, Color4f& color, Time duration, Easing easing = Ease::linear);
300 
301  /**
302  * @brief Change the origin of the activity
303  *
304  * @param origin The new origin
305  */
306  void setOrigin(Color4f origin) {
307  m_tween.setOrigin(origin);
308  }
309 
310  /**
311  * @brief Get the origin of the activity
312  *
313  * @returns The current origin
314  */
315  Color4f getOrigin() const noexcept {
316  return m_tween.getOrigin();
317  }
318 
319  /**
320  * @brief Change the target of the activity
321  *
322  * @param target The new target
323  */
324  void setTarget(Color4f target) {
325  m_tween.setTarget(target);
326  }
327 
328  /**
329  * @brief Get the target of the activity
330  *
331  * @returns The current target
332  */
333  Color4f getTarget() const noexcept {
334  return m_tween.getTarget();
335  }
336 
337  /**
338  * @brief Change the duration of the activity
339  *
340  * @param duration The new duration
341  */
342  void setDuration(Time duration) {
343  m_tween.setDuration(duration);
344  }
345 
346  /**
347  * @brief Get the duration of the activity
348  *
349  * @returns The current duration
350  */
351  Time getDuration() const noexcept {
352  return m_tween.getDuration();
353  }
354 
355  virtual ActivityStatus run(Time time) override;
356  virtual void restart() override;
357 
358  private:
359  Tween<Color4f> m_tween;
360  };
361 
362 
363  /**
364  * @ingroup game
365  * @brief An activity for calling a function once
366  */
368  public:
369  /**
370  * @brief Constructor
371  *
372  * @param callback The function to call
373  */
374  CallbackActivity(std::function<void()> callback);
375 
376  virtual ActivityStatus run(Time time) override;
377  virtual void restart() override;
378 
379  private:
380  std::function<void()> m_callback;
381  bool m_called;
382  };
383 
384 
385  /**
386  * @ingroup game
387  * @brief An activity to wait for a predefined duration
388  */
389  class GF_API DelayActivity : public Activity {
390  public:
391  /**
392  * @brief Constructor
393  *
394  * @param duration The duration to wait for
395  */
396  DelayActivity(Time duration);
397 
398  virtual ActivityStatus run(Time time) override;
399  virtual void restart() override;
400 
401  private:
402  Time m_elapsed;
403  Time m_duration;
404  };
405 
406 
407  /**
408  * @ingroup game
409  * @brief An activity to run several activities sequentially
410  */
412  public:
413  /**
414  * @brief Constructor
415  */
417 
418  /**
419  * @brief Add an activity to the sequence
420  *
421  * @param activity The activity
422  */
423  void addActivity(Activity& activity);
424 
425  virtual ActivityStatus run(Time time) override;
426  virtual void restart() override;
427 
428  private:
429  std::size_t m_current;
430  std::vector<Activity*> m_activities;
431  };
432 
433 
434  /**
435  * @ingroup game
436  * @brief An activity to run an activity several times
437  */
438  class GF_API RepeatActivity : public Activity {
439  public:
440  /**
441  * @brief Constructor
442  *
443  * @param activity The activity to run serveral times
444  * @param repeat The number of time to repeat the activity or 0 for infinite
445  */
446  RepeatActivity(Activity& activity, unsigned repeat = 0);
447 
448  virtual ActivityStatus run(Time time) override;
449  virtual void restart() override;
450 
451  private:
452  Activity& m_activity;
453  unsigned m_count;
454  unsigned m_repeat;
455  };
456 
457  /**
458  * @ingroup game
459  * @brief An activity to run several activities in parallel
460  */
462  public:
463  /**
464  * @brief The type of finish for the activity
465  */
466  enum class Finish {
467  Any, ///< If any of the activities ends
468  All, ///< If all of the activities ends
469  };
470 
471  /**
472  * @brief Constructor
473  *
474  * @param finish The type of finish
475  */
477 
478  /**
479  * @brief Add an activity to the set
480  *
481  * @param activity The activity
482  */
483  void addActivity(Activity& activity);
484 
485  virtual ActivityStatus run(Time time) override;
486  virtual void restart() override;
487 
488  private:
489  Finish m_finish;
490  ActivityStatus m_status;
491  std::vector<Activity*> m_activities;
492  };
493 
494 
495 #ifndef DOXYGEN_SHOULD_SKIP_THIS
496 }
497 #endif
498 }
499 
500 #endif // GF_ACTIVITIES_H
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:270
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:106
void setOrigin(Vector2f origin)
Change the origin of the activity.
Definition: Activities.h:225
virtual void restart() override
Restart the activity.
virtual ActivityStatus run(Time time) override
Run the activity.
void addActivity(Activity &activity)
Add an activity to the sequence.
virtual void restart() override
Restart the activity.
Color4f getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:333
An activity for a change of position.
Definition: Activities.h:207
void setTarget(float target)
Change the target of the activity.
Definition: Activities.h:79
void setOrigin(float origin)
Change the origin of the activity.
Definition: Activities.h:61
void addActivity(Activity &activity)
Add an activity to the set.
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:97
DelayActivity(Time duration)
Constructor.
ParallelActivity(Finish finish=Finish::Any)
Constructor.
virtual ActivityStatus run(Time time) override
Run the activity.
ActivityStatus
Status of an activity.
Definition: Activity.h:38
Finish
The type of finish for the activity.
Definition: Activities.h:466
Represents a time value.
Definition: Time.h:73
void setTarget(Color4f target)
Change the target of the activity.
Definition: Activities.h:324
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:189
ColorActivity(Color4f origin, Color4f target, Color4f &color, Time duration, Easing easing=Ease::linear)
Constructor.
SequenceActivity()
Constructor.
If all of the activities ends.
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:351
RepeatActivity(Activity &activity, unsigned repeat=0)
Constructor.
An activity to run an activity several times.
Definition: Activities.h:438
virtual ActivityStatus run(Time time) override
Run the activity.
void setOrigin(Color4f origin)
Change the origin of the activity.
Definition: Activities.h:306
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:180
An activity to wait for a predefined duration.
Definition: Activities.h:389
virtual ActivityStatus run(Time time) override
Run the activity.
virtual void restart() override
Restart the activity.
The namespace for gf classes.
Definition: Action.h:34
static float linear(float t)
Linear easing.
virtual ActivityStatus run(Time time) override
Run the activity.
virtual ActivityStatus run(Time time) override
Run the activity.
void setOrigin(float origin)
Change the origin of the activity.
Definition: Activities.h:144
Vector2f getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:252
An activity for a change of angle.
Definition: Activities.h:126
An activity to run several activities in parallel.
Definition: Activities.h:461
void setTarget(float target)
Change the target of the activity.
Definition: Activities.h:162
Predefined easing functions.
Definition: Easings.h:239
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:261
virtual ActivityStatus run(Time time) override
Run the activity.
MoveToActivity(Vector2f origin, Vector2f target, Vector2f &position, Time duration, Easing easing=Ease::linear)
Constructor.
virtual void restart() override
Restart the activity.
Vector2f getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:234
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:342
An activity for a change of color.
Definition: Activities.h:288
An activity to run several activities sequentially.
Definition: Activities.h:411
ValueActivity(float origin, float target, float &value, Time duration, Easing easing=Ease::linear)
Constructor.
virtual void restart() override
Restart the activity.
An activity for a simple float value.
Definition: Activities.h:43
virtual void restart() override
Restart the activity.
virtual ActivityStatus run(Time time) override
Run the activity.
An activity for calling a function once.
Definition: Activities.h:367
An interpolation between two values.
Definition: Tween.h:40
#define GF_API
Definition: Portability.h:35
If any of the activities ends.
CallbackActivity(std::function< void()> callback)
Constructor.
RotateToActivity(float origin, float target, float &angle, Time duration, Easing easing=Ease::linear)
Constructor.
float getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:88
float getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:70
float getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:153
A game activity.
Definition: Activity.h:62
virtual ActivityStatus run(Time time) override
Run the activity.
virtual void restart() override
Restart the activity.
float getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:171
virtual void restart() override
Restart the activity.
virtual void restart() override
Restart the activity.
void setTarget(Vector2f target)
Change the target of the activity.
Definition: Activities.h:243
Color4f getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:315