Skip to content

Commit fc6be62

Browse files
Merge pull request OpenWebGAL#442 from hundun000/dev-hundun/globalSetVar
feat: global setVar
2 parents c852020 + 9e3aacd commit fc6be62

File tree

9 files changed

+76
-37
lines changed

9 files changed

+76
-37
lines changed

packages/webgal/src/Core/controller/gamePlay/scriptExecutor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ export const scriptExecutor = () => {
135135
setTimeout(() => {
136136
// 同步当前舞台数据
137137
currentStageState = webgalStore.getState().stage;
138-
logger.debug('本条语句执行结果', currentStageState);
138+
const allState = {
139+
currentStageState: currentStageState,
140+
globalGameVar: webgalStore.getState().userData.globalGameVar,
141+
};
142+
logger.debug('本条语句执行结果', allState);
139143
// 保存 backlog
140144
if (isSaveBacklog) {
141145
// WebGAL.backlogManager.isSaveBacklogNext = true;

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,34 @@ import { webgalStore } from '@/store/store';
44
import { setStageVar } from '@/store/stageReducer';
55
import { logger } from '@/Core/util/logger';
66
import { compile } from 'angular-expressions';
7+
import { setGlobalVar } from '@/store/userDataReducer';
8+
import { ActionCreatorWithPayload } from '@reduxjs/toolkit';
9+
import { ISetGameVar } from '@/store/stageInterface';
10+
import { syncStorageFast } from '@/Core/controller/storage/storageController';
711

812
/**
913
* 设置变量
1014
* @param sentence
1115
*/
1216
export const setVar = (sentence: ISentence): IPerform => {
17+
let setGlobal = false;
18+
sentence.args.forEach((e) => {
19+
if (e.key === 'global') {
20+
setGlobal = true;
21+
}
22+
});
23+
let targetReducerFunction: ActionCreatorWithPayload<ISetGameVar, string>;
24+
if (setGlobal) {
25+
targetReducerFunction = setGlobalVar;
26+
} else {
27+
targetReducerFunction = setStageVar;
28+
}
1329
// 先把表达式拆分为变量名和赋值语句
1430
if (sentence.content.match(/=/)) {
1531
const key = sentence.content.split(/=/)[0];
1632
const valExp = sentence.content.split(/=/)[1];
1733
if (valExp === 'random()') {
18-
webgalStore.dispatch(setStageVar({ key, value: Math.random() }));
34+
webgalStore.dispatch(targetReducerFunction({ key, value: Math.random() }));
1935
} else if (valExp.match(/[+\-*\/()]/)) {
2036
// 如果包含加减乘除号,则运算
2137
// 先取出运算表达式中的变量
@@ -30,20 +46,25 @@ export const setVar = (sentence: ISentence): IPerform => {
3046
.reduce((pre, curr) => pre + curr, '');
3147
const exp = compile(valExp2);
3248
const result = exp();
33-
webgalStore.dispatch(setStageVar({ key, value: result }));
49+
webgalStore.dispatch(targetReducerFunction({ key, value: result }));
3450
} else if (valExp.match(/true|false/)) {
3551
if (valExp.match(/true/)) {
36-
webgalStore.dispatch(setStageVar({ key, value: true }));
52+
webgalStore.dispatch(targetReducerFunction({ key, value: true }));
3753
}
3854
if (valExp.match(/false/)) {
39-
webgalStore.dispatch(setStageVar({ key, value: false }));
55+
webgalStore.dispatch(targetReducerFunction({ key, value: false }));
4056
}
4157
} else {
4258
if (!isNaN(Number(valExp))) {
43-
webgalStore.dispatch(setStageVar({ key, value: Number(valExp) }));
44-
} else webgalStore.dispatch(setStageVar({ key, value: valExp }));
59+
webgalStore.dispatch(targetReducerFunction({ key, value: Number(valExp) }));
60+
} else webgalStore.dispatch(targetReducerFunction({ key, value: valExp }));
61+
}
62+
if (setGlobal) {
63+
logger.debug('设置全局变量:', { key, value: webgalStore.getState().userData.globalGameVar[key] });
64+
syncStorageFast();
65+
} else {
66+
logger.debug('设置变量:', { key, value: webgalStore.getState().stage.GameVar[key] });
4567
}
46-
logger.debug('设置变量:', { key, value: webgalStore.getState().stage.GameVar[key] });
4768
}
4869
return {
4970
performName: 'none',
@@ -60,6 +81,8 @@ export function getValueFromState(key: string) {
6081
let ret: number | string | boolean = 0;
6182
if (webgalStore.getState().stage.GameVar.hasOwnProperty(key)) {
6283
ret = webgalStore.getState().stage.GameVar[key];
84+
} else if (webgalStore.getState().userData.globalGameVar.hasOwnProperty(key)) {
85+
ret = webgalStore.getState().userData.globalGameVar[key];
6386
}
6487
return ret;
6588
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ export const showVars = (sentence: ISentence): IPerform => {
1717
const userDataState = webgalStore.getState().userData;
1818
const dispatch = webgalStore.dispatch;
1919
// 设置文本显示
20-
dispatch(setStage({ key: 'showText', value: JSON.stringify(stageState.GameVar) }));
20+
const allVar = {
21+
stageGameVar: stageState.GameVar,
22+
globalGameVar: userDataState.globalGameVar,
23+
};
24+
dispatch(setStage({ key: 'showText', value: JSON.stringify(allVar) }));
2125
dispatch(setStage({ key: 'showName', value: '展示变量' }));
22-
logger.debug('展示变量:', stageState.GameVar);
26+
logger.debug('展示变量:', allVar);
2327
setTimeout(() => {
2428
WebGAL.eventBus.emit('text-settle');
2529
}, 0);
2630
const performInitName: string = getRandomPerformName();
27-
const textDelay = PERFORM_CONFIG.textInitialDelay - 20 * userDataState.optionData.textSpeed;
2831
const endDelay = 750 - userDataState.optionData.textSpeed * 250;
2932
return {
3033
performName: performInitName,
31-
duration: sentence.content.length * textDelay + endDelay,
34+
duration: endDelay,
3235
isHoldOn: false,
3336
stopFunction: () => {
3437
WebGAL.eventBus.emit('text-settle');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import '../../gameScripts/pixi/performs/cherryBlossoms';
22
import '../../gameScripts/pixi/performs/rain';
3-
import '../../gameScripts/pixi/performs/snow';
3+
import '../../gameScripts/pixi/performs/snow';

packages/webgal/src/Stage/Stage.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC } from 'react';
1+
import React, { FC } from 'react';
22
import styles from './stage.module.scss';
33
import { TextBox } from './TextBox/TextBox';
44
import { AudioContainer } from './AudioContainer/AudioContainer';
@@ -17,15 +17,17 @@ import { WebGAL } from '@/Core/WebGAL';
1717
// import OldStage from '@/Components/Stage/OldStage/OldStage';
1818

1919
function inTextBox(event: React.MouseEvent) {
20-
const tb = document.getElementById("textBoxMain")
20+
const tb = document.getElementById('textBoxMain');
2121
if (!tb) {
22-
return false
22+
return false;
2323
}
24-
var bounds = tb.getBoundingClientRect();
25-
return event.clientX > bounds.left &&
24+
let bounds = tb.getBoundingClientRect();
25+
return (
26+
event.clientX > bounds.left &&
2627
event.clientX < bounds.right &&
2728
event.clientY > bounds.top &&
2829
event.clientY < bounds.bottom
30+
);
2931
}
3032

3133
export const Stage: FC = () => {
@@ -37,12 +39,12 @@ export const Stage: FC = () => {
3739

3840
const checkPosition = (event: React.MouseEvent) => {
3941
if (!GUIState.controlsVisibility && inTextBox(event)) {
40-
dispatch(setVisibility({ component: 'controlsVisibility', visibility: true }))
42+
dispatch(setVisibility({ component: 'controlsVisibility', visibility: true }));
4143
}
4244
if (GUIState.controlsVisibility && !inTextBox(event)) {
43-
dispatch(setVisibility({ component: 'controlsVisibility', visibility: false }))
45+
dispatch(setVisibility({ component: 'controlsVisibility', visibility: false }));
4446
}
45-
}
47+
};
4648

4749
return (
4850
<div className={styles.MainStage_main}>
@@ -70,9 +72,7 @@ export const Stage: FC = () => {
7072
}}
7173
id="FullScreenClick"
7274
style={{ width: '100%', height: '100%', position: 'absolute', zIndex: '12', top: '0' }}
73-
onMouseMove={
74-
(e) => !GUIState.showControls && checkPosition(e)
75-
}
75+
onMouseMove={(e) => !GUIState.showControls && checkPosition(e)}
7676
/>
7777
<IntroContainer />
7878
</div>

packages/webgal/src/UI/BottomControlPanel/BottomControlPanel.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
DoubleDown,
1313
DoubleUp,
1414
Lock,
15-
Unlock
15+
Unlock,
1616
} from '@icon-park/react';
1717
import styles from './bottomControlPanel.module.scss';
1818
import { switchAuto } from '@/Core/controller/gamePlay/autoPlay';
@@ -77,10 +77,7 @@ export const BottomControlPanel = () => {
7777
// <div className={styles.ToCenter}>
7878
<>
7979
{GUIStore.showTextBox && stageState.enableFilm === '' && (
80-
<div
81-
className={styles.main}
82-
style={{visibility: GUIStore.controlsVisibility ? "visible" : "hidden"}}
83-
>
80+
<div className={styles.main} style={{ visibility: GUIStore.controlsVisibility ? 'visible' : 'hidden' }}>
8481
{GUIStore.showTextBox && (
8582
<span
8683
className={styles.singleButton}
@@ -307,11 +304,11 @@ export const BottomControlPanel = () => {
307304
}}
308305
onMouseEnter={playSeEnter}
309306
>
310-
{
311-
GUIStore.showControls ?
312-
(<Lock className={styles.button} theme="outline" size={size} fill="#f5f5f7" strokeWidth={strokeWidth} />) :
313-
(<Unlock className={styles.button} theme="outline" size={size} fill="#f5f5f7" strokeWidth={strokeWidth} />)
314-
}
307+
{GUIStore.showControls ? (
308+
<Lock className={styles.button} theme="outline" size={size} fill="#f5f5f7" strokeWidth={strokeWidth} />
309+
) : (
310+
<Unlock className={styles.button} theme="outline" size={size} fill="#f5f5f7" strokeWidth={strokeWidth} />
311+
)}
315312
</span>
316313
</div>
317314
)}

packages/webgal/src/hooks/useValue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useState} from "react";
1+
import { useState } from 'react';
22

33
export function useValue<T>(initialState: T) {
44
const [value, setValue] = useState<T>(initialState);
@@ -13,6 +13,6 @@ export function useValue<T>(initialState: T) {
1313
},
1414
set value(newValue) {
1515
this.set(newValue);
16-
}
16+
},
1717
};
1818
}

packages/webgal/src/store/userDataInterface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IStageState } from './stageInterface';
1+
import { IGameVar, IStageState } from './stageInterface';
22
import { language } from '@/config/language';
33
import { IBacklogItem } from '@/Core/Modules/backlog';
44
import { ISceneEntry } from '@/Core/Modules/scene';
@@ -88,6 +88,7 @@ export interface IAppreciation {
8888
export interface IUserData {
8989
saveData: Array<ISaveData>; // 用户存档数据
9090
quickSaveData: ISaveData | null;
91+
globalGameVar: IGameVar; // 不跟随存档的全局变量
9192
optionData: IOptionData; // 用户设置选项数据
9293
appreciationData: IAppreciation;
9394
}

packages/webgal/src/store/userDataReducer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '@/store/userDataInterface';
1919
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
2020
import cloneDeep from 'lodash/cloneDeep';
21+
import { ISetGameVar } from './stageInterface';
2122

2223
const initialOptionSet: IOptionData = {
2324
slPage: 1,
@@ -39,6 +40,7 @@ const initialOptionSet: IOptionData = {
3940
export const initState: IUserData = {
4041
saveData: [],
4142
optionData: initialOptionSet,
43+
globalGameVar: {},
4244
appreciationData: {
4345
bgm: [],
4446
cg: [],
@@ -106,6 +108,14 @@ const userDataSlice = createSlice({
106108
const { key, value } = action.payload;
107109
(state.optionData as any)[key] = value;
108110
},
111+
/**
112+
* 修改不跟随存档的全局变量
113+
* @param state 当前状态
114+
* @param action 要改变或添加的变量
115+
*/
116+
setGlobalVar: (state, action: PayloadAction<ISetGameVar>) => {
117+
state.globalGameVar[action.payload.key] = action.payload.value;
118+
},
109119
/**
110120
* 设置存档/读档页面
111121
* @param state
@@ -133,6 +143,7 @@ export const {
133143
setUserData,
134144
resetUserData,
135145
setOptionData,
146+
setGlobalVar,
136147
setSlPage,
137148
unlockCgInUserData,
138149
unlockBgmInUserData,

0 commit comments

Comments
 (0)