Gamedev Framework (gf)  0.7.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  }
147 
153  float getOrigin() const noexcept {
154  return m_tween.getOrigin();
155  }
156 
162  void setTarget(float target) {
163  m_tween.setTarget(target);
164  }
165 
171  float getTarget() const noexcept {
172  return m_tween.getTarget();
173  }
174 
180  void setDuration(Time duration) {
181  m_tween.setDuration(duration);
182  }
183 
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 
207  class GF_API MoveToActivity : public Activity {
208  public:
218  MoveToActivity(Vector2f origin, Vector2f target, Vector2f& position, Time duration, Easing easing = Ease::linear);
219 
225  void setOrigin(Vector2f origin) {
226  m_tween.setOrigin(origin);
227  }
228 
234  Vector2f getOrigin() const noexcept {
235  return m_tween.getOrigin();
236  }
237 
243  void setTarget(Vector2f target) {
244  m_tween.setTarget(target);
245  }
246 
252  Vector2f getTarget() const noexcept {
253  return m_tween.getTarget();
254  }
255 
261  void setDuration(Time duration) {
262  m_tween.setDuration(duration);
263  }
264 
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 
288  class GF_API ColorActivity : public Activity {
289  public:
299  ColorActivity(Color4f origin, Color4f target, Color4f& color, Time duration, Easing easing = Ease::linear);
300 
306  void setOrigin(Color4f origin) {
307  m_tween.setOrigin(origin);
308  }
309 
315  Color4f getOrigin() const noexcept {
316  return m_tween.getOrigin();
317  }
318 
324  void setTarget(Color4f target) {
325  m_tween.setTarget(target);
326  }
327 
333  Color4f getTarget() const noexcept {
334  return m_tween.getTarget();
335  }
336 
342  void setDuration(Time duration) {
343  m_tween.setDuration(duration);
344  }
345 
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 
367  class GF_API CallbackActivity : public Activity {
368  public:
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 
389  class GF_API DelayActivity : public Activity {
390  public:
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 
411  class GF_API SequenceActivity : public Activity {
412  public:
417 
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 
438  class GF_API RepeatActivity : public Activity {
439  public:
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 
461  class GF_API ParallelActivity : public Activity {
462  public:
466  enum class Finish {
467  Any,
468  All,
469  };
470 
476  ParallelActivity(Finish finish = Finish::Any);
477 
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
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 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: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
Time getDuration() const noexcept
Get the duration of the activity.
Definition: Activities.h:351
An activity to run an activity several times.
Definition: Activities.h:438
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
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: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
float(*)(float) Easing
An easing function.
Definition: Easings.h:48
void setDuration(Time duration)
Change the duration of the activity.
Definition: Activities.h:261
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
An activity for a simple float value.
Definition: Activities.h:43
An activity for calling a function once.
Definition: Activities.h:367
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:153
A game activity.
Definition: Activity.h:62
float getTarget() const noexcept
Get the target of the activity.
Definition: Activities.h:171
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