anim

package
v0.0.0-...-94fff03 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 13 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 anim plays sprite animations from Aseprite exports.

It has two halves. The data half is Clip and ClipSet: assets that RegisterLoaders makes loadable from an Aseprite "Sprite sheet" JSON export, one clip per tag. The runtime half is AnimationSystem, which advances gfx.Animator components and writes the current frame into gfx.Sprite.ID. The renderer never sees animation, only the current sprite.

The package makes no Ebiten calls: it reads render.Atlas values, so the sprites are already cut and cached by the render loaders.

Index

Constants

View Source
const AssetKindClip = "clip"

AssetKindClip is the asset kind of a Clip ("chars/hero.aseprite.json#run"): the name a component field carries in its +game:asset:kind=... marker.

Variables

This section is empty.

Functions

func HoldClip

func HoldClip(sc *asset.Scope, h gfx.ClipHandle)

HoldClip adds sc as an owner of h, like asset.Hold.

func HoldClipSet

func HoldClipSet(sc *asset.Scope, h ClipSetHandle)

HoldClipSet adds sc as an owner of h, like asset.Hold.

func LoadClip

func LoadClip(sc *asset.Scope, key string) gfx.ClipHandle

LoadClip returns the handle for a clip key ("chars/hero.aseprite.json#run") and adds a hold for sc. It never blocks and panics like asset.Load.

func LoadClipSync

func LoadClipSync(sc *asset.Scope, key string) (gfx.ClipHandle, error)

LoadClipSync is LoadClip followed by loading until the clip is ready or failed, like asset.LoadSync. It blocks: use it for boot screens, tests and tools.

func PivotOrigin

func PivotOrigin(sp *render.Sprite, p animfile.Point) gfx.Vec2

PivotOrigin converts a pivot p in pixels of the trimmed sprite image into the renderer's Sprite.Origin: a fraction of the untrimmed size, measured from the untrimmed top-left, which is where the trimmed image starts minus Offset. The pivot is measured from each frame's own trimmed rectangle, so it stays on the same art pixel when every frame has the same trim offset. sp must have a positive Size. The animation loader and the anim editor use this one formula.

func RegisterLoaders

func RegisterLoaders(srv *asset.Server)

RegisterLoaders registers ClipSet and Clip on srv. Call it after render.RegisterLoaders: a ClipSet depends on a render.Atlas for its sprites.

It panics with "asset: type not registered" if the render loaders are missing, and like asset.Register if the types are already registered.

func ShapesOf

func ShapesOf(clips ClipStore, an *gfx.Animator) []gfx.Box

ShapesOf returns the hit and hurt boxes of the frame an is showing (Animator.Frame of its clip), relative to the pivot. Place them in the world with gfx.Box.World and the entity's Transform and Sprite flips.

It returns nil for a zero clip handle, a clip that is not Ready, a frame out of range, and clips without boxes. The slice points into the clip: do not modify it, and do not keep it past an unload of the clip. Cache the store (ClipStoreOf) once; ShapesOf itself does not allocate.

Types

type AnimationSystem

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

AnimationSystem advances every entity that has a gfx.Animator and a gfx.Sprite: it adds the tick's delta (scaled by Animator.Speed) to Animator.Time, looks up the current frame and writes its sprite into Sprite.ID. If the frame has a pivot (Frame.Origin, from an *.anim.json file), it writes Sprite.Origin too, so a pivoted clip owns the Origin. Run it in the simulate stage, after the AssetSystem so that freshly loaded clips are Ready.

It needs the *asset.Server resource. An entity whose clip handle is zero, or whose clip is not loaded (yet), keeps its Sprite as it is, so the sprite set by the game, or the placeholder, shows until the clip arrives. An entity whose Animator.Done is set is skipped. Run does not allocate.

The delta is the game clock's scaled tick (ctx.Time.Delta), so clips slow down and stop with the time scale. Entities with the gfx.Unscaled marker advance by gfx.Time.Unscaled instead (the context's delta in a World without the gfx.Time resource).

func NewAnimationSystem

func NewAnimationSystem(w *ecs.World) *AnimationSystem

NewAnimationSystem returns the system for w.

func (*AnimationSystem) Init

