List of all members | Public Types | Public Member Functions
thor::Animator< Animated, Id > Class Template Reference

Class to animate objects. More...

Public Types

typedef std::function< void(Animated &, float)> AnimationFunction
 Functor to animate the objects. More...
 

Public Member Functions

 Animator (const AnimationMap< Animated, Id > &animations)
 Construct from animation map. More...
 
ImplementationDefined play ()
 Plays one or more animations, discarding the current queue. More...
 
ImplementationDefined queue ()
 Plays one or more animations, appending to current queue. More...
 
void stop ()
 Stops all playing and queued animations. More...
 
void update (sf::Time dt)
 Updates the animator's progress. You should call this method each frame. More...
 
void animate (Animated &animated) const
 Applies the stored animations to an object. More...
 

Detailed Description

template<class Animated, typename Id>
class thor::Animator< Animated, Id >

Class to animate objects.

The Animator class takes care of multiple animations which are registered with an ID. The animations can be played at any time, queued for subsequent animations, and repeated or looped as desired. Animator updates the animations' progress and applies them to animated objects. It thus relieves the user of keeping track how far an animation has progressed, when it is finished, and so on.

Each Animator always references an AnimationMap instance, which serves as the storage of animations. The Animator::update() function must be called in every frame in order to update the animation progress.

This class does not provide any methods to query the state and progress of the animations, by design. On one hand, there is not always an unambiguous animation playing – there may be none, or there may be multiple (when the delta time passed to update() has exceeded one). On the other hand, the intent of animators is to abstract such information from the user, and attempts to retrieve it nonetheless indicate a misunderstanding of Animator's capabilities (e.g. the need to know when an animation has finished, in order to start another). Sequential actions can be queued using play() and queue(); it's even possible to notify the user through callbacks.

Member Typedef Documentation

template<class Animated , typename Id >
typedef std::function<void(Animated&, float)> thor::Animator< Animated, Id >::AnimationFunction

Functor to animate the objects.

Signature: void (Animated& animated, float progress)

  • animated is the object being animated.
  • progress is a number in [0,1] determining the animation state.

Constructor & Destructor Documentation

template<class Animated , typename Id >
thor::Animator< Animated, Id >::Animator ( const AnimationMap< Animated, Id > &  animations)
explicit

Construct from animation map.

Parameters
animationsAnimation map that stores the animation used by this animator. It must stay alive as long as this animator uses it.

Member Function Documentation

template<class Animated , typename Id >
void thor::Animator< Animated, Id >::animate ( Animated &  animated) const

Applies the stored animations to an object.

If no animation is active, the animated object is left unchanged. If multiple animations have been finished during the last update() call, each of them is applied in order. The last time an animation is applied, its progress is guaranteed to be 1.

Parameters
animatedObject which is animated by the current animation.
template<class Animated , typename Id >
ImplementationDefined thor::Animator< Animated, Id >::play ( )

Plays one or more animations, discarding the current queue.

This function clears the current playing queue and creates a new one. The behavior of play() is equivalent to stop() followed by queue().

Usage:

animator.play() << "id1" << thor::Playback::repeat("id2", 3);
Returns
Implementation-defined queue object that can be appended to with operator<<. Do not store this object.
template<class Animated, typename Id>
ImplementationDefined thor::Animator< Animated, Id >::queue ( )

Plays one or more animations, appending to current queue.

This function appends to the end of the current playing queue. Make sure there is no looped animation except at the end.

Usage:

animator.queue() << "id1" << thor::Playback::repeat("id2", 3);
Returns
Implementation-defined queue object that can be appended to with operator<<. Do not store this object.
template<class Animated, typename Id>
void thor::Animator< Animated, Id >::stop ( )

Stops all playing and queued animations.

Animations currently playing are interrupted, and the queue of pending animations is cleared. After this call, this object effectively behaves like a newly constructed Animator instance.

template<class Animated, typename Id>
void thor::Animator< Animated, Id >::update ( sf::Time  dt)

Updates the animator's progress. You should call this method each frame.

Progresses the current animation by dt. If its duration is exceeded, the next animation in the queue is loaded. This method guarantees that every animation in the queue is updated at least once, and that the last update occurs with a progress of 1. Therefore, you can design your animations in a way that leaves the animated object in a meaningful state at the end of the animation.

If no animation is currently playing or queued, nothing happens.

Parameters
dtFrame time.

The documentation for this class was generated from the following files: