Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
Activities.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Ref.h"
30 #include "Tween.h"
31 #include "Vector.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
44  class GF_API ValueActivity : public Activity {
45  public:
55  ValueActivity(float origin, float target, float& value, Time duration, Easing easing = Ease::linear);
56 
62  void setOrigin(float origin) {
63  m_tween.setOrigin(origin);
64  }
65 
71  float getOrigin() const noexcept {
72  return m_tween.getOrigin();
73  }
74 
80  void setTarget(float target) {
81  m_tween.setTarget(target);
82  }
83 
89  float getTarget() const noexcept {
90  return m_tween.getTarget();
91  }
92 
98  void setDuration(Time duration) {
99  m_tween.setDuration(duration);
100  }
101 
107  Time getDuration() const noexcept {
108  return m_tween.getDuration();
109  }
110 
111  virtual ActivityStatus run(Time time) override;
112  virtual void restart() override;
113 
114  private:
115  Tween<float> m_tween;
116  };
117 
118 
127  class GF_API RotateToActivity : public Activity {
128  public:
138  RotateToActivity(float origin, float target, float& angle, Time duration, Easing easing = Ease::linear);
139 
145  void setOrigin(float origin) {
146  m_tween.setOrigin(origin);
147  normalize();
148  }
149 
155  float getOrigin() const noexcept {
156  return m_tween.getOrigin();
157  }
158 
164  void setTarget(float target) {
165  m_tween.setTarget(target);
166  normalize();
167  }
168 
174  float getTarget() const noexcept {
175  return m_tween.getTarget();
176  }
177 
183  void setDuration(Time duration) {
184  m_tween.setDuration(duration);
185  }
186 
192  Time getDuration() const noexcept {
193  return m_tween.getDuration();
194  }
195 
196  virtual ActivityStatus run(Time time) override;
197  virtual void restart() override;
198 
199  private:
200  void normalize();
201 
202  private:
203  Tween<float> m_tween;
204  };
205 
206 
213  class GF_API MoveToActivity : public Activity {
214  public:
224  MoveToActivity(Vector2f origin, Vector2f target, Vector2f& position, Time duration, Easing easing = Ease::linear);
225 
231  void setOrigin(Vector2f origin) {
232  m_tween.setOrigin(origin);
233  }
234 
240  Vector2f getOrigin() const noexcept {
241  return m_tween.getOrigin();
242  }
243 
249  void setTarget(Vector2f target) {
250  m_tween.setTarget(target);
251  }
252 
258  Vector2f getTarget() const noexcept {
259  return m_tween.getTarget();
260  }
261 
267  void setDuration(Time duration) {
268  m_tween.setDuration(duration);
269  }
270 
276  Time getDuration() const noexcept {
277  return m_tween.getDuration();
278  }
279 
280  virtual ActivityStatus run(Time time) override;
281  virtual void restart() override;
282 
283  private:
284  Tween<Vector2f> m_tween;
285  };
286 
287 
294  class GF_API ColorActivity : public Activity {
295  public:
305  ColorActivity(Color4f origin, Color4f target, Color4f& color, Time duration, Easing easing = Ease::linear);
306 
312  void setOrigin(Color4f origin) {
313  m_tween.setOrigin(origin);
314  }
315 
321  Color4f getOrigin() const noexcept {
322  return m_tween.getOrigin();
323  }
324 
330  void setTarget(Color4f target) {
331  m_tween.setTarget(target);
332  }
333 
339  Color4f getTarget() const noexcept {
340  return m_tween.getTarget();
341  }
342 
348  void setDuration(Time duration) {
349  m_tween.setDuration(duration);
350  }
351 
357  Time getDuration() const noexcept {
358  return m_tween.getDuration();
359  }
360 
361  virtual ActivityStatus run(Time time) override;
362  virtual void restart() override;
363 
364  private:
365  Tween<Color4f> m_tween;
366  };
367 
368 
373  class GF_API CallbackActivity : public Activity {
374  public:
380  CallbackActivity(std::function<void()> callback);
381 
382  virtual ActivityStatus run(Time time) override;
383  virtual void restart() override;
384 
385  private:
386  std::function<void()> m_callback;
387  bool m_called;
388  };
389 
390 
395  class GF_API DelayActivity : public Activity {
396  public:
402  DelayActivity(Time duration);
403 
404  virtual ActivityStatus run(Time time) override;
405  virtual void restart() override;
406 
407  private:
408  Time m_elapsed;
409  Time m_duration;
410  };
411 
412 
417  class GF_API SequenceActivity : public Activity {
418  public:
423 
429  void addActivity(Activity& activity);
430 
434  void clear();
435 
436  virtual ActivityStatus run(Time time) override;
437  virtual void restart() override;
438 
439  private:
440  std::size_t m_current;
441  std::vector<Ref<Activity>> m_activities;
442  };
443 
444 
449  class GF_API RepeatActivity : public Activity {
450  public:
457  RepeatActivity(Activity& activity, unsigned repeat = 0);
458 
459  virtual ActivityStatus run(Time time) override;
460  virtual void restart() override;
461 
462  private:
463  Activity& m_activity;
464  unsigned m_count;
465  unsigned m_repeat;
466  };
467 
468 
476  class GF_API RepeatedSequenceActivity : public Activity {
477  public:
483  RepeatedSequenceActivity(unsigned repeat = 0);
484 
485  virtual ActivityStatus run(Time time) override;
486  virtual void restart() override;
487 
488  private:
489  SequenceActivity m_sequence;
490  RepeatActivity m_repeat;
491  };
492 
493 
498  class GF_API ParallelActivity : public Activity {
499  public:
503  enum class Finish {
504  Any,
505  All,
506  };
507 
513  ParallelActivity(Finish finish = Finish::Any);
514 
520  void addActivity(Activity& activity);
521 
525  void clear();
526 
527  virtual ActivityStatus run(Time time) override;
528  virtual void restart() override;
529 
530  private:
531  Finish m_finish;
532  ActivityStatus m_status;
533  std::vector<Ref<Activity>> m_activities;
534  };
535 
536 
537 #ifndef DOXYGEN_SHOULD_SKIP_THIS
538 }
539 #endif
540 }
541 
542 #endif // GF_ACTIVITIES_H
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:276
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:107
void setOrigin(Vector2f origin)
Change the origin of the activity.
Definition: Activities.h:231
Color4f getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:339
An activity for a change of position.
Definition: Activities.h:213
void setTarget(float target)
Change the target of the activity.
Definition: Activities.h:80
void setOrigin(float origin)
Change the origin of the activity.
Definition: Activities.h:62
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:98
constexpr AllType All
Constant to represent "all".
Definition: Types.h:61
ActivityStatus
Status of an activity.
Definition: Activity.h:38
Finish
The type of finish for the activity.
Definition: Activities.h:503
Represents a time value.
Definition: Time.h:74
void setTarget(Color4f target)
Change the target of the activity.
Definition: Activities.h:330
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:192
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:357
A repeated sequence activity.
Definition: Activities.h:476
An activity to run an activity several times.
Definition: Activities.h:449
void setOrigin(Color4f origin)
Change the origin of the activity.
Definition: Activities.h:312
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:183
An activity to wait for a predefined duration.
Definition: Activities.h:395
The namespace for gf classes.
Definition: Action.h:35
static float linear(float t)
Linear easing.
void setOrigin(float origin)
Change the origin of the activity.
Definition: Activities.h:145
float angle(Direction direction)
Get an angle from a direction.
Vector2f getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:258
An activity for a change of angle.
Definition: Activities.h:127
An activity to run several activities in parallel.
Definition: Activities.h:498
void setTarget(float target)
Change the target of the activity.
Definition: Activities.h:164
float(*)(float) Easing
An easing function.
Definition: Easings.h:48
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:267
Vector2f getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:240
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:348
An activity for a change of color.
Definition: Activities.h:294
An activity to run several activities sequentially.
Definition: Activities.h:417
An activity for a simple float value.
Definition: Activities.h:44
An activity for calling a function once.
Definition: Activities.h:373
General purpose math vector.
Definition: Vector.h:61
float getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:89
float getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:71
float getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:155
A game activity.
Definition: Activity.h:62
float getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:174
void setTarget(Vector2f target)
Change the target of the activity.
Definition: Activities.h:249
Color4f getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:321