func (s *AnimationSystem) Init(ctx *system.Context)

Init implements system.Initializer. It caches the Clip store, so register the loaders (RegisterLoaders) and set the *asset.Server resource first.

func (*AnimationSystem) Run

func (s *AnimationSystem) Run(ctx *system.Context)

Run implements system.Runner.

type Clip

type Clip struct {
	// Frames are the steps in play order (a reversed Aseprite tag is already
	// reversed here).
	Frames []Frame
	// Loop is the end-of-clip behavior.
	Loop LoopMode
	// Total is the sum of the frame durations: one forward pass.
	Total time.Duration
	// Body is the union of the "body" rects of the clip's *.anim.json
	// definition, relative to the pivot: the fixed body, not the per-frame
	// hurt boxes. animbody.System copies it into a collision.ShapeAnimBody
	// collider. Zero without a definition or body.
	Body gfx.IRect
}

Clip is a sequence of frames. Load one with LoadClip and a "#tag" key ("chars/hero.aseprite.json#run"). A Clip is immutable once loaded.

func (*Clip) FrameAt

func (c *Clip) FrameAt(t time.Duration) (frame int, done bool)

FrameAt returns the index of the frame shown t into the clip and whether a non-looping clip has finished. t is the playback time already scaled by speed; a negative t counts as zero. It does not allocate.

An empty clip returns (0, true).

func (*Clip) ShapesAt

func (c *Clip) ShapesAt(frame int) []gfx.Box

ShapesAt returns the boxes of frame (an index into Frames, as in Animator.Frame), or nil if frame is out of range or has none. The slice points into the clip: do not modify it. It does not allocate.

type ClipSet

type ClipSet struct {
	// Atlas is the sheet the clips' sprites come from.
	Atlas render.AtlasHandle
	// Clips maps tag name to clip. Read it at load or
	// spawn time, not per frame.
	Clips map[string]gfx.ClipHandle
	// Names lists the tag names in file order.
	Names []string
	// Default is the clip to start with, from the "defaultClip" of an
	// *.anim.json file; empty for a plain Aseprite export. It is informational:
	// spawn code picks Clips[Default].
	Default string
}

ClipSet is the set of clips of one Aseprite file, one per tag. Load it with LoadClipSet and the file's path: either the Aseprite export itself or an *.anim.json animation definition that refers to it and adds loop and timing overrides, a pivot and hit/hurt boxes. Each clip is also loadable on its own with a "#tag" key, which loads the set too; a held clip keeps the set (and so the atlas and texture) alive.

type ClipSetHandle

type ClipSetHandle struct{ asset.Ref }

ClipSetHandle refers to a ClipSet. It is 8 bytes, comparable and pointer-free; the zero value means "none". No component holds one (the Animator holds a clip), so it lives here rather than in gfx.

func LoadClipSet

func LoadClipSet(sc *asset.Scope, key string) ClipSetHandle

LoadClipSet returns the handle for a clip set key ("chars/hero.aseprite.json" or "chars/hero.anim.json") and adds a hold for sc. It never blocks and panics like asset.Load.

func LoadClipSetSync

func LoadClipSetSync(sc *asset.Scope, key string) (ClipSetHandle, error)

LoadClipSetSync is LoadClipSet followed by loading until the set is ready or failed, like asset.LoadSync. It blocks.

type ClipSetStore

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

ClipSetStore resolves ClipSetHandle values to clip sets. Get one with ClipSetStoreOf. It is a small value; copy it freely.

func ClipSetStoreOf

func ClipSetStoreOf(srv *asset.Server) ClipSetStore

ClipSetStoreOf returns the clip set store of srv. It panics like asset.StoreOf if RegisterLoaders has not run on srv.

func (ClipSetStore) Err

func (s ClipSetStore) Err(h ClipSetHandle) error

Err returns why the clip set failed to load, or nil.

func (ClipSetStore) Generic

func (s ClipSetStore) Generic() *asset.Store[ClipSet]

Generic returns the underlying generic store, for loaders and tools that work with asset.Handle[ClipSet].

func (ClipSetStore) Get

Get returns the clip set, or the placeholder (an empty set); see asset.Store.Get. It does not allocate.

