PVG
9. Animation System — Program Video Games

9. Animation System

The Fractal Code Cycle Applied to Animations

Recall our fundamental pattern:

Input → Processing → Output

For sprite animations:

  • Input: Delta time, animation state, animation definition
  • Processing: Figure out next frame (if any)
  • Output: Update the animation state

What Are Sprite Animations Good For?

For a game like this, mostly character animations.

However, sprite animations have been used in many games for background effects, or even UI.

Design Considerations

Imagine a game with several playable characters, all sprite-based. You may assume they each have the same walk animation, just with different textures.

With that in mind, the idea of coupling textures and animations may not be the best.

In fact, the animation doesn't need to know anything about the sprite texture. It only needs the data of how many frames there are and what the timings between frames is.

The Big Idea

Animation systems are time-based state machines. At the core, an animation is a timer counting down and some way to tell which frame plays based on that.

The data required for a definition is not the same as the data of a playing animation. The definition doesn't need a timer, it needs to know what the frames are. Conversely, to draw the frame, the animation instance doesn't need to know how many frames there are, only what the current frame is. Updating does require the definition data, hence why we copy it.

Animations are rarely standalone. They are usually triggered by other systems. A unit attacks, they play an attack animation. Pick up an item? Play the pickup item animation, etc.

Input drives movement, movement drives animation state changes, animation state determines visual output. This is the Fractal Code Cycle in action.