Skip to content

Commit

Permalink
fix: optimize setVar
Browse files Browse the repository at this point in the history
  • Loading branch information
MakinoharaShoko committed Jul 8, 2024
1 parent cd61809 commit a6794f0
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions packages/webgal/src/Core/gameScripts/setVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export const setVar = (sentence: ISentence): IPerform => {
const valExp = sentence.content.split(/\s*=\s*/)[1];
if (valExp === 'random()') {
webgalStore.dispatch(targetReducerFunction({ key, value: Math.random() }));
} else if (valExp.match(/\$[.a-zA-Z]/)) {
webgalStore.dispatch(targetReducerFunction({ key, value: String(getValueFromState(valExp.trim())) }));
} else if (valExp.match(/[+\-*\/()]/)) {
// 如果包含加减乘除号,则运算
// 先取出运算表达式中的变量
Expand Down Expand Up @@ -88,24 +86,13 @@ export function getValueFromState(key: string) {
const stage = webgalStore.getState().stage;
const userData = webgalStore.getState().userData;
const _Merge = { ...stage, ...userData };
const is_baseVal = (_obj: object, _val: any, no_get = false) =>
['string', 'number', 'boolean'].includes(typeof (no_get ? _val : Reflect.get(_obj, _val)));
let _all: { [key: PropertyKey]: any } = {};
// 排除GameVar、globalGameVar因为这几个本来就可以获取
for (let propertyName in _Merge) {
if (propertyName === 'GameVar') continue;
if (propertyName === 'globalGameVar') continue;
if (propertyName) {
// @ts-ignore
_all['$' + propertyName] = get(_Merge, propertyName) as BaseVal;
}
}
if (stage.GameVar.hasOwnProperty(key)) {
ret = stage.GameVar[key];
} else if (userData.globalGameVar.hasOwnProperty(key)) {
ret = userData.globalGameVar[key];
} else if (key.startsWith('$') && is_baseVal(_all, compile(key)(_all), true)) {
ret = compile(key)(_all) as BaseVal;
} else if (key.startsWith('$')) {
const propertyKey = key.replace('$', '');
ret = get(_Merge, propertyKey, 0) as BaseVal;
}
return ret;
}

0 comments on commit a6794f0

Please sign in to comment.