Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tween.sequence not supporting single parameter #16443

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions cocos/tween/actions/action-interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;
Expand All @@ -59,7 +60,7 @@

constructor (d?: number) {
super();
if (d !== undefined && !isNaN(d)) {
if (d !== undefined && !Number.isNaN(d)) {
this.initWithDuration(d);
}
}
Expand Down Expand Up @@ -128,6 +129,7 @@
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;
}
Expand Down Expand Up @@ -256,7 +258,7 @@
*/
repeat (times: number): ActionInterval {
times = Math.round(times);
if (isNaN(times) || times < 1) {
if (Number.isNaN(times) || times < 1) {
logID(1014);
return this;
}
Expand Down Expand Up @@ -285,11 +287,11 @@
* Runs actions sequentially, one after another.
*/
export class Sequence extends ActionInterval {
static _actionOneTwo = function (actionOne: ActionInterval, actionTwo: ActionInterval): Sequence {

Check warning on line 290 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed static method '_actionOneTwo'
const sequence = new Sequence();
sequence.initWithTwoActions(actionOne, actionTwo);
return sequence;
}

Check failure on line 294 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing semicolon

private _actions: ActionInterval[] = [];
private _split = 0;
Expand Down Expand Up @@ -323,7 +325,7 @@
for (let i = 1; i < last; i++) {
if (paramArray[i]) {
action1 = prev;
prev = Sequence._actionOneTwo(action1, paramArray[i]);
prev = Sequence._actionOneTwo(action1 as ActionInterval, paramArray[i] as ActionInterval);
}
}
this.initWithTwoActions(prev, paramArray[last]);
Expand All @@ -346,7 +348,7 @@
durationOne *= actionOne._repeatMethod ? actionOne._timesForRepeat : 1;
durationTwo *= actionTwo._repeatMethod ? actionTwo._timesForRepeat : 1;
const d = durationOne + durationTwo;
this.initWithDuration(d);
this.initWithDuration(d as number);

this._actions[0] = actionOne;
this._actions[1] = actionTwo;
Expand All @@ -355,7 +357,7 @@

clone (): any {
const action = new Sequence();
this._cloneDecoration(action as any);
this._cloneDecoration(action as ActionInterval);
action.initWithTwoActions(this._actions[0].clone(), this._actions[1].clone());
return action as any;
}
Expand Down Expand Up @@ -411,6 +413,7 @@
}
}

// eslint-disable-next-line prefer-const
actionFound = locActions[found];
// Last action found and it is done.
if (locLast === found && actionFound.isDone()) return;
Expand Down Expand Up @@ -451,23 +454,24 @@
* const seq = sequence(actArray);
*/
// todo: It should be use new
export function sequence (/* Multiple Arguments */tempArray: any): ActionInterval {
export function sequence (/* Multiple Arguments */tempArray: any): ActionInterval | null {
minggo marked this conversation as resolved.
Show resolved Hide resolved
const paramArray = (tempArray instanceof Array) ? tempArray : arguments;
if (paramArray.length === 1) {
errorID(1019);
return null as any;
return paramArray[0] as ActionInterval;
}
const last = paramArray.length - 1;
if ((last >= 0) && (paramArray[last] == null)) logID(1015);

let result: any = null;
let result: ActionInterval | null = null;
if (last >= 0) {
result = paramArray[0];
result = paramArray[0] as ActionInterval;
for (let i = 1; i <= last; i++) {
if (paramArray[i]) {
result = Sequence._actionOneTwo(result, paramArray[i]);
result = Sequence._actionOneTwo(result, paramArray[i] as ActionInterval);
}
}
} else if(result == null) {

Check warning on line 473 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected space(s) after "if"
logID(1015);
}

return result;
Expand All @@ -493,7 +497,7 @@

constructor (action?: any, times?: any) {
super();
times !== undefined && this.initWithAction(action, times);
times !== undefined && this.initWithAction(action as FiniteTimeAction, times as number);

Check warning on line 500 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected an assignment or function call and instead saw an expression
}

/*
Expand Down Expand Up @@ -635,7 +639,7 @@

constructor (action?: ActionInterval) {
super();
action && this.initWithAction(action);

Check warning on line 642 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected an assignment or function call and instead saw an expression
}

/*
Expand Down Expand Up @@ -666,7 +670,7 @@

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);
Expand Down Expand Up @@ -727,11 +731,11 @@
* @extends ActionInterval
*/
export class Spawn extends ActionInterval {
static _actionOneTwo = function (action1: any, action2: any): Spawn {

Check warning on line 734 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed static method '_actionOneTwo'
const pSpawn = new Spawn();
pSpawn.initWithTwoActions(action1, action2);
return pSpawn;
}
};

private _one: ActionInterval | null = null;
private _two: ActionInterval | null = null;
Expand Down Expand Up @@ -775,14 +779,14 @@
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) {
this._two = Sequence._actionOneTwo(action2, delayTime(d1 - d2));
this._two = Sequence._actionOneTwo(action2 as ActionInterval, delayTime(d1 - d2));
} else if (d1 < d2) {
this._one = Sequence._actionOneTwo(action1, delayTime(d2 - d1));
this._one = Sequence._actionOneTwo(action1 as ActionInterval, delayTime(d2 - d1));
}

ret = true;
Expand Down Expand Up @@ -811,8 +815,8 @@

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 {
Expand All @@ -839,6 +843,7 @@
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);
Expand All @@ -847,14 +852,15 @@
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
* @class DelayTime
* @extends ActionInterval
*/
class DelayTime extends ActionInterval {
// eslint-disable-next-line @typescript-eslint/no-empty-function
update (dt: any): void { }

reverse (): any {
Expand Down Expand Up @@ -905,7 +911,7 @@

constructor (action?: any) {
super();
action && this.initWithAction(action);
action && this.initWithAction(action as ActionInterval);

Check warning on line 914 in cocos/tween/actions/action-interval.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected an assignment or function call and instead saw an expression
}

/*
Expand Down
25 changes: 25 additions & 0 deletions tests/tween/tween.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Loading