Click or drag to resize
DigitalRuneAnimation Manager

The AnimationManager is the core of the animation system. It manages, updates and applies all running animations. A game usually has one instance of this class which is updated in the main game loop.

Initialization

Creation and initialization of the animation manager is straight forward.

C#
// Create a new animation manager.
AnimationManager animationManager = new AnimationManager();

Optionally, it is possible to enable or disable support for multithreading. When multithreading is enabled all animations are evaluated in parallel. The workload is automatically distributed among all available CPU cores. Multithreading is enabled by default. See property AnimationManagerEnableMultithreading for more information.

C#
// Optional: Enable or disable multithreading support. (Enabled by default.)
animationManager.EnableMultithreading = true;

Support for multithreading can be enabled or disabled at any point at runtime.

Update

All active animations are registered by the animation manager. The method AnimationManagerUpdate(TimeSpan) needs to be called regularly, usually once per frame, to update all animations.

C#
// Progress all animations by the given time.
// (deltaTime is the elapsed time since the last update.)
animationManager.Update(deltaTime);

The AnimationManagerUpdate(TimeSpan) method is the part were the animation manager does most of its work.

Important: The Update method does not yet write the animation results to the objects that are being animated. Instead the new animation values are cached by the animations system. The method AnimationManagerApplyAnimations needs to be called to write the animation results.

C#
// Apply the new animation values to the objects and properties that
// are being animated.
animationManager.ApplyAnimations();

By separating the Update and the ApplyAnimations logic it is possible to run the update parallel to other game services (such as AI, graphics, physics, etc.).

It is not allowed to start or stop animations while they are being updated.

Animation service

The AnimationManager implements the interface IAnimationService. This interface exposes all methods required to start and stop animations. All game components that need to start or stop animations need to have access to this interface.

One solution to make the service globally available is to store the animation manager in a global variable. A more elegant solution is to put the interface into a service provider ("service locator") which is globally available. The following is an example of a stripped-down XNA Game class.

C#
public class Game1 : Game
{
  private AnimationManager _animationManager;

  protected override void Initialize()
  {
    // Best practice: The services are stored in Game.Services to make them 
    // accessible for all game components.

    // Add the animation service.
    _animationManager = new AnimationManager();
    Services.AddService(typeof(IAnimationService), _animationManager);

    ...

    base.Initialize();
  }


  protected override void Update(GameTime gameTime)
  {
    TimeSpan deltaTime = gameTime.ElapsedGameTime;

    // Game logic.
    ...

    // Update the game components.
    base.Update(gameTime);

    // Update the animations.
    _animationManager.Update(deltaTime);

    // Apply the animations.
    _animationManager.ApplyAnimations();
  }
}
Note Note

In the example above the animations are updated after the game logic and all game components have been updated. Note that this is just one possible example! It is also possible to update the animations at a different point of the game loop.

Also, the method ApplyAnimations does not necessarily need to be called immediately after Update.

Tip: In the example above the interface IAnimationService is registered as a service in Game.Services. Other game components can retrieve the service from the service provider if necessary. Here is an example:

C#
// Fetch the animation service from the game's service provider.
var animationService = (IAnimationService)Game.Services.GetService(typeof(IAnimationService));