mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-16 20:24:31 +08:00
存储管理系统
This commit is contained in:
1 parent
bf828bcacf
commit
05ed15c0a6
7 files changed
+110
-18
No files matched your search
+31
-13
@@ -4,6 +4,7 @@ import { transition } from '../../plugin/uiController';
|
||||
import { loading } from '../loader/load';
|
||||
import { hook } from './game';
|
||||
import { isMobile } from '../../plugin/use';
|
||||
import { GameStorage } from './storage';
|
||||
|
||||
type MotaSettingType = boolean | number | MotaSetting;
|
||||
|
||||
@@ -396,21 +397,38 @@ mainSetting
|
||||
.register('autoScale', '自动放缩', true)
|
||||
);
|
||||
|
||||
interface SettingStorage {
|
||||
showHalo: boolean;
|
||||
frag: boolean;
|
||||
itemDetail: boolean;
|
||||
transition: boolean;
|
||||
antiAlias: boolean;
|
||||
fontSize: number;
|
||||
smoothView: boolean;
|
||||
criticalGem: boolean;
|
||||
fixed: boolean;
|
||||
betterLoad: boolean;
|
||||
autoScale: boolean;
|
||||
}
|
||||
|
||||
const storage = new GameStorage<SettingStorage>(
|
||||
GameStorage.fromAncTe('setting')
|
||||
);
|
||||
|
||||
loading.once('coreInit', () => {
|
||||
const get = core.getLocalStorage;
|
||||
mainSetting.reset({
|
||||
'screen.fullscreen': false,
|
||||
'screen.halo': !!get('showHalo', true),
|
||||
'screen.frag': !!get('frag', true),
|
||||
'screen.itemDetail': !!get('itemDetail', true),
|
||||
'screen.transition': !!get('transition', false),
|
||||
'screen.antiAlias': !!get('antiAlias', false),
|
||||
'screen.fontSize': get('fontSize', 16),
|
||||
'screen.smoothView': !!get('smoothView', true),
|
||||
'screen.criticalGem': !!get('criticalGem', false),
|
||||
'action.fixed': !!get('fixed', true),
|
||||
'utils.betterLoad': !!get('betterLoad', true),
|
||||
'utils.autoScale': !!get('autoScale', true)
|
||||
'screen.fullscreen': !!document.fullscreenElement,
|
||||
'screen.halo': !!storage.getValue('showHalo', true),
|
||||
'screen.frag': !!storage.getValue('frag', true),
|
||||
'screen.itemDetail': !!storage.getValue('itemDetail', true),
|
||||
'screen.transition': !!storage.getValue('transition', false),
|
||||
'screen.antiAlias': !!storage.getValue('antiAlias', false),
|
||||
'screen.fontSize': storage.getValue('fontSize', 16),
|
||||
'screen.smoothView': !!storage.getValue('smoothView', true),
|
||||
'screen.criticalGem': !!storage.getValue('criticalGem', false),
|
||||
'action.fixed': !!storage.getValue('fixed', true),
|
||||
'utils.betterLoad': !!storage.getValue('betterLoad', true),
|
||||
'utils.autoScale': !!storage.getValue('autoScale', true)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
export class GameStorage<T> {
|
||||
static list: GameStorage<any>[] = [];
|
||||
|
||||
key: string;
|
||||
data!: T;
|
||||
|
||||
constructor(key: string) {
|
||||
this.key = key;
|
||||
this.read();
|
||||
GameStorage.list.push(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地存储读取
|
||||
*/
|
||||
read(): T {
|
||||
const data = localStorage.getItem(this.key) ?? '{}';
|
||||
return (this.data = JSON.parse(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入本地存储
|
||||
*/
|
||||
write() {
|
||||
localStorage.setItem(this.key, JSON.stringify(this.data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置存储的值
|
||||
* @param key 存储的名称
|
||||
* @param value 存储的值
|
||||
*/
|
||||
setValue<K extends keyof T>(key: K, value: T[K]) {
|
||||
this.data[key] = value;
|
||||
}
|
||||
|
||||
getValue<K extends keyof T>(key: K): T[K] | null;
|
||||
getValue<K extends keyof T>(key: K, defaults?: T[K]): T[K];
|
||||
getValue<K extends keyof T>(key: K, defaults?: T[K]) {
|
||||
if (this.data[key]) return this.data[key];
|
||||
else {
|
||||
if (defaults !== void 0) {
|
||||
this.data[key] = defaults;
|
||||
return defaults;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本游戏的存储键
|
||||
* @param key 存储名称
|
||||
*/
|
||||
static fromGame(key: string) {
|
||||
return `HumanBreak_${key}`;
|
||||
}
|
||||
|
||||
static fromAncTe(key: string) {
|
||||
return `AncTe@${key}`;
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('unload', () => {
|
||||
GameStorage.list.forEach(v => v.write());
|
||||
});
|
||||
+3
-3
@@ -1,5 +1,3 @@
|
||||
import { debounce } from 'lodash-es';
|
||||
|
||||
export default function init() {
|
||||
return { useDrag, useWheel, useUp, isMobile };
|
||||
}
|
||||
@@ -31,7 +29,9 @@ checkMobile();
|
||||
|
||||
function checkMobile() {
|
||||
if (isMobile && !alerted) {
|
||||
alert('手机端建议使用自带的浏览器进行游玩,并在进入游戏后开启全屏游玩');
|
||||
alert(
|
||||
'手机端建议使用自带的浏览器进行游玩,并在进入游戏后开启游戏内的全屏设置游玩'
|
||||
);
|
||||
alerted = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,3 +263,10 @@ export function pColor(color: string) {
|
||||
arr[3] ??= 1;
|
||||
return `rgba(${arr.join(',')})` as Color;
|
||||
}
|
||||
|
||||
export function deleteWith<T>(arr: T[], ele: T): T[] {
|
||||
const index = arr.indexOf(ele);
|
||||
if (index === -1) return arr;
|
||||
arr.splice(index, 1);
|
||||
return arr;
|
||||
}
|
||||
Vendored
+2
@@ -997,6 +997,8 @@ interface Core extends Pick<Main, CoreDataFromMain> {
|
||||
*/
|
||||
readonly flags: CoreFlags;
|
||||
|
||||
readonly firstData: FirstData;
|
||||
|
||||
/**
|
||||
* 获得所有楼层的信息
|
||||
* @example core.floors[core.status.floorId].events // 获得本楼层的所有自定义事件
|
||||
|
||||
Reference in new issue
Block a user