TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tbx_tweening_behavior.hpp
1 
9 #ifndef TurtleBrainsExpress_TweeningBehavior_hpp
10 #define TurtleBrainsExpress_TweeningBehavior_hpp
11 
12 #include <turtle_brains/game/tb_entity_behavior_interface.hpp>
13 #include <turtle_brains/game/tb_game_timer.hpp>
14 #include <turtle_brains/math/tb_interpolation.hpp>
15 
16 namespace TurtleBrainsExpress
17 {
18  namespace Behaviors
19  {
20  typedef tbMath::Interpolation::InterpolationMode TweenMode;
21 
26  template <typename Type> class TweeningBehavior : public tbGame::EntityBehaviorInterface
27  {
28  public:
41  TweeningBehavior(tbGame::Entity& entity, const Type& startValue, const Type& finalValue,
42  const tbGame::GameTimer& time, const TweenMode& tweenMode) :
44  mFinalValue(finalValue),
45  mStartValue(startValue),
46  mMaximumTime(time),
47  mTimer(time),
48  mTweenMode(tweenMode)
49  {
50  }
51 
55  virtual ~TweeningBehavior(void)
56  {
57  }
58 
59  protected:
67  inline void SetStartValue(const Type& startValue)
68  {
69  mStartValue = startValue;
70  }
71 
76  inline Type GetCurrentTweenValue(void) const
77  {
78  if (true == mTimer.IsZero())
79  {
80  return mFinalValue;
81  }
82 
83  const float percentage(1.0f - mTimer.GetPercentageOf(mMaximumTime));
84  return tbMath::Interpolation::Interpolate(percentage, mStartValue, mFinalValue, mTweenMode);
85  }
86 
90  inline virtual void OnRemove(void) override
91  {
92  mTimer = 0;
94  }
95 
99  inline virtual void OnSimulate(void) override
100  {
101  if (true == mTimer.DecrementStep())
102  {
104  }
105 
107  }
108 
109  private:
110  const Type mFinalValue;
111  Type mStartValue;
112  const tbGame::GameTimer mMaximumTime;
113  tbGame::GameTimer mTimer;
114  const TweenMode mTweenMode;
115  };
116 
117  }; /* namespace Behaviors */
118 }; /* namespace TurtleBrainsExpress */
119 
121 
122 #endif /* TurtleBrainsExpress_TweeningBehavior_hpp */
Definition: tb_entity.hpp:46
TweeningBehavior(tbGame::Entity &entity, const Type &startValue, const Type &finalValue, const tbGame::GameTimer &time, const TweenMode &tweenMode)
Definition: tbx_tweening_behavior.hpp:41
Entity & mEntity
Definition: tb_entity_behavior_interface.hpp:45
float GetPercentageOf(const GameTimer &timeValue) const
Definition: tb_entity_behavior_interface.hpp:24
A collection of objects and functions to express games quickly.
Type GetCurrentTweenValue(void) const
Definition: tbx_tweening_behavior.hpp:76
void SetStartValue(const Type &startValue)
Definition: tbx_tweening_behavior.hpp:67
virtual void OnSimulate(void) override
Definition: tbx_tweening_behavior.hpp:99
Definition: tbx_tweening_behavior.hpp:26
Contains high-level objects to control the entities in game worlds.
This is the heart of TurtleBrains for game developers to create GameScenes and Entities to interact w...
virtual ~TweeningBehavior(void)
Definition: tbx_tweening_behavior.hpp:55
Definition: tb_game_timer.hpp:25
virtual void OnRemove(void) override
Definition: tbx_tweening_behavior.hpp:90