新的设置控制器

This commit is contained in:
unanmed 2023-08-02 23:17:10 +08:00
parent 7ce6acb664
commit a457438e97
4 changed files with 156 additions and 2 deletions

View File

@ -78,3 +78,4 @@ dam4.png ---- 存档 59
[x] 优化插件加载系统
[] 优化 Scroll 组件
[] 重写技能控制系统
[] 自定义快捷键

153
src/core/main/setting.ts Normal file
View File

@ -0,0 +1,153 @@
type MotaSettingType = boolean | number | MotaSetting;
interface MotaSettingItem<T extends MotaSettingType = MotaSettingType> {
name: string;
value: T;
defaults?: boolean | number;
step?: number;
display?: (value: T) => string;
}
class MotaSetting {
private list: Record<string, MotaSettingItem> = {};
special?: string;
/**
*
*/
markAsSpecial(sp: string) {
this.special = sp;
return this;
}
/**
*
* @param key
* @param value
*/
register(key: string, name: string, value: number, step?: number): this;
/**
*
* @param key
* @param value
*/
register(key: string, name: string, value: boolean | MotaSetting): this;
register(
key: string,
name: string,
value: MotaSettingType,
step: number = 1
) {
const setting: MotaSettingItem = {
name,
value
};
if (!(value instanceof MotaSetting)) setting.defaults = value;
if (typeof value === 'number') setting.step = step;
this.list[key] = setting;
return this;
}
/**
*
* @param key
*/
getSetting(key: string): Readonly<MotaSettingItem | null> {
const list = key.split('.');
return this.getSettingBy(list);
}
/**
*
* @param key
* @param value
*/
setValue(key: string, value: boolean | number) {
const setting = this.getSettingBy(key.split('.'));
if (typeof setting.value !== typeof value) {
throw new Error(
`Setting type mismatch on setting '${key}'.` +
`Expected: ${typeof setting.value}. Recieve: ${typeof value}`
);
}
setting.value = value;
}
/**
*
* @param key
* @param value
*/
addValue(key: string, value: number) {
const setting = this.getSettingBy(key.split('.'));
if (typeof setting.value !== 'number') {
throw new Error(
`Cannot execute addValue method on a non-number setting.` +
`Type expected: number. See: ${typeof setting.value}`
);
}
setting.value += value;
}
/**
*
* @param key
* @param func
*/
setDisplayFunc(key: string, func: (value: MotaSettingType) => string) {
const setting = this.getSettingBy(key.split('.'));
setting.display = func;
}
private getSettingBy(list: string[]) {
let now: MotaSetting = this;
for (let i = 0; i < list.length - 1; i++) {
const item = now.list[list[i]];
if (!(item instanceof MotaSetting)) {
throw new Error(
`Cannot get setting. The parent isn't a MotaSetting instance.` +
`Key: '${list.join('.')}'. Reading: '${list[i]}'`
);
}
now = item;
}
return now.list[list.at(-1)!] ?? null;
}
}
export const mainSetting = new MotaSetting();
// ----- 游戏的所有设置项
mainSetting
.register(
'screen',
'显示设置',
new MotaSetting()
.register('fullscreen', '全屏游戏', false)
.register('halo', '光环显示', true)
.register('frag', '打怪特效', true)
.register('itemDetail', '宝石血瓶显伤', true)
.register('transition', '界面动画', false)
.register('antiAlias', '抗锯齿', false)
.register('autoScale', '自动放缩', true)
.register('fontSize', '字体大小', 16, 1)
)
.register(
'action',
'操作设置',
new MotaSetting()
.register('autoSkill', '自动切换技能', true)
.register('fixed', '定点查看', true)
.register(
'hotkey',
'快捷键',
new MotaSetting().markAsSpecial('hotkey')
)
)
.register(
'utils',
'功能设置',
new MotaSetting().register('betterLoad', '优化加载', true)
);

View File

@ -54,7 +54,7 @@ const toShowAttrs: ComputedRef<[number | string, string, string][]> = computed(
[enemy.value.enemy.enemy.money, '金币', 'lightyellow'],
[enemy.value.enemy.enemy.exp, '经验', 'lawgreen'],
[enemy.value.critical, '临界', 'lightsalmon'],
[enemy.value.criticalDam, '减伤', 'lightpink'],
[enemy.value.criticalDam, '临界减伤', 'lightpink'],
[enemy.value.defDam, `${core.status.thisMap?.ratio ?? 1}`, 'cyan']
]
);

View File

@ -615,7 +615,7 @@ onMounted(async () => {
document.addEventListener('keyup', keyup);
map.addEventListener('touchstart', touchdown);
map.addEventListener('touchend', touchup);
map.addEventListener('touchend', touchmove);
map.addEventListener('touchmove', touchmove);
});
onUnmounted(() => {