animation

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package animation provides state-based sprite animation and playback signals.

Index

Constants

View Source
const SystemName = "animation"

SystemName is the registered name of Animate.

Variables

This section is empty.

Functions

func Animate added in v0.4.0

func Animate(ctx *core.Context) error

Animate advances Animator components and emits playback signals.

Games created by github.com/Leonard-Atorough/castrum.New register it in core.PhaseFixed. When an animator exists, the world must provide Sets and signal.Hub resources.

Types

type Animator added in v0.4.0

type Animator struct {
	// Set is the key of the animation set.
	Set string `json:"set"`
	// State is the desired state name.
	State string `json:"state"`
	// Speed is the playback multiplier; zero means normal speed.
	Speed float64 `json:"speed,omitempty"`
	// Paused controls whether playback is paused.
	Paused bool `json:"paused,omitempty"`
	// Style controls the appearance of the animation.
	Style sprite.Style `json:"style,omitempty"`
	// contains filtered or unexported fields
}

Animator is a per-entity animation component with serializable desired state.

func NewAnimator added in v0.4.0

func NewAnimator(set, state string) (Animator, error)

NewAnimator returns an Animator with the given set and desired state. It returns an error if either name is empty.

func (Animator) Current added in v0.4.0

func (a Animator) Current() string

Current returns the state actually playing, which may differ from State. It returns an empty string before playback starts.

func (Animator) Finished added in v0.4.0

func (a Animator) Finished() bool

Finished reports whether a LoopOnce state has completed and is holding its final frame. It remains true until Animate starts another state.

func (Animator) Frames added in v0.4.0

func (a Animator) Frames(sets Sets) ([]WeightedFrame, error)

Frames returns the current state's weighted layers for drawing. It returns nil without error before playback starts.

During a blend, outgoing layers precede incoming layers. It returns an error if the Animator is invalid or a required set or state is missing, or if the blend easing name is unknown.

func (Animator) Validate added in v0.4.0

func (a Animator) Validate() error

Validate returns an error if the desired state or runtime fields are invalid. It does not check whether the set or state exists in Sets.

type Completed added in v0.3.0

type Completed struct {
	// Entity is the entity whose primary track reached a boundary.
	Entity core.Entity
	// Tick is the fixed tick of the completion.
	Tick uint64
	// State is the name of the state whose primary track reached a boundary.
	State string
	// Loops is the number of forward wraps for [Loop], or 1 for other modes.
	Loops uint64
}

Completed is emitted by Animate when a state's primary track reaches a boundary. It emits one signal per animator per tick, even if a looping state wraps multiple times in one tick.

type LoopMode

type LoopMode int

LoopMode controls what happens after an animation reaches its final frame.

const (
	// LoopOnce plays the state once and holds its final frame if there is no
	// automatic transition. It is the zero value.
	LoopOnce LoopMode = iota
	// Loop wraps from the final frame back to the first.
	Loop
	// LoopPingPong plays the state forward, then backward, repeatedly.
	LoopPingPong
)

type Set added in v0.4.0

type Set struct {
	// Name is the set's name.
	Name string `json:"name"`
	// States maps state names to states.
	States map[string]State `json:"states"`
}

Set groups named animation states.

func NewSet added in v0.4.0

func NewSet() Set

NewSet returns a set with an initialized, empty state map. The set is invalid until it has a name and at least one state.

func (Set) Validate added in v0.4.0

func (s Set) Validate() error

Validate returns an error if the set, its states, or their transitions are invalid.

type Sets added in v0.4.0

type Sets struct {
	// contains filtered or unexported fields
}

Sets is a named collection of animation sets, intended as a world resource.

func NewSets added in v0.4.0

func NewSets(sets map[string]Set) (Sets, error)

NewSets returns a validated Sets containing deep copies of the input sets. It returns an error if the collection is empty or any set or key is invalid.

func (Sets) Get added in v0.4.0

func (ss Sets) Get(name string) (Set, bool)

Get returns the named set and whether it exists. The returned set's maps and slices share storage with the collection.

func (Sets) MarshalJSON added in v0.4.0

func (ss Sets) MarshalJSON() ([]byte, error)

MarshalJSON encodes the collection as a JSON object keyed by set name.

func (Sets) Names added in v0.4.0

func (ss Sets) Names() []string

Names returns the set names in unspecified order.

func (*Sets) UnmarshalJSON added in v0.4.0

func (ss *Sets) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON object into the collection without validating it.

func (Sets) Validate added in v0.4.0

func (ss Sets) Validate() error

Validate returns an error if the collection is empty or contains an invalid set or a set whose name does not match its key.

type Started added in v0.4.0

type Started struct {
	// Entity is the entity whose state started.
	Entity core.Entity
	// Tick is the fixed tick of the start.
	Tick uint64
	// State is the name of the state that started.
	State string
}

Started is emitted by Animate when an Animator begins playing a state.

type State added in v0.4.0

type State struct {
	// Name is the state's name.
	Name string `json:"name"`
	// Tracks are the layers played in the state.
	Tracks []Track `json:"tracks"`
	// Loop controls whether the state repeats.
	Loop LoopMode `json:"loop,omitempty"`
	// EnterBlend is the duration in seconds of the transition into the state.
	EnterBlend float64 `json:"enterBlend,omitempty"`
	// BlendEase names the easing function for the enter transition. Empty
	// defaults to linear; unknown names are rejected by [State.Validate].
	BlendEase string `json:"blendEase,omitempty"`
	// Next is the state to enter when the primary track reaches a boundary.
	// An empty name disables the automatic transition.
	Next string `json:"next,omitempty"`
}

State defines an animation state and its tracks.

func (State) Validate added in v0.4.0

func (e State) Validate() error

Validate returns an error if the state or its tracks are invalid.

type Track added in v0.4.0

type Track struct {
	// Name is the track's name.
	Name string `json:"name"`
	// Source is the sheet document ID.
	Source asset.ID `json:"source"`
	// Frames are the sheet region names in playback order.
	Frames []string `json:"frames"`
	// FPS is the playback rate in frames per second.
	FPS float64 `json:"fps"`
}

Track is one layer of a State: a named sequence of sheet regions at a fixed rate. Tracks in one state play together.

func (Track) SheetRef added in v0.4.0

func (t Track) SheetRef() asset.SheetRef

SheetRef returns the track's sheet as a file reference.

func (Track) Validate added in v0.4.0

func (t Track) Validate() error

Validate returns an error if the track has an invalid name, source, frames, or playback rate. FPS must be finite, positive, and at most 60.

type WeightedFrame added in v0.4.0

type WeightedFrame struct {
	// Track is the name of the track.
	Track string
	// Source is the sheet containing the region.
	Source asset.SheetRef
	// Region is the name of the sheet region.
	Region string
	// Weight is the layer's blend weight.
	Weight float64
}

WeightedFrame is one drawable layer: a track's current region and its blend weight.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL