TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
tbx_tweening_behavior.h
1 
9 #ifndef _TurtleBrainsExpress_TweeningBehavior_h_
10 #define _TurtleBrainsExpress_TweeningBehavior_h_
11 
12 #include "../../game/tb_entity_behavior_interface.h"
13 #include "../../math/tb_interpolation.h"
14 
15 namespace TurtleBrainsExpress
16 {
17  namespace Behaviors
18  {
19  typedef tbMath::Interpolation::InterpolationMode TweenMode;
20 
21  template <typename Type> class TweeningBehavior : public tbGame::EntityBehaviorInterface
22  {
23  public:
24  TweeningBehavior(tbGame::Entity& entity, const Type& startValue, const Type& finalValue,
25  const tbGame::GameTimer& time, const TweenMode& tweenMode) :
27  mFinalValue(finalValue),
28  mStartValue(startValue),
29  mMaximumTime(time),
30  mTimer(time),
31  mTweenMode(tweenMode)
32  {
33  }
34 
35  virtual ~TweeningBehavior(void)
36  {
37  }
38 
39  protected:
47  inline void SetStartValue(const Type& startValue) { mStartValue = startValue; }
48 
52  inline Type GetCurrentTweenValue(void) const
53  {
54  if (true == mTimer.IsZero())
55  {
56  return mFinalValue;
57  }
58 
59  const float percentage(1.0f - mTimer.GetPercentageOf(mMaximumTime));
60  return tbMath::Interpolation::Interpolate(percentage, mStartValue, mFinalValue, mTweenMode);
61  }
62 
66  inline virtual void OnSimulate(void) override
67  {
68  if (true == mTimer.DecrementStep())
69  {
71  }
72 
74  }
75 
76  private:
77  const Type mFinalValue;
78  Type mStartValue;
79  const tbGame::GameTimer mMaximumTime;
80  tbGame::GameTimer mTimer;
81  const TweenMode mTweenMode;
82  };
83 
84  }; /* namespace Behaviors */
85 }; /* namespace TurtleBrainsExpress */
86 
88 
89 #endif /* _TurtleBrainsExpress_TweeningBehavior_h_ */
Definition: tb_entity.h:46
Entity & mEntity
Definition: tb_entity_behavior_interface.h:46
float GetPercentageOf(const GameTimer &timeValue) const
Definition: tb_entity_behavior_interface.h:25
A collection of objects and functions to express games quickly.
void SetStartValue(const Type &startValue)
Definition: tbx_tweening_behavior.h:47
virtual void OnSimulate(void) override
Definition: tbx_tweening_behavior.h:66
Definition: tbx_tweening_behavior.h:21
Contains high-level objects to control the entities in game worlds.
Definition: tb_game_timer.h:25