存储管理系统

This commit is contained in:
unanmed 2023-08-08 13:29:22 +08:00
parent bf828bcacf
commit 05ed15c0a6
7 changed files with 110 additions and 18 deletions

View File

@ -85,3 +85,4 @@ dam4.png ---- 存档 59
[] 优化 ui 控制系统 [] 优化 ui 控制系统
[] 优化游戏进程与渲染进程间的通讯 [] 优化游戏进程与渲染进程间的通讯
[] 优化资源分离,音乐放到 bgm 目录下 [] 优化资源分离,音乐放到 bgm 目录下
[] 一次性道具拾取与清怪

View File

@ -2811,8 +2811,7 @@ control.prototype.getStatus = function (name) {
////// 从status中获得属性如果不存在则从勇士属性中获取 ////// ////// 从status中获得属性如果不存在则从勇士属性中获取 //////
control.prototype.getStatusOrDefault = function (status, name) { control.prototype.getStatusOrDefault = function (status, name) {
if (status && name in status) return Math.floor(status[name]); // Deprecated. See src/plugin/game/hero.ts
return Math.floor(this.getStatus(name));
}; };
////// 获得勇士实际属性(增幅后的) ////// ////// 获得勇士实际属性(增幅后的) //////

View File

@ -4,6 +4,7 @@ import { transition } from '../../plugin/uiController';
import { loading } from '../loader/load'; import { loading } from '../loader/load';
import { hook } from './game'; import { hook } from './game';
import { isMobile } from '../../plugin/use'; import { isMobile } from '../../plugin/use';
import { GameStorage } from './storage';
type MotaSettingType = boolean | number | MotaSetting; type MotaSettingType = boolean | number | MotaSetting;
@ -396,21 +397,38 @@ mainSetting
.register('autoScale', '自动放缩', true) .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', () => { loading.once('coreInit', () => {
const get = core.getLocalStorage;
mainSetting.reset({ mainSetting.reset({
'screen.fullscreen': false, 'screen.fullscreen': !!document.fullscreenElement,
'screen.halo': !!get('showHalo', true), 'screen.halo': !!storage.getValue('showHalo', true),
'screen.frag': !!get('frag', true), 'screen.frag': !!storage.getValue('frag', true),
'screen.itemDetail': !!get('itemDetail', true), 'screen.itemDetail': !!storage.getValue('itemDetail', true),
'screen.transition': !!get('transition', false), 'screen.transition': !!storage.getValue('transition', false),
'screen.antiAlias': !!get('antiAlias', false), 'screen.antiAlias': !!storage.getValue('antiAlias', false),
'screen.fontSize': get('fontSize', 16), 'screen.fontSize': storage.getValue('fontSize', 16),
'screen.smoothView': !!get('smoothView', true), 'screen.smoothView': !!storage.getValue('smoothView', true),
'screen.criticalGem': !!get('criticalGem', false), 'screen.criticalGem': !!storage.getValue('criticalGem', false),
'action.fixed': !!get('fixed', true), 'action.fixed': !!storage.getValue('fixed', true),
'utils.betterLoad': !!get('betterLoad', true), 'utils.betterLoad': !!storage.getValue('betterLoad', true),
'utils.autoScale': !!get('autoScale', true) 'utils.autoScale': !!storage.getValue('autoScale', true)
}); });
}); });

65
src/core/main/storage.ts Normal file
View File

@ -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());
});

View File

@ -1,5 +1,3 @@
import { debounce } from 'lodash-es';
export default function init() { export default function init() {
return { useDrag, useWheel, useUp, isMobile }; return { useDrag, useWheel, useUp, isMobile };
} }
@ -31,7 +29,9 @@ checkMobile();
function checkMobile() { function checkMobile() {
if (isMobile && !alerted) { if (isMobile && !alerted) {
alert('手机端建议使用自带的浏览器进行游玩,并在进入游戏后开启全屏游玩'); alert(
'手机端建议使用自带的浏览器进行游玩,并在进入游戏后开启游戏内的全屏设置游玩'
);
alerted = true; alerted = true;
} }
} }

View File

@ -263,3 +263,10 @@ export function pColor(color: string) {
arr[3] ??= 1; arr[3] ??= 1;
return `rgba(${arr.join(',')})` as Color; 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;
}

2
src/types/core.d.ts vendored
View File

@ -997,6 +997,8 @@ interface Core extends Pick<Main, CoreDataFromMain> {
*/ */
readonly flags: CoreFlags; readonly flags: CoreFlags;
readonly firstData: FirstData;
/** /**
* *
* @example core.floors[core.status.floorId].events // 获得本楼层的所有自定义事件 * @example core.floors[core.status.floorId].events // 获得本楼层的所有自定义事件