From bfeedd3a0df5882857b3bd3a0000f256ffaca983 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 23 May 2024 14:29:01 +0800 Subject: [PATCH] The public method `Tween.union()` should not update workerTarget. (#17020) * The public method `Tween.union()` should not update workerTarget. Add more unit test for tween. * Revert --- cocos/tween/tween.ts | 16 +++--- tests/tween/tween.test.ts | 110 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 8 deletions(-) diff --git a/cocos/tween/tween.ts b/cocos/tween/tween.ts index d62d213a94..0cd1b50d23 100644 --- a/cocos/tween/tween.ts +++ b/cocos/tween/tween.ts @@ -88,7 +88,7 @@ export class Tween { * @param other @en The rear tween of this tween @zh 当前缓动的后置缓动 */ then (other: Tween): Tween { - const u = other._union(); + const u = other._union(true); if (u) this._actions.push(u); return this; } @@ -177,7 +177,7 @@ export class Tween { * @param target @en The target of clone tween @zh 克隆缓动的目标对象 */ clone (target: U): Tween { - const action = this._union(); + const action = this._union(false); const r = tween(target); return action ? r.insertAction(action) : r; } @@ -189,7 +189,7 @@ export class Tween { * 将之前所有的 action 整合为一个 action。 */ union (): Tween { - const action = this._union(); + const action = this._union(false); this._actions.length = 0; if (action) this._actions.push(action); return this; @@ -330,7 +330,7 @@ export class Tween { let action: FiniteTimeAction | undefined | null; if (embedTween instanceof Tween) { - action = embedTween._union(); + action = embedTween._union(false); } else { action = actions.pop(); } @@ -353,7 +353,7 @@ export class Tween { let action: FiniteTimeAction | undefined | null; if (embedTween instanceof Tween) { - action = embedTween._union(); + action = embedTween._union(false); } else { action = actions.pop(); } @@ -380,7 +380,7 @@ export class Tween { let action: Action | undefined | null; if (embedTween instanceof Tween) { - action = embedTween._union(); + action = embedTween._union(false); } else { action = actions.pop(); } @@ -481,7 +481,7 @@ export class Tween { TweenSystem.instance.ActionManager.removeAllActionsFromTarget(target); } - private _union (updateWorkerTarget: boolean = true): FiniteTimeAction | null { + private _union (updateWorkerTarget: boolean): FiniteTimeAction | null { const actions = this._actions; if (!actions) return null; let action: FiniteTimeAction; @@ -504,7 +504,7 @@ export class Tween { tmp_args.length = 0; for (let l = args.length, i = 0; i < l; i++) { const arg = args[i]; - const action = arg._union(); + const action = arg._union(true); if (action) tmp_args.push(action); } } diff --git a/tests/tween/tween.test.ts b/tests/tween/tween.test.ts index ba7763823d..9c968710da 100644 --- a/tests/tween/tween.test.ts +++ b/tests/tween/tween.test.ts @@ -676,6 +676,116 @@ test('Test different target in nest sequence 1', function() { director.unregisterSystem(sys); }); +test('Test different target and union', function() { + const sys = new TweenSystem(); + (TweenSystem.instance as any) = sys; + director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM); + + // test begin + const rootTarget = { position: new Vec3() }; + const uiTransformTarget = { contentSize: new Size() }; + + tween(rootTarget) + .parallel( + tween(rootTarget).by(1, { position: new Vec3(100, 100, 100) }, { + onStart(target) { + expect(target === rootTarget).toBeTruthy(); + }, + }), + tween(uiTransformTarget).by(1, { contentSize: new Size(100, 100) }, { + onStart(target) { + expect(target === uiTransformTarget).toBeTruthy(); + }, + }), + ) + .union() + .start(); + + // The first step is from 0, so we need to add one more frame to make the action run to 1/3 time. + for (let i = 0; i < 21; ++i) { + game.step(); + } + expect(new Vec3(1.0/3.0*100, 1.0/3.0*100, 1.0/3.0*100).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(1.0/3.0*100, 1.0/3.0*100))).toBeTruthy(); + + // 2/3 time + for (let i = 0; i < 20; ++i) { + game.step(); + } + + expect(new Vec3(2.0/3.0*100, 2.0/3.0*100, 2.0/3.0*100).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(2.0/3.0*100, 2.0/3.0*100))).toBeTruthy(); + + // complete position tween + for (let i = 0; i < 20; ++i) { + game.step(); + } + + expect(new Vec3(100, 100, 100).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(100, 100))).toBeTruthy(); + + // test end + director.unregisterSystem(sys); +}); + +test('Test different target, union, repeat with embed tween', function() { + const sys = new TweenSystem(); + (TweenSystem.instance as any) = sys; + director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM); + + // test begin + const rootTarget = { position: new Vec3() }; + const uiTransformTarget = { contentSize: new Size() }; + + const t = tween(rootTarget).parallel( + tween(rootTarget).by(1, { position: new Vec3(100, 100, 100) }, { + onStart(target) { + expect(target === rootTarget).toBeTruthy(); + }, + }), + tween(uiTransformTarget).by(1, { contentSize: new Size(100, 100) }, { + onStart(target) { + expect(target === uiTransformTarget).toBeTruthy(); + }, + }), + ).union(); + + tween(rootTarget).repeat(2, t).start(); + + // The first step is from 0, so we need to add one more frame to make the action run to 1/3 time. + for (let i = 0; i < 21; ++i) { + game.step(); + } + expect(new Vec3(1.0/3.0*100, 1.0/3.0*100, 1.0/3.0*100).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(1.0/3.0*100, 1.0/3.0*100))).toBeTruthy(); + + // 2/3 time + for (let i = 0; i < 20; ++i) { + game.step(); + } + + expect(new Vec3(2.0/3.0*100, 2.0/3.0*100, 2.0/3.0*100).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(2.0/3.0*100, 2.0/3.0*100))).toBeTruthy(); + + // complete position tween + for (let i = 0; i < 20; ++i) { + game.step(); + } + + expect(new Vec3(100, 100, 100).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(100, 100))).toBeTruthy(); + + // 2s + for (let i = 0; i < 60; ++i) { + game.step(); + } + expect(new Vec3(200, 200, 200).equals(rootTarget.position)).toBeTruthy(); + expect(isSizeEqualTo(uiTransformTarget.contentSize, new Size(200, 200))).toBeTruthy(); + + // test end + director.unregisterSystem(sys); +}); + test('Test different target in nest sequence 2', function() { // @ts-expect-error director.root!._batcher = new Batcher2D(director.root!);