-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathchangeFigure.ts
More file actions
295 lines (277 loc) · 10.8 KB
/
changeFigure.ts
File metadata and controls
295 lines (277 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { ISentence } from '@/Core/controller/scene/sceneInterface';
import { IPerform } from '@/Core/Modules/perform/performInterface';
import { webgalStore } from '@/store/store';
import { setStage, stageActions } from '@/store/stageReducer';
import cloneDeep from 'lodash/cloneDeep';
import { getBooleanArgByKey, getNumberArgByKey, getStringArgByKey } from '@/Core/util/getSentenceArg';
import { IFreeFigure, IStageState, ITransform } from '@/store/stageInterface';
import { AnimationFrame, IUserAnimation } from '@/Core/Modules/animations';
import { generateTransformAnimationObj } from '@/Core/controller/stage/pixi/animations/generateTransformAnimationObj';
import { assetSetter, fileType } from '@/Core/util/gameAssetsAccess/assetSetter';
import { logger } from '@/Core/util/logger';
import { getAnimateDuration } from '@/Core/Modules/animationFunctions';
import { WebGAL } from '@/Core/WebGAL';
import { baseBlinkParam, baseFocusParam, BlinkParam, FocusParam } from '@/Core/live2DCore';
import { STAGE_KEYS, WEBGAL_NONE } from '../constants';
/**
* 更改立绘
* @param sentence 语句
*/
// eslint-disable-next-line complexity
export function changeFigure(sentence: ISentence): IPerform {
// 语句内容
let content = sentence.content;
if (content === WEBGAL_NONE) {
content = '';
}
if (getBooleanArgByKey(sentence, 'clear')) {
content = '';
}
// 根据参数设置指定位置
let pos: 'center' | 'left' | 'right' = 'center';
let mouthAnimationKey = 'mouthAnimation';
let eyesAnimationKey = 'blinkAnimation';
const leftFromArgs = getBooleanArgByKey(sentence, 'left') ?? false;
const rightFromArgs = getBooleanArgByKey(sentence, 'right') ?? false;
if (leftFromArgs) {
pos = 'left';
mouthAnimationKey = 'mouthAnimationLeft';
eyesAnimationKey = 'blinkAnimationLeft';
}
if (rightFromArgs) {
pos = 'right';
mouthAnimationKey = 'mouthAnimationRight';
eyesAnimationKey = 'blinkAnimationRight';
}
// id 与 自由立绘
let idFromArgs = getStringArgByKey(sentence, 'id') ?? '';
const isFreeFigure = idFromArgs ? true : false;
let key = '';
if (isFreeFigure) {
key = idFromArgs;
} else {
switch (pos) {
case 'center':
key = STAGE_KEYS.FIG_C;
break;
case 'left':
key = STAGE_KEYS.FIG_L;
break;
case 'right':
key = STAGE_KEYS.FIG_R;
break;
}
}
// live2d 或 spine 相关
let motion = getStringArgByKey(sentence, 'motion') ?? '';
let expression = getStringArgByKey(sentence, 'expression') ?? '';
const boundsFromArgs = getStringArgByKey(sentence, 'bounds') ?? '';
let bounds = getOverrideBoundsArr(boundsFromArgs);
let blink: BlinkParam | null = null;
const blinkFromArgs = getStringArgByKey(sentence, 'blink');
if (blinkFromArgs) {
try {
blink = JSON.parse(blinkFromArgs) as BlinkParam;
} catch (error) {
logger.error('Failed to parse blink parameter:', error);
}
}
let focus: FocusParam | null = null;
const focusFromArgs = getStringArgByKey(sentence, 'focus');
if (focusFromArgs) {
try {
focus = JSON.parse(focusFromArgs) as FocusParam;
} catch (error) {
logger.error('Failed to parse focus parameter:', error);
}
}
// 图片立绘差分
const mouthOpen = assetSetter(getStringArgByKey(sentence, 'mouthOpen') ?? '', fileType.figure);
const mouthClose = assetSetter(getStringArgByKey(sentence, 'mouthClose') ?? '', fileType.figure);
const mouthHalfOpen = assetSetter(getStringArgByKey(sentence, 'mouthHalfOpen') ?? '', fileType.figure);
const eyesOpen = assetSetter(getStringArgByKey(sentence, 'eyesOpen') ?? '', fileType.figure);
const eyesClose = assetSetter(getStringArgByKey(sentence, 'eyesClose') ?? '', fileType.figure);
const animationFlag = getStringArgByKey(sentence, 'animationFlag') ?? '';
// 其他参数
const transformString = getStringArgByKey(sentence, 'transform');
const ease = getStringArgByKey(sentence, 'ease') ?? '';
let duration = getNumberArgByKey(sentence, 'duration') ?? 500;
const enterAnimation = getStringArgByKey(sentence, 'enter');
const exitAnimation = getStringArgByKey(sentence, 'exit');
let zIndex = getNumberArgByKey(sentence, 'zIndex') ?? -1;
const dispatch = webgalStore.dispatch;
const currentFigureAssociatedAnimation = webgalStore.getState().stage.figureAssociatedAnimation;
const filteredFigureAssociatedAnimation = currentFigureAssociatedAnimation.filter((item) => item.targetId !== key);
const newFigureAssociatedAnimationItem = {
targetId: key,
animationFlag: animationFlag,
mouthAnimation: {
open: mouthOpen,
close: mouthClose,
halfOpen: mouthHalfOpen,
},
blinkAnimation: {
open: eyesOpen,
close: eyesClose,
},
};
filteredFigureAssociatedAnimation.push(newFigureAssociatedAnimationItem);
dispatch(setStage({ key: 'figureAssociatedAnimation', value: filteredFigureAssociatedAnimation }));
// 检测 url 是否变化
let isUrlChanged = true;
if (key !== '') {
const figWithKey = webgalStore.getState().stage.freeFigure.find((e) => e.key === key);
if (figWithKey) {
if (figWithKey.name === sentence.content) {
isUrlChanged = false;
}
}
} else {
if (pos === 'center') {
if (webgalStore.getState().stage.figName === sentence.content) {
isUrlChanged = false;
}
}
if (pos === 'left') {
if (webgalStore.getState().stage.figNameLeft === sentence.content) {
isUrlChanged = false;
}
}
if (pos === 'right') {
if (webgalStore.getState().stage.figNameRight === sentence.content) {
isUrlChanged = false;
}
}
}
if (isUrlChanged) {
// 清除动画
WebGAL.gameplay.pixiStage?.removeAnimation(key);
// 移除旧的 effect
webgalStore.dispatch(stageActions.removeEffectByTargetId(key));
// 标记旧的立绘为退出中,防止旧立绘继承新参数
const oldStageObject = WebGAL.gameplay.pixiStage?.getStageObjByKey(key);
if (oldStageObject) {
oldStageObject.isExiting = true;
}
}
const setAnimationNames = (key: string, sentence: ISentence) => {
// 处理 transform 和 默认 transform
let animationObj: AnimationFrame[];
if (transformString) {
console.log(transformString);
try {
const frame = JSON.parse(transformString) as AnimationFrame;
animationObj = generateTransformAnimationObj(key, frame, duration, ease);
// 因为是切换,必须把一开始的 alpha 改为 0
animationObj[0].alpha = 0;
const animationName = (Math.random() * 10).toString(16);
const newAnimation: IUserAnimation = { name: animationName, effects: animationObj };
WebGAL.animationManager.addAnimation(newAnimation);
duration = getAnimateDuration(animationName);
WebGAL.animationManager.nextEnterAnimationName.set(key, animationName);
} catch (e) {
// 解析都错误了,歇逼吧
applyDefaultTransform();
}
} else {
applyDefaultTransform();
}
function applyDefaultTransform() {
// 应用默认的
const frame = {};
animationObj = generateTransformAnimationObj(key, frame as AnimationFrame, duration, ease);
// 因为是切换,必须把一开始的 alpha 改为 0
animationObj[0].alpha = 0;
const animationName = (Math.random() * 10).toString(16);
const newAnimation: IUserAnimation = { name: animationName, effects: animationObj };
WebGAL.animationManager.addAnimation(newAnimation);
duration = getAnimateDuration(animationName);
WebGAL.animationManager.nextEnterAnimationName.set(key, animationName);
}
if (enterAnimation) {
WebGAL.animationManager.nextEnterAnimationName.set(key, enterAnimation);
duration = getAnimateDuration(enterAnimation);
}
if (exitAnimation) {
WebGAL.animationManager.nextExitAnimationName.set(key + '-off', exitAnimation);
duration = getAnimateDuration(exitAnimation);
}
};
function postFigureStateSet() {
if (isUrlChanged) {
// 当 url 发生变化时,即发生新立绘替换
// 应当赋予一些参数以默认值,防止从旧立绘的状态获取数据
bounds = bounds ?? [0, 0, 0, 0];
blink = blink ?? cloneDeep(baseBlinkParam);
focus = focus ?? cloneDeep(baseFocusParam);
zIndex = Math.max(zIndex, 0);
dispatch(stageActions.setLive2dMotion({ target: key, motion, overrideBounds: bounds }));
dispatch(stageActions.setLive2dExpression({ target: key, expression }));
dispatch(stageActions.setLive2dBlink({ target: key, blink }));
dispatch(stageActions.setLive2dFocus({ target: key, focus }));
dispatch(stageActions.setFigureMetaData([key, 'zIndex', zIndex, false]));
} else {
// 当 url 没有发生变化时,即没有新立绘替换
// 应当保留旧立绘的状态,仅在需要时更新
if (motion || bounds) {
dispatch(stageActions.setLive2dMotion({ target: key, motion, overrideBounds: bounds }));
}
if (expression) {
dispatch(stageActions.setLive2dExpression({ target: key, expression }));
}
if (blink) {
dispatch(stageActions.setLive2dBlink({ target: key, blink }));
}
if (focus) {
dispatch(stageActions.setLive2dFocus({ target: key, focus }));
}
if (zIndex >= 0) {
dispatch(stageActions.setFigureMetaData([key, 'zIndex', zIndex, false]));
}
}
}
if (isFreeFigure) {
/**
* 下面的代码是设置自由立绘的
*/
const freeFigureItem: IFreeFigure = { key, name: content, basePosition: pos };
setAnimationNames(key, sentence);
postFigureStateSet();
dispatch(stageActions.setFreeFigureByKey(freeFigureItem));
} else {
/**
* 下面的代码是设置与位置关联的立绘的
*/
const dispatchMap: Record<string, keyof IStageState> = {
center: 'figName',
left: 'figNameLeft',
right: 'figNameRight',
};
setAnimationNames(key, sentence);
postFigureStateSet();
dispatch(setStage({ key: dispatchMap[pos], value: content }));
}
return {
performName: `enter-${key}`,
duration,
isHoldOn: false,
stopFunction: () => {
WebGAL.gameplay.pixiStage?.stopPresetAnimationOnTarget(key);
},
blockingNext: () => false,
blockingAuto: () => true,
stopTimeout: undefined, // 暂时不用,后面会交给自动清除
};
}
function getOverrideBoundsArr(raw: string): undefined | [number, number, number, number] {
const parseOverrideBoundsResult = raw.split(',').map((e) => Number(e));
let isPass = true;
parseOverrideBoundsResult.forEach((e) => {
if (isNaN(e)) {
isPass = false;
}
});
isPass = isPass && parseOverrideBoundsResult.length === 4;
if (isPass) return parseOverrideBoundsResult as [number, number, number, number];
else return undefined;
}