Skip to content
1 change: 1 addition & 0 deletions packages/parser/src/config/scriptConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const SCRIPT_CONFIG = [
{ scriptString: 'wait', scriptType: commandType.wait },
{ scriptString: 'callSteam', scriptType: commandType.callSteam },
{ scriptString: 'return', scriptType: commandType.return },
{ scriptString: 'character', scriptType: commandType.character },
];
export const ADD_NEXT_ARG_LIST = [
commandType.bgm,
Expand Down
1 change: 1 addition & 0 deletions packages/parser/src/interface/sceneInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export enum commandType {
callSteam, // 调用Steam功能
return, // 从被调用的场景返回
changeFigureDiff, // 切换等尺寸静态立绘差分;追加以保持旧指令编号
character, // 管理静态组合角色
}

/**
Expand Down
71 changes: 71 additions & 0 deletions packages/parser/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,74 @@ test("comment-only line preserves following semicolons", async () => {
};
expectSentenceIn(result.sentenceList, expectSentenceItem);
});

test("character command parses default preset, explicit preset, components, none, and clear without emitting raw targets into assetsList", () => {
const prefetchedAssets: IAsset[] = [];
const parser = new SceneParser(
(assetList) => {
prefetchedAssets.push(...assetList);
},
(fileName) => `/game/figure/${fileName}`,
ADD_NEXT_ARG_LIST,
SCRIPT_CONFIG,
);

const result = parser.parse(
`character:alice ;默认第一个预设\ncharacter:alice.smile -left;\ncharacter:alice -components=school_body, eye_open, mouth_smile -right;\ncharacter:none;\ncharacter:alice -clear;`,
'test',
'test',
);

expect(result.sentenceList.map(dropLineRange)).toEqual([
{
command: commandType.character,
commandRaw: 'character',
content: 'alice',
args: [],
sentenceAssets: [],
subScene: [],
inlineComment: '默认第一个预设',
},
{
command: commandType.character,
commandRaw: 'character',
content: 'alice.smile',
args: [{ key: 'left', value: true }],
sentenceAssets: [],
subScene: [],
inlineComment: '',
},
{
command: commandType.character,
commandRaw: 'character',
content: 'alice',
args: [
{ key: 'components', value: 'school_body, eye_open, mouth_smile' },
{ key: 'right', value: true },
],
sentenceAssets: [],
subScene: [],
inlineComment: '',
},
{
command: commandType.character,
commandRaw: 'character',
content: '',
args: [],
sentenceAssets: [],
subScene: [],
inlineComment: '',
},
{
command: commandType.character,
commandRaw: 'character',
content: 'alice',
args: [{ key: 'clear', value: true }],
sentenceAssets: [],
subScene: [],
inlineComment: '',
},
]);
expect(result.assetsList).toEqual([]);
expect(prefetchedAssets).toEqual([]);
});
2 changes: 1 addition & 1 deletion packages/parser/test/parserOptionalAssets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SceneParser from '../src/index';
import { IAsset } from '../src/interface/sceneInterface';

const SCENE = `changeBg:a.webp;
character:hello -vocal=b.mp3;
speaker:hello -vocal=b.mp3;
callScene:chapter.txt;`;

const identityAssetSetter = (fileName: string) => fileName;
Expand Down
12 changes: 8 additions & 4 deletions packages/webgal/src/Core/Modules/animationFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export function getAnimateDuration(animationName: string) {
return 0;
}

export function getExitDuration(target: string, isBg = false): number {
const setting = stageStateManager.getCalculationStageState().animationSettings.find((item) => item.target === target);
if (setting?.exitAnimationName) return getAnimateDuration(setting.exitAnimationName);
return setting?.exitDuration ?? (isBg ? DEFAULT_BG_OUT_DURATION : DEFAULT_FIG_OUT_DURATION);
}

/**
* 取退出动画。
*
Expand All @@ -110,11 +116,10 @@ export function getExitAnimation(
duration: number;
animation: IAnimationObject | null;
} {
let duration = isBg ? DEFAULT_BG_OUT_DURATION : DEFAULT_FIG_OUT_DURATION;
const duration = getExitDuration(target, isBg);
const animationSettings = stageStateManager
.getCalculationStageState()
.animationSettings.find((setting) => setting.target === target);
duration = animationSettings?.exitDuration ?? duration;
// 走默认动画
let animation: IAnimationObject | null = generateUniversalSoftOffAnimationObj(realTarget ?? target, duration);
const animationName = animationSettings?.exitAnimationName;
Expand All @@ -123,11 +128,10 @@ export function getExitAnimation(
animation = getAnimationObject(
animationName,
realTarget ?? target,
getAnimateDuration(animationName),
duration,
false,
!(animationSettings?.exitAnimationIgnoreDefault ?? false),
);
duration = getAnimateDuration(animationName);
}
if (animationSettings) {
// 退出动画拿完后,删了这个设定
Expand Down
9 changes: 9 additions & 0 deletions packages/webgal/src/Core/Modules/stage/stageInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ export interface IFreeFigure {
key: string;
}

export interface ICharacterFigure {
key: string;
name: string;
basePosition: IFigurePosition;
preset?: string;
components?: string[];
}

/**
* Live2D 自定义绘制范围的归一:未指定与全 0 是同一件事。
*
Expand Down Expand Up @@ -225,6 +233,7 @@ export interface IStageState {
figNameRight14: string; // 立绘_右 1/4 文件地址(相对或绝对)
// 自由立绘
freeFigure: Array<IFreeFigure>;
characterFigures: Array<ICharacterFigure>;
figureAssociatedAnimation: Array<IFigureAssociatedAnimation>;
isRead: boolean; // 是否已读
showText: string; // 文字
Expand Down
34 changes: 34 additions & 0 deletions packages/webgal/src/Core/Modules/stage/stageStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { baseBlinkParam, baseFocusParam } from '@/Core/live2DCore';
import {
baseTransform,
FIGURE_KEYS,
FIGURE_POSITIONS,
figureStateKeyByPosition,
ICharacterFigure,
IEffect,
IFigureMetadata,
IFreeFigure,
Expand Down Expand Up @@ -48,6 +51,7 @@ export const initState: IStageState = {
figNameLeft14: '',
figNameRight14: '',
freeFigure: [],
characterFigures: [],
figureAssociatedAnimation: [],
isRead: false,
showText: '',
Expand Down Expand Up @@ -132,6 +136,7 @@ export class StageStateManager {
// 读档、回溯与场景重建只恢复普通状态,不能携带上一次演算的换图意图。
WebGAL.figureDiffManager.clear();
this.calculationStageState = cloneDeep(stageState);
this.calculationStageState.characterFigures ??= [];
}

public replaceAllStageState(stageState: IStageState, options?: IStageCommitOptions) {
Expand All @@ -156,6 +161,7 @@ export class StageStateManager {
STAGE_KEYS.BGMAIN,
...FIGURE_KEYS,
...state.freeFigure.map((figure) => figure.key),
...(state.characterFigures ?? []).map((character) => character.key),
];
if (!activeTargets.includes(target)) return;

Expand Down Expand Up @@ -291,6 +297,34 @@ export class StageStateManager {
}
}

public setCharacterFigureByKey(character: ICharacterFigure): void {
const current = (this.calculationStageState.characterFigures ??= []);
const next: ICharacterFigure = {
key: character.key,
name: character.name,
basePosition: character.basePosition,
...(character.preset !== undefined ? { preset: character.preset } : {}),
...(character.components !== undefined ? { components: [...character.components] } : {}),
};
const index = current.findIndex((entry) => entry.key === character.key);
if (index >= 0) current[index] = next;
else current.push(next);
}

public removeCharacterFigureByKey(key: string): void {
const current = (this.calculationStageState.characterFigures ??= []);
const index = current.findIndex((entry) => entry.key === key);
if (index >= 0) current.splice(index, 1);
}

public clearStandardFigureSlotByKey(key: string): void {
const state = this.calculationStageState;
const pos = FIGURE_POSITIONS.find((p) => `fig-${p}` === key);
if (pos) state[figureStateKeyByPosition[pos]] = '';
this.setFreeFigureByKey({ key, name: '', basePosition: 'center' });
state.figureAssociatedAnimation = state.figureAssociatedAnimation.filter((item) => item.targetId !== key);
}

public setLive2dMotion(payload: ILive2DMotion) {
const { target, motion, skin, overrideBounds } = payload;
const index = this.calculationStageState.live2dMotion.findIndex((e) => e.target === target);
Expand Down
Loading
Loading