Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 12 additions & 19 deletions packages/webgal/src/Core/gameScripts/changeBg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { WebGAL } from '@/Core/WebGAL';
import { DEFAULT_BG_OUT_DURATION } from '@/Core/constants';
import localforage from 'localforage';
import { stageStateManager } from '@/Core/Modules/stage/stageStateManager';
import { parseTransformFrame } from '../parseTransformFrame';

/**
* 进行背景图片的切换
Expand Down Expand Up @@ -56,29 +57,15 @@ export const changeBg = (sentence: ISentence): IPerform => {

// 处理 transform 和 默认 transform
let animationObj: AnimationFrame[];
if (transformString) {
try {
const frame = JSON.parse(transformString.toString()) as AnimationFrame;
animationObj = generateTransformAnimationObj('bg-main', frame, enterDuration, ease, !ignoreDefault);
// 因为是切换,必须把一开始的 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);
stageStateManager.updateAnimationSettings({ target: 'bg-main', key: 'enterAnimationName', value: animationName });
} catch (e) {
// 解析都错误了,歇逼吧
applyDefaultTransform();
}
const frame = transformString ? parseTransformFrame(transformString) : null;
if (frame) {
applyTransform(frame);
} else {
applyDefaultTransform();
}

function applyDefaultTransform() {
// 应用默认的
const frame = {};
animationObj = generateTransformAnimationObj('bg-main', frame as AnimationFrame, duration, ease, !ignoreDefault);
function applyTransform(frame: AnimationFrame) {
animationObj = generateTransformAnimationObj('bg-main', frame, enterDuration, ease, !ignoreDefault);
// 因为是切换,必须把一开始的 alpha 改为 0
animationObj[0].alpha = 0;
const animationName = (Math.random() * 10).toString(16);
Expand All @@ -87,6 +74,12 @@ export const changeBg = (sentence: ISentence): IPerform => {
duration = getAnimateDuration(animationName);
stageStateManager.updateAnimationSettings({ target: 'bg-main', key: 'enterAnimationName', value: animationName });
}

function applyDefaultTransform() {
// 应用默认的
const frame = {};
applyTransform(frame as AnimationFrame);
}
stageStateManager.updateAnimationSettings({
target: 'bg-main',
key: 'enterAnimationIgnoreDefault',
Expand Down
32 changes: 12 additions & 20 deletions packages/webgal/src/Core/gameScripts/changeFigure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { WebGAL } from '@/Core/WebGAL';
import { baseBlinkParam, baseFocusParam, BlinkParam, FocusParam } from '@/Core/live2DCore';
import { DEFAULT_FIG_IN_DURATION, DEFAULT_FIG_OUT_DURATION, WEBGAL_NONE } from '../constants';
import { stageStateManager } from '@/Core/Modules/stage/stageStateManager';
import { parseTransformFrame } from './parseTransformFrame';
/**
* 更改立绘
* @param sentence 语句
Expand Down Expand Up @@ -161,30 +162,15 @@ export function changeFigure(sentence: ISentence): IPerform {
}
// 处理 transform 和 默认 transform
let animationObj: AnimationFrame[];
if (transformString) {
console.log(transformString);
try {
const frame = JSON.parse(transformString) as AnimationFrame;
animationObj = generateTransformAnimationObj(key, frame, duration, ease, !ignoreDefault);
// 因为是切换,必须把一开始的 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);
stageStateManager.updateAnimationSettings({ target: key, key: 'enterAnimationName', value: animationName });
} catch (e) {
// 解析都错误了,歇逼吧
applyDefaultTransform();
}
const frame = transformString ? parseTransformFrame(transformString) : null;
if (frame) {
applyTransform(frame);
} else {
applyDefaultTransform();
}

function applyDefaultTransform() {
// 应用默认的
const frame = {};
animationObj = generateTransformAnimationObj(key, frame as AnimationFrame, duration, ease, !ignoreDefault);
function applyTransform(frame: AnimationFrame) {
animationObj = generateTransformAnimationObj(key, frame, duration, ease, !ignoreDefault);
// 因为是切换,必须把一开始的 alpha 改为 0
animationObj[0].alpha = 0;
const animationName = (Math.random() * 10).toString(16);
Expand All @@ -193,6 +179,12 @@ export function changeFigure(sentence: ISentence): IPerform {
duration = getAnimateDuration(animationName);
stageStateManager.updateAnimationSettings({ target: key, key: 'enterAnimationName', value: animationName });
}

function applyDefaultTransform() {
// 应用默认的
const frame = {};
applyTransform(frame as AnimationFrame);
}
stageStateManager.updateAnimationSettings({
target: key,
key: 'enterAnimationIgnoreDefault',
Expand Down
21 changes: 21 additions & 0 deletions packages/webgal/src/Core/gameScripts/parseTransformFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AnimationFrame } from '@/Core/Modules/animations';

export function parseTransformFrame(raw: string): AnimationFrame | null {
const source = raw.trim();
if (source === '') return null;

try {
const parsed: unknown = JSON.parse(source);
return isTransformFrame(parsed) ? parsed : null;
} catch {
return null;
}
}

export function parseSetTransformFrame(raw: string): AnimationFrame | null {
return parseTransformFrame(raw.trim() === '' ? '{}' : raw);
}

function isTransformFrame(value: unknown): value is AnimationFrame {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
9 changes: 4 additions & 5 deletions packages/webgal/src/Core/gameScripts/setTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WebGAL } from '@/Core/WebGAL';
import { applyAnimationEndState, getAnimateDuration } from '../Modules/animationFunctions';
import { v4 as uuid } from 'uuid';
import { generateTimelineObj } from '@/Core/controller/stage/pixi/animations/timeline';
import { parseSetTransformFrame } from './parseTransformFrame';
/**
* 设置变换
* @param sentence
Expand All @@ -31,12 +32,10 @@ export const setTransform = (sentence: ISentence): IPerform => {

if (!parallel) WebGAL.gameplay.performController.unmountPerform(performInitName, true);

try {
const frame = JSON.parse(animationString) as AnimationFrame;
const frame = parseSetTransformFrame(animationString);
if (frame) {
animationObj = generateTransformAnimationObj(target, frame, duration, ease, writeFullEffect);
console.log('animationObj:', animationObj);
} catch (e) {
// 解析都错误了,歇逼吧
} else {
animationObj = [];
}

Expand Down
Loading