Skip to main content

Animation

The engine animation system is based on spritesheet clips and one animator per entity. It is intentionally simple, but it covers the common cases needed by a small 2D game: idle states, walking loops, one-shot effects, and basic clip switching.

AnimationClip

AnimationClip describes how to read frames from a spritesheet.

FieldDefaultUse
FrameSize(0, 0)size of each frame
FrameCount0total number of frames
FramesPerRow0horizontal layout
FPS12.0fplayback speed
Looptruewhether it loops
StartFrame0base frame inside the spritesheet

Behavior

  • if FramesPerRow is 0, the system uses 1
  • if FrameCount <= 0, the clip does not advance
  • if FPS <= 0, the clip stays on the current frame

Example

AnimationClip run(iVec2(32, 32), 6, 3, 10.0f, true, 0);

Animator

Animator stores the playback state.

FieldDefaultUse
Clipsemptyclip map by name
CurrentClipemptyactive clip
Playingtruewhether it advances
CurrentFrame0current frame
Accumulator0.0ftime accumulated toward the next frame

AnimationProxy

Entity::Animation() returns a proxy for managing the animator.

MethodUse
AddClip(name, clip)registers or replaces a clip
HasClip(name)checks existence
RemoveClip(name)removes a clip
Play(name)plays a clip from the start
Play()resumes the current clip
Pause()freezes the current frame
Stop()stops and rewinds
Restart()restarts the active clip
IsPlaying()checks playback state
GetCurrentClip()active clip name
GetFrame() / SetFrame()current frame
GetFPS() / SetFPS()speed
IsLooping() / SetLoop()looping
GetFrameSize() / SetFrameSize()frame size
GetFrameCount() / SetFrameCount()number of frames
GetFramesPerRow() / SetFramesPerRow()sheet layout

Key Behavior

  • Play(name) does nothing if the clip does not exist
  • RemoveClip(name) clears the active clip if it was removed
  • SetFrame() resets the time accumulator

Example

player.Animation().AddClip("idle", AnimationClip(iVec2(32, 32), 4, 4, 0.0f, true));
player.Animation().AddClip("walk", AnimationClip(iVec2(32, 32), 6, 3, 10.0f, true));
player.Animation().Play("walk");

System Integration

AnimationSystem automatically updates entities that have:

  • Animator
  • SpriteRenderer

When the frame advances, it writes the sprite SourceRect.

important

Public animation does not move the entity. It only changes the sprite frame selection. Use Transform or Rigidbody to move an actor.