Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(animation): exit-time transitions for Animator (T2)

yhhf0b8135febde
diff --git a/sdk/src/animation/Animator.ts b/sdk/src/animation/Animator.ts
index 85e83fcc3..7faa559ba 100644
--- a/sdk/src/animation/Animator.ts
+++ b/sdk/src/animation/Animator.ts
@@ -49,6 +49,12 @@ export interface AnimatorTransition {
to: string;
/** All conditions must hold (AND). Empty = unconditional. */
conditions: AnimatorCondition[];
+ /**
+ * Only fire once the current state's clip has finished (a non-looping sprite
+ * clip reached its end). Combined with `conditions`, both must hold. With no
+ * conditions, this is "auto-advance when the clip ends" (e.g. attack→idle).
+ */
+ hasExitTime?: boolean;
}
/** One stop on a 1D blend: at/above `value`, play `clip`. */
@@ -119,13 +125,16 @@ function conditionHolds(
}
}
-/** First transition in `list` whose conditions all hold, with the triggers it used. */
+/** First transition in `list` that is ready (conditions hold; exit time met if
+ * required), with the triggers it used. */
function firstReady(
list: readonly AnimatorTransition[],
params: AnimatorParamValues,
triggers: ReadonlySet<string>,
+ clipFinished: boolean,
): { to: string; usedTriggers: string[] } | null {
for (const t of list) {
+ if (t.hasExitTime && !clipFinished) continue;
const used: string[] = [];
let ok = true;
for (const c of t.conditions) {
@@ -139,7 +148,9 @@ function firstReady(
/**
* Evaluate one step of the state machine. Any-state transitions are checked
- * before the current state's. Returns the next state (or null to stay) and the
+ * before the current state's. `clipFinished` gates `hasExitTime` transitions
+ * (true = the current clip has ended); it defaults true so transitions without
+ * exit time are unaffected. Returns the next state (or null to stay) and the
* triggers a fired transition consumed. Pure.
*/
export function evaluateAnimatorTransitions(
@@ -147,10 +158,11 @@ export function evaluateAnimatorTransitions(
currentState: string,
params: AnimatorParamValues,
triggers: ReadonlySet<string>,
+ clipFinished: boolean = true,
): AnimatorEvalResult {
- const fromAny = firstReady(def.anyStateTransitions ?? [], params, triggers);
+ const fromAny = firstReady(def.anyStateTransitions ?? [], params, triggers, clipFinished);
const current = def.states.find((s) => s.name === currentState);
- const fired = fromAny ?? firstReady(current?.transitions ?? [], params, triggers);
+ const fired = fromAny ?? firstReady(current?.transitions ?? [], params, triggers, clipFinished);
return fired
? { next: fired.to, consumedTriggers: fired.usedTriggers }
: { next: null, consumedTriggers: [] };
@@ -298,8 +310,21 @@ export class AnimatorControllerApi {
const params = resolveParams(def, this.params.get(entity) ?? EMPTY_PARAMS);
const triggerSet = this.triggers.get(entity);
+ // The current clip has finished when its SpriteAnimator stopped (a
+ // non-looping clip reached its end). Gates hasExitTime transitions.
+ const spNow = world.has(entity, SpriteAnimator)
+ ? (world.get(entity, SpriteAnimator) as SpriteAnimatorData)
+ : null;
+ // "Finished" means the SpriteAnimator is playing *this* state's clip
+ // and has stopped — not merely stopped (which is also true before the
+ // clip is ever applied, e.g. the frame a state is entered).
+ const fromDef = def.states.find((s) => s.name === fromState);
+ const expectedClip = fromDef ? this.motionClipOf(fromDef, params).clip : '';
+ const clipFinished = spNow
+ ? (expectedClip !== '' && spNow.clip === expectedClip && !spNow.playing)
+ : true;
const { next, consumedTriggers } = evaluateAnimatorTransitions(
- def, fromState, params, triggerSet ?? EMPTY_TRIGGERS,
+ def, fromState, params, triggerSet ?? EMPTY_TRIGGERS, clipFinished,
);
if (triggerSet) for (const t of consumedTriggers) triggerSet.delete(t);
@@ -329,10 +354,7 @@ export class AnimatorControllerApi {
const st = def.states.find((s) => s.name === stateName);
if (!st || !world.has(entity, SpriteAnimator)) return;
- const sel: AnimatorBlendThreshold = st.blend
- ? selectBlendClip(st.blend, Number(params[st.blend.parameter] ?? 0))
- : { value: 0, clip: st.clip ?? '', speed: st.speed, loop: st.loop };
-
+ const sel = this.motionClipOf(st, params);
const sp = world.get(entity, SpriteAnimator) as SpriteAnimatorData;
if (sp.clip === sel.clip && !forceRestart) return;
@@ -345,6 +367,14 @@ export class AnimatorControllerApi {
sp.enabled = true;
world.insert(entity, SpriteAnimator, sp);
}
+
+ /** The clip (and its speed/loop) a state would play for the given params:
+ * the single clip, or the 1D-blend selection. */
+ private motionClipOf(st: AnimatorState, params: AnimatorParamValues): AnimatorBlendThreshold {
+ return st.blend
+ ? selectBlendClip(st.blend, Number(params[st.blend.parameter] ?? 0))
+ : { value: 0, clip: st.clip ?? '', speed: st.speed, loop: st.loop };
+ }
}
const EMPTY_PARAMS: ReadonlyMap<string, number | boolean> = new Map();
diff --git a/sdk/tests/animator.test.ts b/sdk/tests/animator.test.ts
index 8f404e642..fff6520ca 100644
--- a/sdk/tests/animator.test.ts
+++ b/sdk/tests/animator.test.ts
@@ -77,6 +77,23 @@ describe('evaluateAnimatorTransitions', () => {
expect(r.consumedTriggers).toEqual(['jump']);
});
+ it('gates hasExitTime transitions on clipFinished', () => {
+ const d: AnimatorControllerDef = {
+ parameters: [],
+ initialState: 'attack',
+ states: [
+ { name: 'attack', clip: 'attack', transitions: [
+ { to: 'idle', conditions: [], hasExitTime: true },
+ ] },
+ { name: 'idle', clip: 'idle', transitions: [] },
+ ],
+ };
+ // clip still playing → does not fire
+ expect(evaluateAnimatorTransitions(d, 'attack', {}, new Set(), false).next).toBeNull();
+ // clip finished → auto-advances
+ expect(evaluateAnimatorTransitions(d, 'attack', {}, new Set(), true).next).toBe('idle');
+ });
+
it('eq / neq comparisons', () => {
const d: AnimatorControllerDef = {
parameters: [{ name: 'phase', type: 'float', default: 0 }],
@@ -231,6 +248,39 @@ describe('AnimatorControllerApi.update', () => {
expect(ctrl.getFloat(E, 'speed')).toBe(0);
});
+ it('auto-advances on exit time when the current clip finishes', () => {
+ const ctrl = new AnimatorControllerApi();
+ ctrl.registerController('atk', {
+ parameters: [],
+ initialState: 'attack',
+ states: [
+ { name: 'attack', clip: 'attack', loop: false, transitions: [
+ { to: 'idle', conditions: [], hasExitTime: true },
+ ] },
+ { name: 'idle', clip: 'idle', transitions: [] },
+ ],
+ });
+ const world = makeWorld();
+ world.insert(E, Animator, { controller: 'atk', currentState: '', enabled: true } as AnimatorData);
+ world.insert(E, SpriteAnimator, spriteData());
+
+ ctrl.update(world); // → attack, applyMotion sets playing = true
+ expect((world.get(E, Animator) as AnimatorData).currentState).toBe('attack');
+
+ // Still playing → stays in attack.
+ ctrl.update(world);
+ expect((world.get(E, Animator) as AnimatorData).currentState).toBe('attack');
+
+ // Simulate the non-looping clip ending (SpriteAnimationApi clears playing).
+ const sp = world.get(E, SpriteAnimator) as SpriteAnimatorData;
+ sp.playing = false;
+ world.insert(E, SpriteAnimator, sp);
+
+ ctrl.update(world); // exit time met → idle
+ expect((world.get(E, Animator) as AnimatorData).currentState).toBe('idle');
+ expect((world.get(E, SpriteAnimator) as SpriteAnimatorData).clip).toBe('idle');
+ });
+
it('a 1D-blend state switches clip within the state as the parameter crosses thresholds', () => {
const ctrl = new AnimatorControllerApi();
ctrl.registerController('loco', {