func (ClipSetStore) Key

Key returns the key h was loaded from, or the zero Key for a zero or stale handle.

func (ClipSetStore) Lookup

func (s ClipSetStore) Lookup(h ClipSetHandle) (*ClipSet, asset.State)

Lookup is Get plus the load state; see asset.Store.Lookup.

func (ClipSetStore) Ready

func (s ClipSetStore) Ready(h ClipSetHandle) bool

Ready reports whether h refers to a loaded clip set.

type ClipStore

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

ClipStore resolves gfx.ClipHandle values to clips. Get one with ClipStoreOf. It is a small value; copy it freely.

func ClipStoreOf

func ClipStoreOf(srv *asset.Server) ClipStore

ClipStoreOf returns the clip store of srv. It panics like asset.StoreOf if RegisterLoaders has not run on srv.

func (ClipStore) Err

func (s ClipStore) Err(h gfx.ClipHandle) error

Err returns why the clip failed to load, or nil.

func (ClipStore) Generic

func (s ClipStore) Generic() *asset.Store[Clip]

Generic returns the underlying generic store, for loaders and tools that work with asset.Handle[Clip].

func (ClipStore) Get

func (s ClipStore) Get(h gfx.ClipHandle) *Clip

Get returns the clip, or the placeholder (an empty clip); see asset.Store.Get for the pointer validity rule. It does not allocate.

func (ClipStore) Key

func (s ClipStore) Key(h gfx.ClipHandle) asset.Key

Key returns the key h was loaded from, or the zero Key for a zero or stale handle.

func (ClipStore) Lookup

func (s ClipStore) Lookup(h gfx.ClipHandle) (*Clip, asset.State)

Lookup is Get plus the load state; see asset.Store.Lookup. Use it to tell a loaded clip from the placeholder.

func (ClipStore) Ready

func (s ClipStore) Ready(h gfx.ClipHandle) bool

Ready reports whether h refers to a loaded clip.

type Frame

type Frame struct {
	// Sprite is the image shown during the frame.
	Sprite gfx.SpriteHandle
	// Duration is how long the frame is shown. It is positive in clips that
	// come from a loader.
	Duration time.Duration
	// Events is an optional bitmask for gameplay (footstep, hitbox on, ...).
	// The Aseprite loader leaves it zero.
	Events uint32
	// Origin is the pivot written to Sprite.Origin while the frame shows, as
	// a fraction of the sprite's untrimmed size. It is computed per frame from
	// the pivot of an *.anim.json file so that the pivot stays put even when
	// trimming differs between frames. Zero means the clip has no pivot and
	// leaves Sprite.Origin alone.
	Origin gfx.Vec2
	// Boxes are the hit and hurt boxes active during the frame, relative to
	// the pivot. It is a sub-slice of one array per clip; do not modify or
	// append to it. Nil for clips without an *.anim.json file.
	Boxes []gfx.Box
}

Frame is one step of a Clip.

type LoopMode

type LoopMode uint8

LoopMode says what a Clip does when it reaches its last frame.

const (
	// Loop restarts from the first frame.
	Loop LoopMode = iota
	// Once stops on the last frame.
	Once
	// PingPong plays forward, then backward, without repeating the end
	// frames, and so on.
	PingPong
)

Loop modes.

Directories

Path Synopsis
Package animbody keeps animated colliders in step with their animation: an entity whose collision.Collider has the shape collision.ShapeAnimBody takes its Offset and Size from the Body of the clip its gfx.Animator plays.
Package animbody keeps animated colliders in step with their animation: an entity whose collision.Collider has the shape collision.ShapeAnimBody takes its Offset and Size from the Body of the clip its gfx.Animator plays.
Package animfile reads and checks the file formats package anim loads, without Ebiten, the ECS or any GPU work: the Aseprite sprite sheet export (DecodeSheet) and the *.anim.json animation definition (Decode, Encode, Resolve).
Package animfile reads and checks the file formats package anim loads, without Ebiten, the ECS or any GPU work: the Aseprite sprite sheet export (DecodeSheet) and the *.anim.json animation definition (Decode, Encode, Resolve).

Jump to

Keyboard shortcuts

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