mirror of
https://github.com/motajs/template.git
synced 2026-11-03 07:24:30 +08:00
refactor: flag 系统
This commit is contained in:
1 parent
ebfe59b4f5
commit
ebae685a4e
15 files changed
+385
-21
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { Patch, PatchClass } from '@motajs/legacy-common';
|
||||
import { ICoreState } from '@user/data-state';
|
||||
|
||||
/**
|
||||
* Flag 系统 patch
|
||||
*/
|
||||
export function patchFlags(state: ICoreState) {
|
||||
const patch = new Patch(PatchClass.Control);
|
||||
const flags = state.flags;
|
||||
patch.add('setFlag', (name, value) => {
|
||||
logger.warn(56, 'core.setFlag', 'IFlagSystem');
|
||||
if (value === null || value === undefined) {
|
||||
flags.deleteField(name);
|
||||
} else {
|
||||
flags.setFieldValue(name, value);
|
||||
}
|
||||
});
|
||||
patch.add('getFlag', <T>(name: string, defaultValue?: T) => {
|
||||
logger.warn(56, 'core.getFlag', 'IFlagSystem');
|
||||
if (defaultValue === undefined) {
|
||||
return flags.getFieldValue<T>(name);
|
||||
} else {
|
||||
return flags.getFieldValueDefaults(name, defaultValue);
|
||||
}
|
||||
});
|
||||
patch.add('addFlag', (name, value) => {
|
||||
logger.warn(56, 'core.addFlag', 'IFlagSystem');
|
||||
if (typeof value !== 'number') return;
|
||||
flags.addFieldValue(name, value);
|
||||
});
|
||||
patch.add('hasFlag', name => {
|
||||
return flags.occupied(name);
|
||||
});
|
||||
patch.add('removeFlag', name => {
|
||||
flags.deleteField(name);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { hook } from '@user/data-base';
|
||||
import { ICoreState } from '@user/data-state';
|
||||
|
||||
export function patchHero(state: ICoreState) {
|
||||
hook.on('resetHero', hero => {
|
||||
// patch 旧样板的 core.status.hero,主要是为编辑器服务的
|
||||
const attr = state.hero.getModifiableAttribute();
|
||||
const proxy = new Proxy(hero, {
|
||||
set(target, p, newValue) {
|
||||
target[p] = newValue;
|
||||
// @ts-expect-error 旧样板无法处理此类型
|
||||
attr.setBaseAttribute(p, newValue);
|
||||
return true;
|
||||
},
|
||||
get(_, p) {
|
||||
// @ts-expect-error 旧样板无法处理此类型
|
||||
return attr.getBaseAttribute(p);
|
||||
}
|
||||
});
|
||||
core.status.hero = proxy;
|
||||
|
||||
// 不允许再使用旧样板的 flags 接口
|
||||
const flagsProxy = new Proxy(core.status.hero.flags, {
|
||||
set() {
|
||||
logger.error(54);
|
||||
return false;
|
||||
},
|
||||
get() {
|
||||
logger.error(54);
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
core.status.hero.flags = flagsProxy;
|
||||
window.flags = flagsProxy;
|
||||
});
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
import { ICoreState } from '@user/data-state';
|
||||
import { patchBattle } from './battle';
|
||||
import { patchDamage } from './damage';
|
||||
import { patchFlags } from './flag';
|
||||
import { patchHero } from './hero';
|
||||
|
||||
export function patchAll() {
|
||||
export function patchAll(state: ICoreState) {
|
||||
patchBattle();
|
||||
patchDamage();
|
||||
patchFlags(state);
|
||||
patchHero(state);
|
||||
}
|
||||
Reference in new issue
Block a user