AnimationPlayer

View source

Manages animation playback state for an entity.

Registered animations retain their live asset references while their timing state is managed by the component. Creating the component does not load an asset or begin playback, and serialized state omits live references.

Retained references are released when their registrations are removed or the component is dropped.

Examples

import { World } from '@wimaengine/ecs'
import {
  AnimationClip,
  AnimationClipAssets,
  AnimationPlayer,
  PlaybackRepeat
} from '@wimaengine/animation'

const world = new World()
const clips = new AnimationClipAssets(AnimationClip)
world.setResource(clips)

// A real app normally gets this live handle from its animation asset pool
// or asset server. This local pool keeps the example self-contained.
const animationHandle = clips.add(new AnimationClip())
const entity = world.spawn([new AnimationPlayer()])
const player = world.get(entity, AnimationPlayer)

player
  .set(animationHandle, {
    duration: 1,
    repeatMode: PlaybackRepeat.Forever
  })
  .start(animationHandle)

Constructor


constructor()

Properties


animations: Map<AssetId, Playback>

View source

Maps each animation asset id to its Playback state.

Each key identifies an animation asset, and its value stores the playback settings and current state for that asset. AnimationPlayer#set adds or replaces an entry, while the playback control methods update the entry for the given id.

Default: ...

Methods


staticcopy(source: AnimationPlayer, target?: AnimationPlayer): AnimationPlayer

View source

Copies playback state, the selected id, and asset-handle references.

Each Playback value is copied into a new Playback object. Every source handle is cloned, so the returned player owns independent handle references. When target is supplied, its maps are cleared first. Its previous handles are not dropped by this method, so release those handles before reusing a populated target.

Parameters

NameDescriptionDefault
source——
target?—...

Returns

The populated target player.

staticclone(target: AnimationPlayer): AnimationPlayer

View source

Creates a player copy with cloned asset-handle references.

This delegates to AnimationPlayer.copy, so playback objects and handles are independent of the source while callback-free values such as current are copied directly.

Parameters

NameDescriptionDefault
target——

Returns

A new player containing copied state.

staticserialize(value: AnimationPlayer): AnimationPlayerSerial

View source

Converts the player to its serializable playback state.

The animations map becomes an array of [assetId, playback] entries and each playback is passed through Playback.serialize. The live handles map is omitted because handles are runtime references; the returned value contains only animations and current.

Parameters

NameDescriptionDefault
value——

Returns

Serialized player state.

staticvalidateSerial(value: unknown): value is AnimationPlayerSerial

View source

Checks the object shape accepted by AnimationPlayer.deserialize.

animations must be an array of two-item entries containing a numeric asset id and a valid Playback serial. current must be null or a number. This check does not confirm that an asset id resolves to a loaded clip, and it does not validate or restore live handles. Guard null separately when validating arbitrary input because the current implementation uses property-membership checks after its object check.

Parameters

NameDescriptionDefault
value——

Returns

Whether the value has a valid player serial shape.

staticdeserialize(value: AnimationPlayerSerial, out?: AnimationPlayer): AnimationPlayer

View source

Restores serialized playback state into a player.

A new Playback is created for each serialized entry and current is copied. Deserialization does not recreate asset handles or clear an existing out.handles map; the default output therefore has no retained handles, and an application restoring into a populated player must manage those handles explicitly. Validate external data with AnimationPlayer.validateSerial before calling this method.

Parameters

NameDescriptionDefault
value——
out?—...

Returns

The populated output player.

set(handle: Handle<AnimationClip>, settings?: PlaybackSettings): AnimationPlayer

View source

Registers or replaces playback for an animation asset.

The new Playback is created from settings with elapsed time at zero and playback unpaused. The supplied handle is stored directly and keyed by handle.id(). If another handle object is already stored for the same id, that old handle is dropped before it is replaced. This method does not load or verify the clip and does not change current.

Parameters

NameDescriptionDefault
handle——
settings?——

Returns

This player.

get(handle: Handle<AnimationClip>): Playback

View source

Returns the playback state registered for an animation asset id.

The lookup uses handle.id() and does not inspect the asset pool or the handles map, so a matching playback can still be returned after deserialization even when no live handle has been reattached.

Parameters

NameDescriptionDefault
handle——

Returns

The matching playback, when registered.

delete(handle: Handle<AnimationClip>): AnimationPlayer

View source

Removes playback for an animation asset and releases its retained handle.

If the id is not registered, this is a no-op. The current field is not changed, even when it contains the removed id.

Parameters

NameDescriptionDefault
handle——

Returns

This player.

start(handle: Handle<AnimationClip>): AnimationPlayer

View source

Resets the matching playback to time zero and starts it.

If the handle is not registered, this is a no-op. Returns this player for chaining.

Parameters

NameDescriptionDefault
handle——

Returns

This player.

stop(handle: Handle<AnimationClip>): AnimationPlayer

View source

Resets the matching playback to time zero and pauses it.

If the handle is not registered, this is a no-op. Returns this player for chaining.

Parameters

NameDescriptionDefault
handle——

Returns

This player.

play(handle: Handle<AnimationClip>): AnimationPlayer

View source

Resumes the matching playback without resetting its elapsed time.

If the handle is not registered, this is a no-op. Returns this player for chaining.

Parameters

NameDescriptionDefault
handle——

Returns

This player.

pause(handle: Handle<AnimationClip>): AnimationPlayer

View source

Pauses the matching playback at its current elapsed time.

If the handle is not registered, this is a no-op. Returns this player for chaining.

Parameters

NameDescriptionDefault
handle——

Returns

This player.