diff --git a/packages/webgal/src/Core/controller/stage/playBgm.ts b/packages/webgal/src/Core/controller/stage/playBgm.ts index 07d72a7d2..b6e76c2e8 100644 --- a/packages/webgal/src/Core/controller/stage/playBgm.ts +++ b/packages/webgal/src/Core/controller/stage/playBgm.ts @@ -39,8 +39,10 @@ export function playBgm(url: string, enter = 0, volume = 100): void { clearTimeout(emptyBgmTimeout); webgalStore.dispatch(setStage({ key: 'bgm', value: { src: url, enter: enter, volume: volume } })); } - const audioElement = document.getElementById('currentBgm') as HTMLAudioElement; - if (audioElement.src) { - audioElement?.play(); - } + setTimeout(() => { + const audioElement = document.getElementById('currentBgm') as HTMLAudioElement; + if (audioElement.src) { + audioElement?.play(); + } + }, 0); } diff --git a/packages/webgal/src/Core/gameScripts/setVar.ts b/packages/webgal/src/Core/gameScripts/setVar.ts index e42780ab4..7cec5c2dc 100644 --- a/packages/webgal/src/Core/gameScripts/setVar.ts +++ b/packages/webgal/src/Core/gameScripts/setVar.ts @@ -4,7 +4,7 @@ import { webgalStore } from '@/store/store'; import { setStageVar } from '@/store/stageReducer'; import { logger } from '@/Core/util/logger'; import { compile } from 'angular-expressions'; -import { setGlobalVar } from '@/store/userDataReducer'; +import { setScriptManagedGlobalVar } from '@/store/userDataReducer'; import { ActionCreatorWithPayload } from '@reduxjs/toolkit'; import { ISetGameVar } from '@/store/stageInterface'; import { dumpToStorageFast } from '@/Core/controller/storage/storageController'; @@ -22,7 +22,7 @@ export const setVar = (sentence: ISentence): IPerform => { }); let targetReducerFunction: ActionCreatorWithPayload; if (setGlobal) { - targetReducerFunction = setGlobalVar; + targetReducerFunction = setScriptManagedGlobalVar; } else { targetReducerFunction = setStageVar; } diff --git a/packages/webgal/src/Core/util/coreInitialFunction/infoFetcher.ts b/packages/webgal/src/Core/util/coreInitialFunction/infoFetcher.ts index 10af13945..f807cd369 100644 --- a/packages/webgal/src/Core/util/coreInitialFunction/infoFetcher.ts +++ b/packages/webgal/src/Core/util/coreInitialFunction/infoFetcher.ts @@ -33,14 +33,14 @@ export const infoFetcher = (url: string) => { // 按照游戏的配置开始设置对应的状态 gameConfig.forEach((e) => { const { command, args } = e; - let res: any = args[0].trim(); - if (/^(true|false)$/g.test(args[0])) { - res = !!res; - } else if (/^[0-9]+\.?[0-9]+$/g.test(args[0])) { - res = Number(res); - } - if (!webgalStore.getState().userData.globalGameVar?.[command]) { - logger.info('首次写入 Game Config'); + if (args.length > 0) { + let res: any = args[0].trim(); + if (/^(true|false)$/g.test(args[0])) { + res = !!res; + } else if (/^[0-9]+\.?[0-9]+$/g.test(args[0])) { + res = Number(res); + } + dispatch( setGlobalVar({ key: command, diff --git a/packages/webgal/src/store/userDataInterface.ts b/packages/webgal/src/store/userDataInterface.ts index 585568406..626544ded 100644 --- a/packages/webgal/src/store/userDataInterface.ts +++ b/packages/webgal/src/store/userDataInterface.ts @@ -92,6 +92,7 @@ export interface IAppreciation { * @interface IUserData 用户数据接口 */ export interface IUserData { + scriptManagedGlobalVar: string[]; globalGameVar: IGameVar; // 不跟随存档的全局变量 optionData: IOptionData; // 用户设置选项数据 appreciationData: IAppreciation; diff --git a/packages/webgal/src/store/userDataReducer.ts b/packages/webgal/src/store/userDataReducer.ts index 7efc5b8de..a70827073 100644 --- a/packages/webgal/src/store/userDataReducer.ts +++ b/packages/webgal/src/store/userDataReducer.ts @@ -41,6 +41,7 @@ const initialOptionSet: IOptionData = { // 初始化用户数据 export const initState: IUserData = { optionData: initialOptionSet, + scriptManagedGlobalVar: [], globalGameVar: {}, appreciationData: { bgm: [], @@ -114,7 +115,17 @@ const userDataSlice = createSlice({ * @param action 要改变或添加的变量 */ setGlobalVar: (state, action: PayloadAction) => { + const isRegistedInUserData = state.scriptManagedGlobalVar.findIndex((key) => key === action.payload.key) >= 0; + if (!isRegistedInUserData) { + state.globalGameVar[action.payload.key] = action.payload.value; + } + }, + setScriptManagedGlobalVar: (state, action: PayloadAction) => { + const isRegistedInUserData = state.scriptManagedGlobalVar.findIndex((key) => key === action.payload.key) >= 0; state.globalGameVar[action.payload.key] = action.payload.value; + if (!isRegistedInUserData) { + state.scriptManagedGlobalVar.push(action.payload.key); + } }, /** * 设置存档/读档页面 @@ -138,6 +149,7 @@ export const { resetUserData, setOptionData, setGlobalVar, + setScriptManagedGlobalVar, setSlPage, unlockCgInUserData, unlockBgmInUserData,