From f6f9843ed55ce1f4b2edd195d5f9e125a047a783 Mon Sep 17 00:00:00 2001 From: zxx43 Date: Fri, 1 Sep 2023 14:36:56 +0800 Subject: [PATCH 1/2] fix tween.sequence --- cocos/tween/actions/action-interval.ts | 37 ++++++++++++++++++-------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/cocos/tween/actions/action-interval.ts b/cocos/tween/actions/action-interval.ts index c1a6b1fe93d..767cee3f4da 100644 --- a/cocos/tween/actions/action-interval.ts +++ b/cocos/tween/actions/action-interval.ts @@ -51,6 +51,7 @@ export class ActionInterval extends FiniteTimeAction { protected MAX_VALUE = 2; protected _elapsed = 0; protected _firstTick = false; + // eslint-disable-next-line @typescript-eslint/ban-types protected _easeList: Function[] = []; protected _speed = 1; protected _repeatForever = false; @@ -59,7 +60,7 @@ export class ActionInterval extends FiniteTimeAction { constructor (d?: number) { super(); - if (d !== undefined && !isNaN(d)) { + if (d !== undefined && !Number.isNaN(d)) { this.initWithDuration(d); } } @@ -128,6 +129,7 @@ export class ActionInterval extends FiniteTimeAction { easing (easeObj: any): ActionInterval { if (this._easeList) this._easeList.length = 0; else this._easeList = []; + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument for (let i = 0; i < arguments.length; i++) this._easeList.push(arguments[i]); return this; } @@ -256,7 +258,7 @@ export class ActionInterval extends FiniteTimeAction { */ repeat (times: number): ActionInterval { times = Math.round(times); - if (isNaN(times) || times < 1) { + if (Number.isNaN(times) || times < 1) { logID(1014); return this; } @@ -289,7 +291,7 @@ export class Sequence extends ActionInterval { const sequence = new Sequence(); sequence.initWithTwoActions(actionOne, actionTwo); return sequence; - } + }; private _actions: ActionInterval[] = []; private _split = 0; @@ -323,6 +325,7 @@ export class Sequence extends ActionInterval { for (let i = 1; i < last; i++) { if (paramArray[i]) { action1 = prev; + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument prev = Sequence._actionOneTwo(action1, paramArray[i]); } } @@ -346,6 +349,7 @@ export class Sequence extends ActionInterval { durationOne *= actionOne._repeatMethod ? actionOne._timesForRepeat : 1; durationTwo *= actionTwo._repeatMethod ? actionTwo._timesForRepeat : 1; const d = durationOne + durationTwo; + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument this.initWithDuration(d); this._actions[0] = actionOne; @@ -355,6 +359,7 @@ export class Sequence extends ActionInterval { clone (): any { const action = new Sequence(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument this._cloneDecoration(action as any); action.initWithTwoActions(this._actions[0].clone(), this._actions[1].clone()); return action as any; @@ -411,6 +416,7 @@ export class Sequence extends ActionInterval { } } + // eslint-disable-next-line prefer-const actionFound = locActions[found]; // Last action found and it is done. if (locLast === found && actionFound.isDone()) return; @@ -454,8 +460,9 @@ export class Sequence extends ActionInterval { export function sequence (/* Multiple Arguments */tempArray: any): ActionInterval { const paramArray = (tempArray instanceof Array) ? tempArray : arguments; if (paramArray.length === 1) { - errorID(1019); - return null as any; + // errorID(1019); + // return null as any; + return paramArray[0] as ActionInterval; } const last = paramArray.length - 1; if ((last >= 0) && (paramArray[last] == null)) logID(1015); @@ -465,11 +472,13 @@ export function sequence (/* Multiple Arguments */tempArray: any): ActionInterva result = paramArray[0]; for (let i = 1; i <= last; i++) { if (paramArray[i]) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument result = Sequence._actionOneTwo(result, paramArray[i]); } } } + // eslint-disable-next-line @typescript-eslint/no-unsafe-return return result; } @@ -493,6 +502,7 @@ export class Repeat extends ActionInterval { constructor (action?: any, times?: any) { super(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument times !== undefined && this.initWithAction(action, times); } @@ -666,7 +676,7 @@ export class RepeatForever extends ActionInterval { step (dt: any): void { const locInnerAction = this._innerAction!; - locInnerAction.step(dt); + locInnerAction.step(dt as number); if (locInnerAction.isDone()) { // var diff = locInnerAction.getElapsed() - locInnerAction._duration; locInnerAction.startWithTarget(this.target); @@ -731,7 +741,7 @@ export class Spawn extends ActionInterval { const pSpawn = new Spawn(); pSpawn.initWithTwoActions(action1, action2); return pSpawn; - } + }; private _one: ActionInterval | null = null; private _two: ActionInterval | null = null; @@ -775,13 +785,15 @@ export class Spawn extends ActionInterval { const d1 = action1._duration; const d2 = action2._duration; - if (this.initWithDuration(Math.max(d1, d2))) { + if (this.initWithDuration(Math.max(d1 as number, d2 as number))) { this._one = action1; this._two = action2; if (d1 > d2) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument this._two = Sequence._actionOneTwo(action2, delayTime(d1 - d2)); } else if (d1 < d2) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument this._one = Sequence._actionOneTwo(action1, delayTime(d2 - d1)); } @@ -811,8 +823,8 @@ export class Spawn extends ActionInterval { update (dt: any): void { dt = this._computeEaseTime(dt); - if (this._one) this._one.update(dt); - if (this._two) this._two.update(dt); + if (this._one) this._one.update(dt as number); + if (this._two) this._two.update(dt as number); } reverse (): any { @@ -839,6 +851,7 @@ export function spawn (/* Multiple Arguments */tempArray: any): FiniteTimeAction const paramArray = (tempArray instanceof Array) ? tempArray : arguments; if (paramArray.length === 1) { errorID(1020); + // eslint-disable-next-line @typescript-eslint/no-unsafe-return return null as any; } if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null)) logID(1015); @@ -847,7 +860,7 @@ export function spawn (/* Multiple Arguments */tempArray: any): FiniteTimeAction for (let i = 1; i < paramArray.length; i++) { if (paramArray[i] != null) prev = Spawn._actionOneTwo(prev, paramArray[i]); } - return prev; + return prev as FiniteTimeAction; } /* Delays the action a certain amount of seconds @@ -855,6 +868,7 @@ export function spawn (/* Multiple Arguments */tempArray: any): FiniteTimeAction * @extends ActionInterval */ class DelayTime extends ActionInterval { + // eslint-disable-next-line @typescript-eslint/no-empty-function update (dt: any): void { } reverse (): any { @@ -905,6 +919,7 @@ export class ReverseTime extends ActionInterval { constructor (action?: any) { super(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument action && this.initWithAction(action); } From 8b9488107119439d1adcdc908f65b48f75bb666c Mon Sep 17 00:00:00 2001 From: zxx43 Date: Fri, 15 Sep 2023 15:41:05 +0800 Subject: [PATCH 2/2] add sequence unit test --- cocos/tween/actions/action-interval.ts | 27 +++++++++----------------- tests/tween/tween.test.ts | 25 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/cocos/tween/actions/action-interval.ts b/cocos/tween/actions/action-interval.ts index 767cee3f4da..87d04694422 100644 --- a/cocos/tween/actions/action-interval.ts +++ b/cocos/tween/actions/action-interval.ts @@ -325,8 +325,7 @@ export class Sequence extends ActionInterval { for (let i = 1; i < last; i++) { if (paramArray[i]) { action1 = prev; - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - prev = Sequence._actionOneTwo(action1, paramArray[i]); + prev = Sequence._actionOneTwo(action1 as ActionInterval, paramArray[i] as ActionInterval); } } this.initWithTwoActions(prev, paramArray[last]); @@ -349,8 +348,7 @@ export class Sequence extends ActionInterval { durationOne *= actionOne._repeatMethod ? actionOne._timesForRepeat : 1; durationTwo *= actionTwo._repeatMethod ? actionTwo._timesForRepeat : 1; const d = durationOne + durationTwo; - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - this.initWithDuration(d); + this.initWithDuration(d as number); this._actions[0] = actionOne; this._actions[1] = actionTwo; @@ -359,8 +357,7 @@ export class Sequence extends ActionInterval { clone (): any { const action = new Sequence(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - this._cloneDecoration(action as any); + this._cloneDecoration(action as ActionInterval); action.initWithTwoActions(this._actions[0].clone(), this._actions[1].clone()); return action as any; } @@ -472,14 +469,12 @@ export function sequence (/* Multiple Arguments */tempArray: any): ActionInterva result = paramArray[0]; for (let i = 1; i <= last; i++) { if (paramArray[i]) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - result = Sequence._actionOneTwo(result, paramArray[i]); + result = Sequence._actionOneTwo(result as ActionInterval, paramArray[i] as ActionInterval); } } } - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return result; + return result as ActionInterval; } /* @@ -502,8 +497,7 @@ export class Repeat extends ActionInterval { constructor (action?: any, times?: any) { super(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - times !== undefined && this.initWithAction(action, times); + times !== undefined && this.initWithAction(action as FiniteTimeAction, times as number); } /* @@ -790,11 +784,9 @@ export class Spawn extends ActionInterval { this._two = action2; if (d1 > d2) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - this._two = Sequence._actionOneTwo(action2, delayTime(d1 - d2)); + this._two = Sequence._actionOneTwo(action2 as ActionInterval, delayTime(d1 - d2)); } else if (d1 < d2) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - this._one = Sequence._actionOneTwo(action1, delayTime(d2 - d1)); + this._one = Sequence._actionOneTwo(action1 as ActionInterval, delayTime(d2 - d1)); } ret = true; @@ -919,8 +911,7 @@ export class ReverseTime extends ActionInterval { constructor (action?: any) { super(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - action && this.initWithAction(action); + action && this.initWithAction(action as ActionInterval); } /* diff --git a/tests/tween/tween.test.ts b/tests/tween/tween.test.ts index 32e90d0048a..07b9453f8d4 100644 --- a/tests/tween/tween.test.ts +++ b/tests/tween/tween.test.ts @@ -37,5 +37,30 @@ test('destroySelf', function () { tween(node).destroySelf().start(); game.step(); expect(onDestroy).toBeCalledTimes(1); + director.unregisterSystem(sys); +}); + +test('sequence', function () { + const sys = new TweenSystem(); + (TweenSystem.instance as any) = sys; + director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM); + + const node = new Node(); + const target = new Vec3(10, 20, 30); + const tweenact = tween(node).to(1, {position: target}, { easing: "bounceOut" }); + tween(node).sequence(tweenact).start(); + + for (let i = 0; i < 100; ++i) { + game.step(); + } + // @ts-expect-error access private property + const action = tweenact._actions[0] as TweenAction; + // @ts-expect-error access private property + const props = action._props; + for (const property in props) { + const prop = props[property]; + expect(Vec3.equals(prop.current, target)).toBeTruthy(); + } + director.unregisterSystem(sys); }); \ No newline at end of file