Skip to content

Commit fa4c9e8

Browse files
author
Hardy--Lee
committed
fix: clear animation before exiting
1 parent fd35764 commit fa4c9e8

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

packages/webgal/src/Core/Modules/animationFunctions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@ export function getEnterExitAnimation(
9393
return { duration, animation };
9494
}
9595
}
96+
97+
export function getAnimationPerformName(target: string) {
98+
return `animation-${target}`;
99+
}

packages/webgal/src/Core/gameScripts/changeBg/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ITransform } from '@/store/stageInterface';
1111
import { generateTransformAnimationObj } from '@/Core/controller/stage/pixi/animations/generateTransformAnimationObj';
1212
import { AnimationFrame, IUserAnimation } from '@/Core/Modules/animations';
1313
import cloneDeep from 'lodash/cloneDeep';
14-
import { getAnimateDuration } from '@/Core/Modules/animationFunctions';
14+
import { getAnimateDuration, getAnimationPerformName } from '@/Core/Modules/animationFunctions';
1515
import { WebGAL } from '@/Core/WebGAL';
1616

1717
/**
@@ -32,11 +32,18 @@ export const changeBg = (sentence: ISentence): IPerform => {
3232
dispatch(unlockCgInUserData({ name: unlockName, url, series }));
3333
}
3434

35-
/**
36-
* 删掉相关 Effects,因为已经移除了
37-
*/
38-
if (webgalStore.getState().stage.bgName !== sentence.content) {
35+
// 检测 url 是否变化
36+
let isUrlChanged = webgalStore.getState().stage.bgName !== sentence.content;
37+
if (isUrlChanged) {
38+
// 移除旧的 effect
3939
dispatch(stageActions.removeEffectByTargetId(`bg-main`));
40+
// 清除动画演出
41+
WebGAL.gameplay.performController.unmountPerform(getAnimationPerformName('bg-main'), true);
42+
// 标记旧的背景为退出中,防止旧背景继承新参数
43+
const oldStageObject = WebGAL.gameplay.pixiStage?.getStageObjByKey('bg-main');
44+
if (oldStageObject) {
45+
oldStageObject.isExiting = true;
46+
}
4047
}
4148

4249
// 处理 transform 和 默认 transform

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { AnimationFrame, IUserAnimation } from '@/Core/Modules/animations';
99
import { generateTransformAnimationObj } from '@/Core/controller/stage/pixi/animations/generateTransformAnimationObj';
1010
import { assetSetter, fileType } from '@/Core/util/gameAssetsAccess/assetSetter';
1111
import { logger } from '@/Core/util/logger';
12-
import { getAnimateDuration } from '@/Core/Modules/animationFunctions';
12+
import { getAnimateDuration, getAnimationPerformName } from '@/Core/Modules/animationFunctions';
1313
import { WebGAL } from '@/Core/WebGAL';
1414
import { baseBlinkParam, baseFocusParam, BlinkParam, FocusParam } from '@/Core/live2DCore';
15-
import { WEBGAL_NONE } from '../constants';
15+
import { STAGE_KEYS, WEBGAL_NONE } from '../constants';
1616
/**
1717
* 更改立绘
1818
* @param sentence 语句
@@ -46,9 +46,24 @@ export function changeFigure(sentence: ISentence): IPerform {
4646
}
4747

4848
// id 与 自由立绘
49-
let key = getStringArgByKey(sentence, 'id') ?? '';
50-
const isFreeFigure = key ? true : false;
51-
const id = key ? key : `fig-${pos}`;
49+
let idFromArgs = getStringArgByKey(sentence, 'id') ?? '';
50+
const isFreeFigure = idFromArgs ? true : false;
51+
let key = '';
52+
if (isFreeFigure) {
53+
key = idFromArgs;
54+
} else {
55+
switch (pos) {
56+
case 'center':
57+
key = STAGE_KEYS.FIG_C;
58+
break;
59+
case 'left':
60+
key = STAGE_KEYS.FIG_L;
61+
break;
62+
case 'right':
63+
key = STAGE_KEYS.FIG_R;
64+
break;
65+
}
66+
}
5267

5368
// live2d 或 spine 相关
5469
let motion = getStringArgByKey(sentence, 'motion') ?? '';
@@ -95,9 +110,9 @@ export function changeFigure(sentence: ISentence): IPerform {
95110
const dispatch = webgalStore.dispatch;
96111

97112
const currentFigureAssociatedAnimation = webgalStore.getState().stage.figureAssociatedAnimation;
98-
const filteredFigureAssociatedAnimation = currentFigureAssociatedAnimation.filter((item) => item.targetId !== id);
113+
const filteredFigureAssociatedAnimation = currentFigureAssociatedAnimation.filter((item) => item.targetId !== key);
99114
const newFigureAssociatedAnimationItem = {
100-
targetId: id,
115+
targetId: key,
101116
animationFlag: animationFlag,
102117
mouthAnimation: {
103118
open: mouthOpen,
@@ -112,9 +127,7 @@ export function changeFigure(sentence: ISentence): IPerform {
112127
filteredFigureAssociatedAnimation.push(newFigureAssociatedAnimationItem);
113128
dispatch(setStage({ key: 'figureAssociatedAnimation', value: filteredFigureAssociatedAnimation }));
114129

115-
/**
116-
* 如果 url 没变,不移除
117-
*/
130+
// 检测 url 是否变化
118131
let isUrlChanged = true;
119132
if (key !== '') {
120133
const figWithKey = webgalStore.getState().stage.freeFigure.find((e) => e.key === key);
@@ -140,16 +153,19 @@ export function changeFigure(sentence: ISentence): IPerform {
140153
}
141154
}
142155
}
143-
/**
144-
* 处理 Effects
145-
*/
156+
146157
if (isUrlChanged) {
147-
webgalStore.dispatch(stageActions.removeEffectByTargetId(id));
148-
const oldStageObject = WebGAL.gameplay.pixiStage?.getStageObjByKey(id);
158+
// 移除旧的 effect
159+
webgalStore.dispatch(stageActions.removeEffectByTargetId(key));
160+
// 清除动画演出
161+
WebGAL.gameplay.performController.unmountPerform(getAnimationPerformName(key), true);
162+
// 标记旧的立绘为退出中,防止旧立绘继承新参数
163+
const oldStageObject = WebGAL.gameplay.pixiStage?.getStageObjByKey(key);
149164
if (oldStageObject) {
150165
oldStageObject.isExiting = true;
151166
}
152167
}
168+
153169
const setAnimationNames = (key: string, sentence: ISentence) => {
154170
// 处理 transform 和 默认 transform
155171
let animationObj: AnimationFrame[];
@@ -242,18 +258,12 @@ export function changeFigure(sentence: ISentence): IPerform {
242258
/**
243259
* 下面的代码是设置与位置关联的立绘的
244260
*/
245-
const positionMap = {
246-
center: 'fig-center',
247-
left: 'fig-left',
248-
right: 'fig-right',
249-
};
250261
const dispatchMap: Record<string, keyof IStageState> = {
251262
center: 'figName',
252263
left: 'figNameLeft',
253264
right: 'figNameRight',
254265
};
255266

256-
key = positionMap[pos];
257267
setAnimationNames(key, sentence);
258268
setFigureData();
259269
dispatch(setStage({ key: dispatchMap[pos], value: content }));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IAnimationObject } from '@/Core/controller/stage/pixi/PixiController';
55
import { logger } from '@/Core/util/logger';
66
import { webgalStore } from '@/store/store';
77

8-
import { getAnimateDuration, getAnimationObject } from '@/Core/Modules/animationFunctions';
8+
import { getAnimateDuration, getAnimationObject, getAnimationPerformName } from '@/Core/Modules/animationFunctions';
99
import { WebGAL } from '@/Core/WebGAL';
1010

1111
/**
@@ -22,7 +22,7 @@ export const setAnimation = (sentence: ISentence): IPerform => {
2222
const keep = getBooleanArgByKey(sentence, 'keep') ?? false;
2323

2424
const key = `${target}-${animationName}-${animationDuration}`;
25-
const performInitName = `animation-${target}`;
25+
const performInitName = getAnimationPerformName(target);
2626

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { generateTimelineObj } from '@/Core/controller/stage/pixi/animations/tim
88
import cloneDeep from 'lodash/cloneDeep';
99
import { baseTransform } from '@/store/stageInterface';
1010
import { IUserAnimation } from '../Modules/animations';
11-
import { getAnimateDuration, getAnimationObject } from '@/Core/Modules/animationFunctions';
11+
import { getAnimateDuration, getAnimationObject, getAnimationPerformName } from '@/Core/Modules/animationFunctions';
1212
import { WebGAL } from '@/Core/WebGAL';
1313

1414
/**
@@ -33,7 +33,7 @@ export const setTempAnimation = (sentence: ISentence): IPerform => {
3333
const keep = getBooleanArgByKey(sentence, 'keep') ?? false;
3434

3535
const key = `${target}-${animationName}-${animationDuration}`;
36-
const performInitName = `animation-${target}`;
36+
const performInitName = getAnimationPerformName(target);
3737

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { baseTransform, ITransform } from '@/store/stageInterface';
1010
import { AnimationFrame, IUserAnimation } from '../Modules/animations';
1111
import { generateTransformAnimationObj } from '@/Core/controller/stage/pixi/animations/generateTransformAnimationObj';
1212
import { WebGAL } from '@/Core/WebGAL';
13-
import { getAnimateDuration, getAnimationObject } from '../Modules/animationFunctions';
13+
import { getAnimateDuration, getAnimationObject, getAnimationPerformName } from '@/Core/Modules/animationFunctions';
1414

1515
/**
1616
* 设置变换
@@ -28,7 +28,7 @@ export const setTransform = (sentence: ISentence): IPerform => {
2828
const target = getStringArgByKey(sentence, 'target') ?? '0';
2929
const keep = getBooleanArgByKey(sentence, 'keep') ?? false;
3030

31-
const performInitName = `animation-${target}`;
31+
const performInitName = getAnimationPerformName(target);
3232

3333
WebGAL.gameplay.performController.unmountPerform(performInitName, true);
3434

0 commit comments

Comments
 (0)