Gamedev Framework (gf)  0.10.0
A C++14 framework for 2D games
Activities.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
43  class GF_API ValueActivity : public Activity {
44  public:
54  ValueActivity(float origin, float target, float& value, Time duration, Easing easing = Ease::linear);
55 
61  void setOrigin(float origin) {
62  m_tween.setOrigin(origin);
63  }
64 
70  float getOrigin() const noexcept {
71  return m_tween.getOrigin();
72  }
73 
79  void setTarget(float target) {
80  m_tween.setTarget(target);
81  }
82 
88  float getTarget() const noexcept {
89  return m_tween.getTarget();
90  }
91 
97  void setDuration(Time duration) {
98  m_tween.setDuration(duration);
99  }
100 
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 
126  class GF_API RotateToActivity : public Activity {
127  public:
137  RotateToActivity(float origin, float target, float& angle, Time duration, Easing easing = Ease::linear);
138 
144  void setOrigin(float origin) {
145  m_tween.setOrigin(origin);
146  normalize();
147  }
148 
154  float getOrigin() const noexcept {
155  return m_tween.getOrigin();
156  }
157 
163  void setTarget(float target) {
164  m_tween.setTarget(target);
165  normalize();
166  }
167 
173  float getTarget() const noexcept {
174  return m_tween.getTarget();
175  }
176 
182  void setDuration(Time duration) {
183  m_tween.setDuration(duration);
184  }
185 
191  Time getDuration() const noexcept {
192  return m_tween.getDuration();
193  }
194 
195  virtual ActivityStatus run(Time time) override;
196  virtual void restart() override;
197 
198  private:
199  void normalize();
200 
201  private:
202  Tween<float> m_tween;
203  };
204 
205 
212  class GF_API MoveToActivity : public Activity {
213  public:
223  MoveToActivity(Vector2f origin, Vector2f target, Vector2f& position, Time duration, Easing easing = Ease::linear);
224 
230  void setOrigin(Vector2f origin) {
231  m_tween.setOrigin(origin);
232  }
233 
239  Vector2f getOrigin() const noexcept {
240  return m_tween.getOrigin();
241  }
242 
248  void setTarget(Vector2f target) {
249  m_tween.setTarget(target);
250  }
251 
257  Vector2f getTarget() const noexcept {
258  return m_tween.getTarget();
259  }
260 
266  void setDuration(Time duration) {
267  m_tween.setDuration(duration);
268  }
269 
275  Time getDuration() const noexcept {
276  return m_tween.getDuration();
277  }
278 
279  virtual ActivityStatus run(Time time) override;
280  virtual void restart() override;
281 
282  private:
283  Tween<Vector2f> m_tween;
284  };
285 
286 
293  class GF_API ColorActivity : public Activity {
294  public:
304  ColorActivity(Color4f origin, Color4f target, Color4f& color, Time duration, Easing easing = Ease::linear);
305 
311  void setOrigin(Color4f origin) {
312  m_tween.setOrigin(origin);
313  }
314 
320  Color4f getOrigin() const noexcept {
321  return m_tween.getOrigin();
322  }
323 
329  void setTarget(Color4f target) {
330  m_tween.setTarget(target);
331  }
332 
338  Color4f getTarget() const noexcept {
339  return m_tween.getTarget();
340  }
341 
347  void setDuration(Time duration) {
348  m_tween.setDuration(duration);
349  }
350 
356  Time getDuration() const noexcept {
357  return m_tween.getDuration();
358  }
359 
360  virtual ActivityStatus run(Time time) override;
361  virtual void restart() override;
362 
363  private:
364  Tween<Color4f> m_tween;
365  };
366 
367 
372  class GF_API CallbackActivity : public Activity {
373  public:
379  CallbackActivity(std::function<void()> callback);
380 
381  virtual ActivityStatus run(Time time) override;
382  virtual void restart() override;
383 
384  private:
385  std::function<void()> m_callback;
386  bool m_called;
387  };
388 
389 
394  class GF_API DelayActivity : public Activity {
395  public:
401  DelayActivity(Time duration);
402 
403  virtual ActivityStatus run(Time time) override;
404  virtual void restart() override;
405 
406  private:
407  Time m_elapsed;
408  Time m_duration;
409  };
410 
411 
416  class GF_API SequenceActivity : public Activity {
417  public:
422 
428  void addActivity(Activity& activity);
429 
433  void clear();
434 
435  virtual ActivityStatus run(Time time) override;
436  virtual void restart() override;
437 
438  private:
439  std::size_t m_current;
440  std::vector<Activity*> m_activities;
441  };
442 
443 
448  class GF_API RepeatActivity : public Activity {
449  public:
456  RepeatActivity(Activity& activity, unsigned repeat = 0);
457 
458  virtual ActivityStatus run(Time time) override;
459  virtual void restart() override;
460 
461  private:
462  Activity& m_activity;
463  unsigned m_count;
464  unsigned m_repeat;
465  };
466 
467 
475  class GF_API RepeatedSequenceActivity : public Activity {
476  public:
482  RepeatedSequenceActivity(unsigned repeat = 0);
483 
484  virtual ActivityStatus run(Time time) override;
485  virtual void restart() override;
486 
487  private:
488  SequenceActivity m_sequence;
489  RepeatActivity m_repeat;
490  };
491 
492 
497  class GF_API ParallelActivity : public Activity {
498  public:
502  enum class Finish {
503  Any,
504  All,
505  };
506 
512  ParallelActivity(Finish finish = Finish::Any);
513 
519  void addActivity(Activity& activity);
520 
524  void clear();
525 
526  virtual ActivityStatus run(Time time) override;
527  virtual void restart() override;
528 
529  private:
530  Finish m_finish;
531  ActivityStatus m_status;
532  std::vector<Activity*> m_activities;
533  };
534 
535 
536 #ifndef DOXYGEN_SHOULD_SKIP_THIS
537 }
538 #endif
539 }
540 
541 #endif // GF_ACTIVITIES_H
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:275
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:230
Color4f getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:338
An activity for a change of position.
Definition: Activities.h:212
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 setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:97
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:502
Represents a time value.
Definition: Time.h:74
void setTarget(Color4f target)
Change the target of the activity.
Definition: Activities.h:329
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:191
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:356
A repeated sequence activity.
Definition: Activities.h:475
An activity to run an activity several times.
Definition: Activities.h:448
void setOrigin(Color4f origin)
Change the origin of the activity.
Definition: Activities.h:311
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:182
An activity to wait for a predefined duration.
Definition: Activities.h:394
The namespace for gf classes.
Definition: Action.h:34
static float linear(float t)
Linear easing.
void setOrigin(float origin)
Change the origin of the activity.
Definition: Activities.h:144
float angle(Direction direction)
Get an angle from a direction.
Vector2f getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:257
An activity for a change of angle.
Definition: Activities.h:126
An activity to run several activities in parallel.
Definition: Activities.h:497
void setTarget(float target)
Change the target of the activity.
Definition: Activities.h:163
float(*)(float) Easing
An easing function.
Definition: Easings.h:48
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:266
Vector2f getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:239
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:347
An activity for a change of color.
Definition: Activities.h:293
An activity to run several activities sequentially.
Definition: Activities.h:416
An activity for a simple float value.
Definition: Activities.h:43
An activity for calling a function once.
Definition: Activities.h:372
General purpose math vector.
Definition: Vector.h:61
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:154
A game activity.
Definition: Activity.h:62
float getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:173
void setTarget(Vector2f target)
Change the target of the activity.
Definition: Activities.h:248
Color4f getOrigin() const noexcept
Get the origin of the activity.
Definition: Activities.h:320