Skip to content

Commit d5b6f95

Browse files
author
Hardy--Lee
committed
refactor: remove timeout in animation commands
1 parent fa4c9e8 commit d5b6f95

File tree

4 files changed

+61
-72
lines changed

4 files changed

+61
-72
lines changed

packages/webgal/src/Core/controller/stage/pixi/animations/generateTransformAnimationObj.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { AnimationFrame } from '@/Core/Modules/animations';
22
import { webgalStore } from '@/store/store';
33
import isNull from 'lodash/isNull';
44

5-
type AnimationObj = Array<AnimationFrame>;
5+
type AnimationFrames = Array<AnimationFrame>;
66

77
// eslint-disable-next-line max-params
88
export function generateTransformAnimationObj(
99
target: string,
1010
applyFrame: AnimationFrame,
1111
duration: number | string | boolean | null,
1212
ease: string,
13-
): AnimationObj {
13+
): AnimationFrames {
1414
let animationObj;
1515
// 获取那个 target 的当前变换
1616
const transformState = webgalStore.getState().stage.effects;

packages/webgal/src/Core/gameScripts/setAnimation.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,22 @@ export const setAnimation = (sentence: ISentence): IPerform => {
2626

2727
WebGAL.gameplay.performController.unmountPerform(performInitName, true);
2828

29-
let stopFunction;
30-
setTimeout(() => {
31-
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(target);
32-
const animationObj: IAnimationObject | null = getAnimationObject(
33-
animationName,
34-
target,
35-
animationDuration,
36-
writeDefault,
37-
);
38-
if (animationObj) {
39-
logger.debug(`动画${animationName}作用在${target}`, animationDuration);
40-
WebGAL.gameplay.pixiStage?.registerAnimation(animationObj, key, target);
41-
}
42-
}, 0);
43-
stopFunction = () => {
44-
setTimeout(() => {
45-
const endDialogKey = webgalStore.getState().stage.currentDialogKey;
46-
const isHasNext = startDialogKey !== endDialogKey;
47-
WebGAL.gameplay.pixiStage?.removeAnimationWithSetEffects(key);
48-
}, 0);
29+
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(target);
30+
const animationObj: IAnimationObject | null = getAnimationObject(
31+
animationName,
32+
target,
33+
animationDuration,
34+
writeDefault,
35+
);
36+
if (animationObj) {
37+
logger.debug(`动画${animationName}作用在${target}`, animationDuration);
38+
WebGAL.gameplay.pixiStage?.registerAnimation(animationObj, key, target);
39+
}
40+
41+
let stopFunction = () => {
42+
const endDialogKey = webgalStore.getState().stage.currentDialogKey;
43+
const isHasNext = startDialogKey !== endDialogKey;
44+
WebGAL.gameplay.pixiStage?.removeAnimationWithSetEffects(key);
4945
};
5046

5147
return {

packages/webgal/src/Core/gameScripts/setTempAnimation.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { webgalStore } from '@/store/store';
77
import { generateTimelineObj } from '@/Core/controller/stage/pixi/animations/timeline';
88
import cloneDeep from 'lodash/cloneDeep';
99
import { baseTransform } from '@/store/stageInterface';
10-
import { IUserAnimation } from '../Modules/animations';
10+
import { AnimationFrame, IUserAnimation } from '../Modules/animations';
1111
import { getAnimateDuration, getAnimationObject, getAnimationPerformName } from '@/Core/Modules/animationFunctions';
1212
import { WebGAL } from '@/Core/WebGAL';
1313

@@ -19,13 +19,13 @@ export const setTempAnimation = (sentence: ISentence): IPerform => {
1919
const startDialogKey = webgalStore.getState().stage.currentDialogKey;
2020
const animationName = (Math.random() * 10).toString(16);
2121
const animationString = sentence.content;
22-
let animationObj;
22+
let animationFrames: AnimationFrame[];
2323
try {
24-
animationObj = JSON.parse(animationString);
24+
animationFrames = JSON.parse(animationString);
2525
} catch (e) {
26-
animationObj = [];
26+
animationFrames = [];
2727
}
28-
const newAnimation: IUserAnimation = { name: animationName, effects: animationObj };
28+
const newAnimation: IUserAnimation = { name: animationName, effects: animationFrames };
2929
WebGAL.animationManager.addAnimation(newAnimation);
3030
const animationDuration = getAnimateDuration(animationName);
3131
const target = getStringArgByKey(sentence, 'target') ?? '0';
@@ -37,26 +37,22 @@ export const setTempAnimation = (sentence: ISentence): IPerform => {
3737

3838
WebGAL.gameplay.performController.unmountPerform(performInitName, true);
3939

40-
let stopFunction = () => {};
41-
setTimeout(() => {
42-
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(target);
43-
const animationObj: IAnimationObject | null = getAnimationObject(
44-
animationName,
45-
target,
46-
animationDuration,
47-
writeDefault,
48-
);
49-
if (animationObj) {
50-
logger.debug(`动画${animationName}作用在${target}`, animationDuration);
51-
WebGAL.gameplay.pixiStage?.registerAnimation(animationObj, key, target);
52-
}
53-
}, 0);
54-
stopFunction = () => {
55-
setTimeout(() => {
56-
const endDialogKey = webgalStore.getState().stage.currentDialogKey;
57-
const isHasNext = startDialogKey !== endDialogKey;
58-
WebGAL.gameplay.pixiStage?.removeAnimationWithSetEffects(key);
59-
}, 0);
40+
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(target);
41+
const animationObj: IAnimationObject | null = getAnimationObject(
42+
animationName,
43+
target,
44+
animationDuration,
45+
writeDefault,
46+
);
47+
if (animationObj) {
48+
logger.debug(`动画${animationName}作用在${target}`, animationDuration);
49+
WebGAL.gameplay.pixiStage?.registerAnimation(animationObj, key, target);
50+
}
51+
52+
let stopFunction = () => {
53+
const endDialogKey = webgalStore.getState().stage.currentDialogKey;
54+
const isHasNext = startDialogKey !== endDialogKey;
55+
WebGAL.gameplay.pixiStage?.removeAnimationWithSetEffects(key);
6056
};
6157

6258
return {

packages/webgal/src/Core/gameScripts/setTransform.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const setTransform = (sentence: ISentence): IPerform => {
2020
const startDialogKey = webgalStore.getState().stage.currentDialogKey;
2121
const animationName = (Math.random() * 10).toString(16);
2222
const animationString = sentence.content;
23-
let animationObj: AnimationFrame[];
23+
let animationFrames: AnimationFrame[];
2424

2525
const duration = getNumberArgByKey(sentence, 'duration') ?? 500;
2626
const ease = getStringArgByKey(sentence, 'ease') ?? '';
@@ -34,38 +34,35 @@ export const setTransform = (sentence: ISentence): IPerform => {
3434

3535
try {
3636
const frame = JSON.parse(animationString) as AnimationFrame;
37-
animationObj = generateTransformAnimationObj(target, frame, duration, ease);
38-
console.log('animationObj:', animationObj);
37+
animationFrames = generateTransformAnimationObj(target, frame, duration, ease);
38+
console.log('animationFrames:', animationFrames);
3939
} catch (e) {
4040
// 解析都错误了,歇逼吧
41-
animationObj = [];
41+
animationFrames = [];
4242
}
4343

44-
const newAnimation: IUserAnimation = { name: animationName, effects: animationObj };
44+
const newAnimation: IUserAnimation = { name: animationName, effects: animationFrames };
4545
WebGAL.animationManager.addAnimation(newAnimation);
4646
const animationDuration = getAnimateDuration(animationName);
4747

4848
const key = `${target}-${animationName}-${animationDuration}`;
49-
let stopFunction = () => {};
50-
setTimeout(() => {
51-
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(target);
52-
const animationObj: IAnimationObject | null = getAnimationObject(
53-
animationName,
54-
target,
55-
animationDuration,
56-
writeDefault,
57-
);
58-
if (animationObj) {
59-
logger.debug(`动画${animationName}作用在${target}`, animationDuration);
60-
WebGAL.gameplay.pixiStage?.registerAnimation(animationObj, key, target);
61-
}
62-
}, 0);
63-
stopFunction = () => {
64-
setTimeout(() => {
65-
const endDialogKey = webgalStore.getState().stage.currentDialogKey;
66-
const isHasNext = startDialogKey !== endDialogKey;
67-
WebGAL.gameplay.pixiStage?.removeAnimationWithSetEffects(key);
68-
}, 0);
49+
50+
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(target);
51+
const animationObj: IAnimationObject | null = getAnimationObject(
52+
animationName,
53+
target,
54+
animationDuration,
55+
writeDefault,
56+
);
57+
if (animationObj) {
58+
logger.debug(`动画${animationName}作用在${target}`, animationDuration);
59+
WebGAL.gameplay.pixiStage?.registerAnimation(animationObj, key, target);
60+
}
61+
62+
let stopFunction = () => {
63+
const endDialogKey = webgalStore.getState().stage.currentDialogKey;
64+
const isHasNext = startDialogKey !== endDialogKey;
65+
WebGAL.gameplay.pixiStage?.removeAnimationWithSetEffects(key);
6966
};
7067

7168
return {

0 commit comments

Comments
 (0)