mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-06-28 13:17:59 +08:00
refactor: 将 src/common src/core/common 移入 @motajs/legacy-common
This commit is contained in:
parent
a5e6f5b5f6
commit
dc502ddd64
6
packages/legacy-client/package.json
Normal file
6
packages/legacy-client/package.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "@motajs/legacy-client",
|
||||||
|
"dependencies": {
|
||||||
|
"@motajs/common": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
6
packages/legacy-common/package.json
Normal file
6
packages/legacy-common/package.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "@motajs/legacy-common",
|
||||||
|
"dependencies": {
|
||||||
|
"@motajs/common": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import { EventEmitter } from './eventEmitter';
|
import { EventEmitter } from 'eventemitter3';
|
||||||
|
|
||||||
interface DisposableEvent<T> {
|
interface DisposableEvent<T> {
|
||||||
active: (value: T) => void;
|
active: [value: T];
|
||||||
dispose: (value: T) => void;
|
dispose: [value: T];
|
||||||
destroy: () => void;
|
destroy: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
|
export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
|
209
packages/legacy-common/src/eventEmitter.ts
Normal file
209
packages/legacy-common/src/eventEmitter.ts
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
import { isNil } from 'lodash-es';
|
||||||
|
|
||||||
|
export type Callable = (...params: any) => any;
|
||||||
|
|
||||||
|
export interface Listener<T extends (...params: any) => any> {
|
||||||
|
fn: T;
|
||||||
|
once?: boolean;
|
||||||
|
immediate?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ListenerOptions {
|
||||||
|
once: boolean;
|
||||||
|
immediate: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
type EmitFn<F extends (...params: any) => any> = (
|
||||||
|
events: Listener<F>[],
|
||||||
|
...params: Parameters<F>
|
||||||
|
) => any;
|
||||||
|
|
||||||
|
type Key = number | string | symbol;
|
||||||
|
|
||||||
|
export class EventEmitter<T extends Record<keyof T, Callable> = {}> {
|
||||||
|
protected events: {
|
||||||
|
[x: Key]: Listener<Callable>[];
|
||||||
|
} = {};
|
||||||
|
private emitted: Set<string> = new Set();
|
||||||
|
|
||||||
|
protected emitter: {
|
||||||
|
[x: Key]: EmitFn<Callable> | undefined;
|
||||||
|
} = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听某个事件
|
||||||
|
* @param event 要监听的事件类型
|
||||||
|
* @param fn 触发事件时执行的函数
|
||||||
|
* @param options 监听选项
|
||||||
|
*/
|
||||||
|
on<K extends keyof T>(
|
||||||
|
event: K,
|
||||||
|
fn: T[K],
|
||||||
|
options?: Partial<ListenerOptions>
|
||||||
|
): void;
|
||||||
|
on(event: string, fn: Callable, options?: Partial<ListenerOptions>): void;
|
||||||
|
on(event: string, fn: Callable, options?: Partial<ListenerOptions>): void {
|
||||||
|
if (options?.immediate && this.emitted.has(event)) {
|
||||||
|
fn();
|
||||||
|
if (!options.once) {
|
||||||
|
this.events[event] ??= [];
|
||||||
|
this.events[event]?.push({
|
||||||
|
fn
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.events[event] ??= [];
|
||||||
|
this.events[event]?.push({
|
||||||
|
fn,
|
||||||
|
once: options?.once
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消监听某个事件
|
||||||
|
* @param event 要取消监听的事件类型
|
||||||
|
* @param fn 要取消监听的函数
|
||||||
|
*/
|
||||||
|
off<K extends keyof T>(event: K, fn: T[K]): void;
|
||||||
|
off(event: string, fn: Callable): void;
|
||||||
|
off(event: string, fn: Callable): void {
|
||||||
|
const index = this.events[event]?.findIndex(v => v.fn === fn);
|
||||||
|
if (index === -1 || index === void 0) return;
|
||||||
|
this.events[event]?.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听事件,并只触发一次
|
||||||
|
* @param event 要监听的事件
|
||||||
|
* @param fn 监听函数
|
||||||
|
*/
|
||||||
|
once<K extends keyof T>(event: K, fn: T[K]): void;
|
||||||
|
once(event: string, fn: Callable): void;
|
||||||
|
once(event: string, fn: Callable): void {
|
||||||
|
this.on(event, fn, { once: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发某个事件
|
||||||
|
* @param event 要触发的事件类型
|
||||||
|
* @param params 传入的参数
|
||||||
|
*/
|
||||||
|
emit<K extends keyof T>(
|
||||||
|
event: K,
|
||||||
|
...params: Parameters<T[K]>
|
||||||
|
): ReturnType<T[K]>[];
|
||||||
|
emit(event: string, ...params: any[]): any[];
|
||||||
|
emit<K extends keyof T, R>(event: K, ...params: Parameters<T[K]>): R;
|
||||||
|
emit<R>(event: string, ...params: any[]): R;
|
||||||
|
emit(event: string, ...params: any[]): any[] {
|
||||||
|
this.emitted.add(event);
|
||||||
|
const events = (this.events[event] ??= []);
|
||||||
|
if (this.emitter[event]) {
|
||||||
|
const returns = this.emitter[event]!(events, ...params);
|
||||||
|
this.events[event] = events.filter(v => !v.once);
|
||||||
|
return returns;
|
||||||
|
} else {
|
||||||
|
const returns: ReturnType<Callable>[] = [];
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
const e = events[i];
|
||||||
|
returns.push(e.fn(...(params as any)));
|
||||||
|
}
|
||||||
|
this.events[event] = events.filter(v => !v.once);
|
||||||
|
return returns;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置一个事件的执行器(emitter)
|
||||||
|
* @param event 要设置的事件
|
||||||
|
* @param fn 事件执行器,留空以清除触发器
|
||||||
|
*/
|
||||||
|
// @ts-expect-error 无法推导
|
||||||
|
setEmitter<K extends keyof T>(event: K, fn?: EmitFn<T[K]>): void;
|
||||||
|
setEmitter(event: string, fn?: EmitFn<Callable>): void;
|
||||||
|
setEmitter(event: string, fn?: EmitFn<Callable>): void {
|
||||||
|
this.emitter[event] = fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消监听所有的事件,删除所有监听函数
|
||||||
|
*/
|
||||||
|
removeAllListeners(): void;
|
||||||
|
/**
|
||||||
|
* 取消监听一个事件的所有函数
|
||||||
|
* @param event 要取消监听的事件
|
||||||
|
*/
|
||||||
|
removeAllListeners(event: keyof T): void;
|
||||||
|
/**
|
||||||
|
* 取消监听一个事件的所有函数
|
||||||
|
* @param event 要取消监听的事件
|
||||||
|
*/
|
||||||
|
removeAllListeners(event: string): void;
|
||||||
|
removeAllListeners(event?: string | keyof T) {
|
||||||
|
if (!isNil(event)) this.events[event] = [];
|
||||||
|
else this.events = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndexedSymbol = number | string | symbol;
|
||||||
|
|
||||||
|
export class IndexedEventEmitter<
|
||||||
|
T extends Record<keyof T, Callable>
|
||||||
|
> extends EventEmitter<T> {
|
||||||
|
private fnMap: {
|
||||||
|
[P in keyof T]?: Map<IndexedSymbol, T[P]>;
|
||||||
|
} = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听事件,并将函数与唯一标识符绑定
|
||||||
|
* @param event 事件类型
|
||||||
|
* @param symbol 监听函数的唯一标识符
|
||||||
|
* @param fn 监听函数
|
||||||
|
* @param options 监听配置
|
||||||
|
*/
|
||||||
|
onIndex<K extends keyof T>(
|
||||||
|
event: K,
|
||||||
|
symbol: IndexedSymbol,
|
||||||
|
fn: T[K],
|
||||||
|
options: Partial<ListenerOptions>
|
||||||
|
) {
|
||||||
|
const map = this.ensureMap(event);
|
||||||
|
if (map.has(symbol)) {
|
||||||
|
console.warn(
|
||||||
|
`监听${String(event)}出错:已存在标识符为${String(
|
||||||
|
symbol
|
||||||
|
)}的监听函数,已将其覆盖`
|
||||||
|
);
|
||||||
|
this.offIndex(event, symbol);
|
||||||
|
}
|
||||||
|
map.set(symbol, fn);
|
||||||
|
this.on(event, fn, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听事件,绑定唯一标识符,但监听函数只会执行一次
|
||||||
|
* @param event 要监听的事件
|
||||||
|
* @param symbol 监听函数的唯一标识符
|
||||||
|
* @param fn 监听函数
|
||||||
|
*/
|
||||||
|
onceIndex<K extends keyof T>(event: K, symbol: IndexedSymbol, fn: T[K]) {
|
||||||
|
this.onIndex(event, symbol, fn, { once: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消监听一个事件
|
||||||
|
* @param event 要取消监听的事件
|
||||||
|
* @param symbol 取消监听的函数的唯一标识符
|
||||||
|
*/
|
||||||
|
offIndex<K extends keyof T>(event: K, symbol: IndexedSymbol) {
|
||||||
|
const map = this.ensureMap(event);
|
||||||
|
const fn = map.get(symbol);
|
||||||
|
if (!fn) return;
|
||||||
|
this.off(event, fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ensureMap<K extends keyof T>(event: K) {
|
||||||
|
return this.fnMap[event] ?? new Map<IndexedSymbol, T[K]>();
|
||||||
|
}
|
||||||
|
}
|
3
packages/legacy-common/src/index.ts
Normal file
3
packages/legacy-common/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from './patch';
|
||||||
|
export * from './disposable';
|
||||||
|
export * from './eventEmitter';
|
6
packages/legacy-data/package.json
Normal file
6
packages/legacy-data/package.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "@motajs/legacy-data",
|
||||||
|
"dependencies": {
|
||||||
|
"@motajs/common": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
@ -1,75 +0,0 @@
|
|||||||
import { EventEmitter } from 'eventemitter3';
|
|
||||||
|
|
||||||
interface MonoStoreEvent<T> {
|
|
||||||
change: [before: T | undefined, after: T | undefined];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 一个可序列化存储结构,存储多个数据,但是同时只能有一个数据正在使用,适合那些需要切换的场景,
|
|
||||||
* 例如多勇士,多摄像机等,就可以通过此结构进行存储,然后同时只能操作一个勇士、摄像机等。
|
|
||||||
*/
|
|
||||||
export class MonoStore<T> extends EventEmitter<MonoStoreEvent<T>> {
|
|
||||||
list: Map<string, T> = new Map();
|
|
||||||
using?: T;
|
|
||||||
usingId?: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用指定id的数据
|
|
||||||
* @param id 要使用的数据id
|
|
||||||
*/
|
|
||||||
use(id: string) {
|
|
||||||
const before = this.using;
|
|
||||||
this.using = this.list.get(id);
|
|
||||||
this.usingId = id;
|
|
||||||
this.emit('change', before, this.using);
|
|
||||||
}
|
|
||||||
|
|
||||||
static toJSON(data: MonoStore<any>) {
|
|
||||||
return JSON.stringify({
|
|
||||||
now: data.usingId,
|
|
||||||
data: [...data.list]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromJSON<T>(data: string): MonoStore<T> {
|
|
||||||
const d = JSON.parse(data);
|
|
||||||
const arr: [string, T][] = d.data;
|
|
||||||
const store = new MonoStore<T>();
|
|
||||||
arr.forEach(([key, value]) => {
|
|
||||||
store.list.set(key, value);
|
|
||||||
});
|
|
||||||
if (d.now) store.use(d.now);
|
|
||||||
return store;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export namespace SerializeUtils {
|
|
||||||
interface StructSerializer<T> {
|
|
||||||
toJSON(data: T): string;
|
|
||||||
fromJSON(data: string): T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Map键值对序列化函数,如果使用Map作为state,那么需要使用这里面的函数作为序列化与反序列化函数
|
|
||||||
*/
|
|
||||||
export const mapSerializer: StructSerializer<Map<any, any>> = {
|
|
||||||
toJSON(data) {
|
|
||||||
return JSON.stringify([...data]);
|
|
||||||
},
|
|
||||||
fromJSON(data) {
|
|
||||||
return new Map(JSON.parse(data));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set集合序列化函数,如果使用Set作为state,那么需要使用这里面的函数作为序列化与反序列化函数
|
|
||||||
*/
|
|
||||||
export const setSerializer: StructSerializer<Set<any>> = {
|
|
||||||
toJSON(data) {
|
|
||||||
return JSON.stringify([...data]);
|
|
||||||
},
|
|
||||||
fromJSON(data) {
|
|
||||||
return new Set(JSON.parse(data));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
@ -101,7 +101,7 @@ export class EventEmitter<T extends Record<keyof T, Callable> = {}> {
|
|||||||
emit(event: string, ...params: any[]): any[] {
|
emit(event: string, ...params: any[]): any[] {
|
||||||
this.emitted.add(event);
|
this.emitted.add(event);
|
||||||
const events = (this.events[event] ??= []);
|
const events = (this.events[event] ??= []);
|
||||||
if (!!this.emitter[event]) {
|
if (this.emitter[event]) {
|
||||||
const returns = this.emitter[event]!(events, ...params);
|
const returns = this.emitter[event]!(events, ...params);
|
||||||
this.events[event] = events.filter(v => !v.once);
|
this.events[event] = events.filter(v => !v.once);
|
||||||
return returns;
|
return returns;
|
||||||
@ -121,7 +121,7 @@ export class EventEmitter<T extends Record<keyof T, Callable> = {}> {
|
|||||||
* @param event 要设置的事件
|
* @param event 要设置的事件
|
||||||
* @param fn 事件执行器,留空以清除触发器
|
* @param fn 事件执行器,留空以清除触发器
|
||||||
*/
|
*/
|
||||||
// @ts-ignore
|
// @ts-expect-error 无法推导
|
||||||
setEmitter<K extends keyof T>(event: K, fn?: EmitFn<T[K]>): void;
|
setEmitter<K extends keyof T>(event: K, fn?: EmitFn<T[K]>): void;
|
||||||
setEmitter(event: string, fn?: EmitFn<Callable>): void;
|
setEmitter(event: string, fn?: EmitFn<Callable>): void;
|
||||||
setEmitter(event: string, fn?: EmitFn<Callable>): void {
|
setEmitter(event: string, fn?: EmitFn<Callable>): void {
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import axios, { AxiosRequestConfig, ResponseType } from 'axios';
|
import axios, { AxiosRequestConfig, ResponseType } from 'axios';
|
||||||
import { Disposable } from './disposable';
|
import { Disposable, EventEmitter } from '@motajs/legacy-common';
|
||||||
import { logger } from '@motajs/common';
|
import { logger } from '@motajs/common';
|
||||||
import JSZip from 'jszip';
|
import JSZip from 'jszip';
|
||||||
import { EventEmitter } from './eventEmitter';
|
|
||||||
|
|
||||||
type ProgressFn = (now: number, total: number) => void;
|
type ProgressFn = (now: number, total: number) => void;
|
||||||
|
|
||||||
|
@ -55,7 +55,6 @@ import EnemySpecial from '@/panel/enemySpecial.vue';
|
|||||||
import EnemyTarget from '@/panel/enemyTarget.vue';
|
import EnemyTarget from '@/panel/enemyTarget.vue';
|
||||||
import KeyboardPanel from '@/panel/keyboard.vue';
|
import KeyboardPanel from '@/panel/keyboard.vue';
|
||||||
import { MCGenerator } from './main/layout';
|
import { MCGenerator } from './main/layout';
|
||||||
import { ResourceController } from './loader/controller';
|
|
||||||
import { logger } from '@motajs/common';
|
import { logger } from '@motajs/common';
|
||||||
import { Danmaku } from './main/custom/danmaku';
|
import { Danmaku } from './main/custom/danmaku';
|
||||||
import * as Shadow from './fx/shadow';
|
import * as Shadow from './fx/shadow';
|
||||||
@ -75,7 +74,6 @@ Mota.register('class', 'MotaSetting', MotaSetting);
|
|||||||
Mota.register('class', 'SettingDisplayer', SettingDisplayer);
|
Mota.register('class', 'SettingDisplayer', SettingDisplayer);
|
||||||
Mota.register('class', 'UiController', UiController);
|
Mota.register('class', 'UiController', UiController);
|
||||||
Mota.register('class', 'MComponent', MComponent);
|
Mota.register('class', 'MComponent', MComponent);
|
||||||
Mota.register('class', 'ResourceController', ResourceController);
|
|
||||||
Mota.register('class', 'Danmaku', Danmaku);
|
Mota.register('class', 'Danmaku', Danmaku);
|
||||||
// ----- 函数注册
|
// ----- 函数注册
|
||||||
Mota.register('fn', 'm', m);
|
Mota.register('fn', 'm', m);
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
import { EventEmitter } from '../common/eventEmitter';
|
|
||||||
|
|
||||||
interface ResourceControllerEvent<D = any, T = D> {
|
|
||||||
add: (uri: string, data: D) => void;
|
|
||||||
delete: (uri: string, content: T) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export abstract class ResourceController<
|
|
||||||
D,
|
|
||||||
T = D
|
|
||||||
> extends EventEmitter<ResourceControllerEvent> {
|
|
||||||
list: Record<string, T> = {};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加一个资源
|
|
||||||
* @param uri 资源uri
|
|
||||||
* @param data 资源数据
|
|
||||||
*/
|
|
||||||
abstract add(uri: string, data: D): void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除一个资源
|
|
||||||
* @param uri 要删除的资源的uri
|
|
||||||
*/
|
|
||||||
remove(uri: string) {
|
|
||||||
const content = this.list[uri];
|
|
||||||
delete this.list[uri];
|
|
||||||
// this.emit(uri, content);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
import BoxAnimate from '@/components/boxAnimate.vue';
|
import BoxAnimate from '@/components/boxAnimate.vue';
|
||||||
import { EventEmitter } from '@/core/common/eventEmitter';
|
import { EventEmitter } from 'eventemitter3';
|
||||||
import { logger } from '@motajs/common';
|
import { logger } from '@motajs/common';
|
||||||
import { ResponseBase } from '@/core/interface';
|
import { ResponseBase } from '@/core/interface';
|
||||||
import {
|
import {
|
||||||
@ -53,9 +53,9 @@ interface PostLikeResponse extends ResponseBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface DanmakuEvent {
|
interface DanmakuEvent {
|
||||||
showStart: (danmaku: Danmaku) => void;
|
showStart: [danmaku: Danmaku];
|
||||||
showEnd: (danmaku: Danmaku) => void;
|
showEnd: [danmaku: Danmaku];
|
||||||
like: (liked: boolean, danmaku: Danmaku) => void;
|
like: [liked: boolean, danmaku: Danmaku];
|
||||||
}
|
}
|
||||||
|
|
||||||
type SpecContentFn = (content: string, type: string) => VNode;
|
type SpecContentFn = (content: string, type: string) => VNode;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { EventEmitter, Listener } from '@/core/common/eventEmitter';
|
import { EventEmitter, Listener } from '@motajs/legacy-common';
|
||||||
import { KeyCode } from '@/plugin/keyCodes';
|
import { KeyCode } from '@/plugin/keyCodes';
|
||||||
import { gameKey } from './hotkey';
|
import { gameKey } from './hotkey';
|
||||||
import { unwarpBinary } from './hotkey';
|
import { unwarpBinary } from './hotkey';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { EventEmitter } from '@/core/common/eventEmitter';
|
import { EventEmitter } from '@motajs/legacy-common';
|
||||||
import { deleteWith, has } from '@/plugin/utils';
|
import { deleteWith, has } from '@/plugin/utils';
|
||||||
import { Component, nextTick, reactive, shallowReactive } from 'vue';
|
import { Component, nextTick, reactive, shallowReactive } from 'vue';
|
||||||
import { fixedUi } from '../init/ui';
|
import { fixedUi } from '../init/ui';
|
||||||
@ -11,7 +11,6 @@ import type {
|
|||||||
ToolbarItemMap,
|
ToolbarItemMap,
|
||||||
ToolbarItemType
|
ToolbarItemType
|
||||||
} from '../init/toolbar';
|
} from '../init/toolbar';
|
||||||
import { isMobile } from '@/plugin/use';
|
|
||||||
|
|
||||||
interface CustomToolbarEvent {
|
interface CustomToolbarEvent {
|
||||||
add: (item: ValueOf<ToolbarItemMap>) => void;
|
add: (item: ValueOf<ToolbarItemMap>) => void;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import { Component, shallowReactive } from 'vue';
|
import { Component, shallowReactive } from 'vue';
|
||||||
import { Callable, EventEmitter } from '../../common/eventEmitter';
|
import { EventEmitter } from '@motajs/legacy-common';
|
||||||
import { KeyCode } from '@/plugin/keyCodes';
|
|
||||||
import { Hotkey } from './hotkey';
|
import { Hotkey } from './hotkey';
|
||||||
import { generateBinary } from '@/plugin/utils';
|
|
||||||
|
|
||||||
interface FocusEvent<T> {
|
interface FocusEvent<T> {
|
||||||
focus: (before: T | null, after: T) => void;
|
focus: (before: T | null, after: T) => void;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { FunctionalComponent, reactive } from 'vue';
|
import { FunctionalComponent, reactive } from 'vue';
|
||||||
import { EventEmitter } from '../common/eventEmitter';
|
import { EventEmitter } from '@motajs/legacy-common';
|
||||||
import { GameStorage } from './storage';
|
import { GameStorage } from './storage';
|
||||||
import { has, triggerFullscreen } from '@/plugin/utils';
|
import { has, triggerFullscreen } from '@/plugin/utils';
|
||||||
import { createSettingComponents } from './init/settings';
|
import { createSettingComponents } from './init/settings';
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
import './system';
|
import './system';
|
||||||
import '../plugin/game/index';
|
import '../plugin/game/index';
|
||||||
import * as damage from './enemy/damage';
|
import * as damage from './enemy/damage';
|
||||||
import { EventEmitter, IndexedEventEmitter } from '@/core/common/eventEmitter';
|
import { EventEmitter, IndexedEventEmitter } from '@motajs/legacy-common';
|
||||||
import { specials } from './enemy/special';
|
import { specials } from './enemy/special';
|
||||||
import { gameListener, hook, loading } from './game';
|
import { gameListener, hook, loading } from './game';
|
||||||
import * as battle from './enemy/battle';
|
import * as battle from './enemy/battle';
|
||||||
import * as hero from './state/hero';
|
import * as hero from './state/hero';
|
||||||
import * as miscMechanism from './mechanism/misc';
|
import * as miscMechanism from './mechanism/misc';
|
||||||
import { registerPresetState } from './state/preset';
|
|
||||||
import { ItemState } from './state/item';
|
import { ItemState } from './state/item';
|
||||||
import {
|
import {
|
||||||
BlockMover,
|
BlockMover,
|
||||||
@ -52,8 +51,6 @@ loading.once('coreInit', () => {
|
|||||||
Mota.Plugin.init();
|
Mota.Plugin.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
registerPresetState();
|
|
||||||
|
|
||||||
window.addEventListener('beforeunload', () => {
|
window.addEventListener('beforeunload', () => {
|
||||||
core.checkAutosave();
|
core.checkAutosave();
|
||||||
});
|
});
|
||||||
|
@ -1,64 +0,0 @@
|
|||||||
import { MonoStore } from '@/common/struct';
|
|
||||||
import { GameState } from './state';
|
|
||||||
import { Hero, HeroState } from './hero';
|
|
||||||
|
|
||||||
export function registerPresetState() {
|
|
||||||
GameState.register<MonoStore<Hero<any>>>('hero', heroToJSON, heroFromJSON);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface HeroSave {
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
floorId: FloorIds;
|
|
||||||
id: string;
|
|
||||||
items: [AllIdsOf<'items'>, number][];
|
|
||||||
state: {
|
|
||||||
status: any;
|
|
||||||
buffable: (string | number | symbol)[];
|
|
||||||
buffMap: [string | number | symbol, number][];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface HeroSerializable {
|
|
||||||
now: string | null;
|
|
||||||
saves: HeroSave[];
|
|
||||||
}
|
|
||||||
|
|
||||||
function heroToJSON(data: MonoStore<Hero<any>>): string {
|
|
||||||
const now = data.usingId ?? null;
|
|
||||||
const saves: HeroSave[] = [...data.list.values()].map(v => {
|
|
||||||
return {
|
|
||||||
x: v.x,
|
|
||||||
y: v.y,
|
|
||||||
floorId: v.floorId,
|
|
||||||
id: v.id,
|
|
||||||
items: [...v.items],
|
|
||||||
state: {
|
|
||||||
status: v.state.status,
|
|
||||||
buffable: [...v.state.buffable],
|
|
||||||
buffMap: [...v.state.buffMap]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
const obj: HeroSerializable = {
|
|
||||||
now,
|
|
||||||
saves
|
|
||||||
};
|
|
||||||
return JSON.stringify(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
function heroFromJSON(data: string): MonoStore<Hero<any>> {
|
|
||||||
const obj: HeroSerializable = JSON.parse(data);
|
|
||||||
const store = new MonoStore<Hero<any>>();
|
|
||||||
const saves: [string, Hero<any>][] = obj.saves.map(v => {
|
|
||||||
const state = new HeroState(v.state.status);
|
|
||||||
v.state.buffable.forEach(v => state.buffable.add(v));
|
|
||||||
v.state.buffMap.forEach(v => state.buffMap.set(v[0], v[1]));
|
|
||||||
const hero = new Hero(v.id, v.x, v.y, v.floorId, state);
|
|
||||||
v.items.forEach(v => hero.items.set(v[0], v[1]));
|
|
||||||
return [hero.id, hero];
|
|
||||||
});
|
|
||||||
store.list = new Map(saves);
|
|
||||||
if (obj.now) store.use(obj.now);
|
|
||||||
return store;
|
|
||||||
}
|
|
@ -1,8 +1,8 @@
|
|||||||
import type { Disposable } from '@/core/common/disposable';
|
|
||||||
import type {
|
import type {
|
||||||
|
Disposable,
|
||||||
EventEmitter,
|
EventEmitter,
|
||||||
IndexedEventEmitter
|
IndexedEventEmitter
|
||||||
} from '@/core/common/eventEmitter';
|
} from '@motajs/legacy-common';
|
||||||
import type { loading } from './game';
|
import type { loading } from './game';
|
||||||
import type { Hotkey } from '@/core/main/custom/hotkey';
|
import type { Hotkey } from '@/core/main/custom/hotkey';
|
||||||
import type { Keyboard } from '@/core/main/custom/keyboard';
|
import type { Keyboard } from '@/core/main/custom/keyboard';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Patch, PatchClass } from '@/common/patch';
|
import { Patch, PatchClass } from '@motajs/legacy-common';
|
||||||
import { audioPlayer, bgmController, soundPlayer } from '../audio';
|
import { audioPlayer, bgmController, soundPlayer } from '../audio';
|
||||||
import { mainSetting } from '@/core/main/setting';
|
import { mainSetting } from '@/core/main/setting';
|
||||||
import { sleep } from 'mutate-animate';
|
import { sleep } from 'mutate-animate';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Patch } from '@/common/patch';
|
import { Patch } from '@motajs/legacy-common';
|
||||||
import { patchAudio } from './audio';
|
import { patchAudio } from './audio';
|
||||||
import { patchWeather } from './weather';
|
import { patchWeather } from './weather';
|
||||||
import { patchUI } from './ui';
|
import { patchUI } from './ui';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Patch, PatchClass } from '@/common/patch';
|
import { Patch, PatchClass } from '@motajs/legacy-common';
|
||||||
import { TipStore } from '../render/components/tip';
|
import { TipStore } from '../render/components/tip';
|
||||||
|
|
||||||
export function patchUI() {
|
export function patchUI() {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Patch, PatchClass } from '@/common/patch';
|
import { Patch, PatchClass } from '@motajs/legacy-common';
|
||||||
import { WeatherController } from '../weather';
|
import { WeatherController } from '../weather';
|
||||||
import { isNil } from 'lodash-es';
|
import { isNil } from 'lodash-es';
|
||||||
|
|
||||||
|
@ -5,6 +5,9 @@
|
|||||||
"@motajs/common": "workspace:*",
|
"@motajs/common": "workspace:*",
|
||||||
"@motajs/render": "workspace:*",
|
"@motajs/render": "workspace:*",
|
||||||
"@motajs/system": "workspace:*",
|
"@motajs/system": "workspace:*",
|
||||||
"@motajs/system-ui": "workspace:*"
|
"@motajs/system-ui": "workspace:*",
|
||||||
|
"@motajs/legacy-common": "workspace:*",
|
||||||
|
"@motajs/legacy-client": "workspace:*",
|
||||||
|
"@motajs/legacy-data": "workspace:*"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user