Class
AnimationPlayer
View sourceManages 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
Properties
animations: Map<AssetId, Playback>
View sourceMaps 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: ...
handles: Map<AssetId, Handle<AnimationClip>>
View sourceLive animation asset handles keyed by the same ids as animations.
The player keeps the exact handles passed to AnimationPlayer#set.
The map is cleared when an animation entry is removed and by
dropAnimationPlayer, and each stored handle is released before
it is removed. AnimationPlayer.serialize and
AnimationPlayer.deserialize do not include or recreate this map.
Default: ...
current: AssetId
View sourceOptional application-selected animation AssetId.
The value starts as null and is not changed when playback entries are
added, removed, or controlled. AnimationPlayer.copy preserves
it, and AnimationPlayer.serialize and
AnimationPlayer.deserialize include it in the serialized
player state.
Default: null
Methods
staticcopy(source: AnimationPlayer, target?: AnimationPlayer): AnimationPlayer
View sourceCopies 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.
staticclone(target: AnimationPlayer): AnimationPlayer
View sourceCreates 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.
staticdefault(): AnimationPlayer
View sourcestaticserialize(value: AnimationPlayer): AnimationPlayerSerial
View sourceConverts 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.
staticvalidateSerial(value: unknown): value is AnimationPlayerSerial
View sourceChecks 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.
staticdeserialize(value: AnimationPlayerSerial, out?: AnimationPlayer): AnimationPlayer
View sourceRestores 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.
set(handle: Handle<AnimationClip>, settings?: PlaybackSettings): AnimationPlayer
View sourceRegisters 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.
get(handle: Handle<AnimationClip>): Playback
View sourceReturns 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.
delete(handle: Handle<AnimationClip>): AnimationPlayer
View sourceRemoves 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.
start(handle: Handle<AnimationClip>): AnimationPlayer
View sourceResets 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.
stop(handle: Handle<AnimationClip>): AnimationPlayer
View sourceResets 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.
play(handle: Handle<AnimationClip>): AnimationPlayer
View sourceResumes the matching playback without resetting its elapsed time.
If the handle is not registered, this is a no-op. Returns this player for chaining.
pause(handle: Handle<AnimationClip>): AnimationPlayer
View sourcePauses the matching playback at its current elapsed time.
If the handle is not registered, this is a no-op. Returns this player for chaining.
startAll(): AnimationPlayer
View sourceResets and starts every registered playback.
stopAll(): AnimationPlayer
View sourceResets and pauses every registered playback.
playAll(): AnimationPlayer
View sourceResumes every registered playback without resetting elapsed time.
pauseAll(): AnimationPlayer
View sourcePauses every registered playback at its current elapsed time.
