refactor: 接口与进程通讯优化

This commit is contained in:
AncTe committed 2024-01-24 21:32:49 +08:00
1 parent b407e7b80a
commit 5119227a41
41 files changed
+506 -181

No files matched your search

+2
View File
@@ -70,3 +70,5 @@ export class BgmController extends ResourceController<HTMLAudioElement> {
return this.list[`bgms.${id}`];
}
}
export const bgm = new BgmController();
+2
View File
@@ -215,3 +215,5 @@ export class SoundController extends ResourceController<
}
}
}
export const sound = new SoundController();
+60 -39
View File
@@ -1,15 +1,24 @@
import { BgmController } from './audio/bgm';
import { SoundController } from './audio/sound';
import { BgmController, bgm } from './audio/bgm';
import { SoundController, sound } from './audio/sound';
import { EventEmitter } from './common/eventEmitter';
import { loading, readyAllResource } from './loader/load';
import { ResourceStore, ResourceType } from './loader/resource';
import {
ResourceStore,
ResourceType,
resource,
zipResource
} from './loader/resource';
import { UiController } from './main/custom/ui';
import { GameEvent, hook } from './main/game';
import { fixedUi, mainUi } from './main/init/ui';
import { GameEvent, gameListener, hook } from './main/game';
import { GameStorage } from './main/storage';
import { resolvePlugin } from './plugin';
// import { resolvePlugin } from './plugin';
import './main/init/';
import './main/custom/toolbar';
import { fixedUi, mainUi } from './main/init/ui';
import { gameKey } from './main/init/hotkey';
import { mainSetting, settingStorage } from './main/setting';
import { isMobile } from '../plugin/use';
import { KeyCode } from '@/plugin/keyCodes';
interface AncTePlugin {
pop: ReturnType<typeof import('../plugin/pop').default>;
@@ -33,42 +42,54 @@ interface AncTePlugin {
frag: ReturnType<typeof import('../plugin/fx/frag').default>;
}
export interface Mota {
sound: SoundController;
/** 游戏资源 */
resource: ResourceStore<Exclude<ResourceType, 'zip'>>;
zipResource: ResourceStore<'zip'>;
bgm: BgmController;
plugin: AncTePlugin;
game: {
hook: EventEmitter<GameEvent>;
storage: typeof GameStorage;
};
ui: {
main: UiController;
fixed: UiController;
};
}
// export interface Mota {
// sound: SoundController;
// /** 游戏资源 */
// resource: ResourceStore<Exclude<ResourceType, 'zip'>>;
// zipResource: ResourceStore<'zip'>;
// bgm: BgmController;
// plugin: AncTePlugin;
// hook: EventEmitter<GameEvent>;
// classes: {
// storage: typeof GameStorage;
// };
// ui: {
// main: UiController;
// fixed: UiController;
// };
// }
function ready() {
window.mota = {
bgm: new BgmController(),
resource: new ResourceStore(),
zipResource: new ResourceStore(),
sound: new SoundController(),
// @ts-ignore
plugin: {},
game: {
hook,
storage: GameStorage
},
ui: {
main: mainUi,
fixed: fixedUi
}
};
// window.mota = {
// bgm: new BgmController(),
// resource: new ResourceStore(),
// zipResource: new ResourceStore(),
// sound: new SoundController(),
// // @ts-ignore
// plugin: {},
// hook,
// storage: GameStorage,
// ui: {
// main: mainUi,
// fixed: fixedUi
// }
// };
readyAllResource();
loading.once('coreInit', resolvePlugin);
// loading.once('coreInit', resolvePlugin);
}
ready();
Mota.register('var', 'mainUi', mainUi);
Mota.register('var', 'fixedUi', fixedUi);
Mota.register('var', 'hook', hook);
Mota.register('var', 'gameListener', gameListener);
Mota.register('var', 'bgm', bgm);
Mota.register('var', 'sound', sound);
Mota.register('var', 'gameKey', gameKey);
Mota.register('var', 'loading', loading);
Mota.register('var', 'mainSetting', mainSetting);
Mota.register('var', 'KeyCode', KeyCode);
Mota.register('var', 'resource', resource);
Mota.register('var', 'zipResource', zipResource);
Mota.register('var', 'settingStorage', settingStorage);
+11 -6
View File
@@ -1,6 +1,11 @@
import resource from '@/data/resource.json';
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
import { Resource, getTypeByResource } from './resource';
import {
Resource,
getTypeByResource,
zipResource,
resource as res
} from './resource';
interface GameLoadEvent extends EmitableEvent {
coreLoaded: () => void;
@@ -19,9 +24,9 @@ export function readyAllResource() {
info.resource.forEach(v => {
const type = getTypeByResource(v);
if (type === 'zip') {
mota.zipResource.set(v, new Resource(v, 'zip'));
zipResource.set(v, new Resource(v, 'zip'));
} else {
mota.resource.set(v, new Resource(v, type));
res.set(v, new Resource(v, type));
}
});
}
@@ -35,13 +40,13 @@ export function readyAllResource() {
loadData.forEach(v => {
const type = getTypeByResource(v);
if (type !== 'zip') {
mota.resource.set(v, new Resource(v, type));
res.set(v, new Resource(v, type));
}
});
mota.resource.forEach(v => v.active());
res.forEach(v => v.active());
loading.once('coreInit', () => {
const animates = new Resource('__all_animates__', 'text');
mota.resource.set('__all_animates__', animates);
res.set('__all_animates__', animates);
animates.active();
});
}
+6 -3
View File
@@ -54,7 +54,7 @@ export class Resource<
protected onLoadStart(v?: ResourceData[T]) {
if (this.format === 'bgm') {
// bgm 单独处理,因为它可以边播放边加载
mota.bgm.add(this.uri, v!);
Mota.require('var', 'bgm').add(this.uri, v!);
}
}
@@ -63,7 +63,7 @@ export class Resource<
if (this.type === 'fonts') {
document.fonts.add(new FontFace(this.name, v as ArrayBuffer));
} else if (this.type === 'sounds') {
mota.sound.add(this.uri, v as ArrayBuffer);
Mota.require('var', 'sound').add(this.uri, v as ArrayBuffer);
} else if (this.type === 'images') {
const name = `${this.name}${this.ext}` as ImageIds;
loading.on(
@@ -111,7 +111,7 @@ export class Resource<
const id = `${base}.${name}`;
const type = getTypeByResource(id) as NonZipResource;
const format = getZipFormatByType(type);
mota.resource.set(
resource.set(
id,
new Resource(id, type).setData(file.async(format))
);
@@ -377,3 +377,6 @@ export function getZipFormatByType(type: ResourceType): 'arraybuffer' | 'text' {
if (type === 'text' || type === 'json') return 'text';
else return 'arraybuffer';
}
export const resource = new ResourceStore();
export const zipResource = new ResourceStore();
+1
View File
@@ -34,6 +34,7 @@ const closeFixed = () => {
});
};
// todo: 应当在这里实现查看临界与特殊属性的功能
export let hovered: Block | null;
gameListener.on('hoverBlock', block => {
-1
View File
@@ -1,3 +1,2 @@
import './fixed';
import './hotkey';
import './keyboard';
+49 -16
View File
@@ -3,7 +3,7 @@ import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
import { loading } from '../loader/load';
import { hook } from './game';
import { GameStorage } from './storage';
import { triggerFullscreen } from '@/plugin/utils';
import { has, triggerFullscreen } from '@/plugin/utils';
import { createSettingComponents } from './init/settings';
export interface SettingComponentProps {
@@ -46,6 +46,8 @@ export interface SettingDisplayInfo {
const COM = createSettingComponents();
export class MotaSetting extends EventEmitter<SettingEvent> {
static noStorage: string[] = [];
readonly list: Record<string, MotaSettingItem> = {};
/**
@@ -145,6 +147,31 @@ export class MotaSetting extends EventEmitter<SettingEvent> {
this.emit('valueChange', key, old, value);
}
/**
* 获取一个设置的值,如果获取到的是一个MotaSetting实例,那么返回undefined
* @param key 要获取的设置
*/
getValue(key: string): boolean | number | undefined;
/**
* 获取一个设置的值,如果获取到的是一个MotaSetting实例,那么返回defaultValue
* @param key 要获取的设置
* @param defaultValue 设置的默认值
*/
getValue<T extends boolean | number>(key: string, defaultValue: T): T;
getValue<T extends boolean | number>(
key: string,
defaultValue?: T
): T | undefined {
const setting = this.getSetting(key);
if (!has(setting) && !has(defaultValue)) return void 0;
if (setting instanceof MotaSetting) {
if (has(setting)) return defaultValue;
return void 0;
} else {
return has(setting) ? (setting.value as T) : (defaultValue as T);
}
}
/**
* 设置一个设置的值显示函数
* @param key 要设置的设置的键
@@ -277,8 +304,10 @@ export class SettingDisplayer extends EventEmitter<SettingDisplayerEvent> {
// todo: 优化存储方式
export const mainSetting = new MotaSetting();
// 添加不参与全局存储的设置
MotaSetting.noStorage.push('action.autoSkill', 'screen.fullscreen');
interface SettingStorage {
export interface SettingStorage {
showHalo: boolean;
frag: boolean;
itemDetail: boolean;
@@ -298,8 +327,14 @@ const storage = new GameStorage<SettingStorage>(
GameStorage.fromAuthor('AncTe', 'setting')
);
export { storage as settingStorage };
// ----- 监听设置修改
mainSetting.on('valueChange', (key, n, o) => {
if (!MotaSetting.noStorage.includes(key)) {
storage.setValue(key, n);
}
const [root, setting] = key.split('.');
if (root === 'screen') {
@@ -323,25 +358,23 @@ function handleScreenSetting<T extends number | boolean>(
triggerFullscreen(n as boolean);
} else if (key === 'halo') {
// 光环
core.setLocalStorage('showHalo', n);
// core.setLocalStorage('showHalo', n);
} else if (key === 'frag') {
// 打怪特效
core.setLocalStorage('frag', n);
// core.setLocalStorage('frag', n);
} else if (key === 'itemDetail') {
// 宝石血瓶显伤
core.setLocalStorage('itemDetail', n);
// core.setLocalStorage('itemDetail', n);
} else if (key === 'heroDetail') {
// 勇士显伤
core.setLocalStorage('heroDetail', n);
// core.setLocalStorage('heroDetail', n);
core.drawHero();
// storage.setValue('heroDetail', n as boolean);
} else if (key === 'transition') {
// 界面动画
core.setLocalStorage('transition', n);
// transition.value = n as boolean;
// core.setLocalStorage('transition', n);
} else if (key === 'antiAlias') {
// 抗锯齿
core.setLocalStorage('antiAlias', n);
// core.setLocalStorage('antiAlias', n);
for (const canvas of core.dom.gameCanvas) {
if (core.domStyle.hdCanvas.includes(canvas.id)) continue;
if (n) {
@@ -352,12 +385,12 @@ function handleScreenSetting<T extends number | boolean>(
}
} else if (key === 'fontSize') {
// 字体大小
core.setLocalStorage('fontSize', n);
// core.setLocalStorage('fontSize', n);
root.style.fontSize = `${n}px`;
} else if (key === 'smoothView') {
core.setLocalStorage('smoothView', n);
// core.setLocalStorage('smoothView', n);
} else if (key === 'criticalGem') {
core.setLocalStorage('criticalGem', n);
// core.setLocalStorage('criticalGem', n);
}
}
@@ -371,7 +404,7 @@ function handleActionSetting<T extends number | boolean>(
flags.autoSkill = n;
} else if (key === 'fixed') {
// 定点查看
core.setLocalStorage('fixed', n);
// core.setLocalStorage('fixed', n);
}
}
@@ -382,10 +415,10 @@ function handleUtilsSetting<T extends number | boolean>(
) {
if (key === 'betterLoad') {
// 加载优化
core.setLocalStorage('betterLoad', n);
// core.setLocalStorage('betterLoad', n);
} else if (key === 'autoScale') {
// 自动放缩
core.setLocalStorage('autoScale', n);
// core.setLocalStorage('autoScale', n);
}
}
+3
View File
@@ -84,3 +84,6 @@ export class GameStorage<T> {
window.addEventListener('unload', () => {
GameStorage.list.forEach(v => v.write());
});
window.addEventListener('blur', () => {
GameStorage.list.forEach(v => v.write());
});
+50 -50
View File
@@ -1,54 +1,54 @@
import pop from '@/plugin/pop';
import use from '@/plugin/use';
import animate from '@/plugin/animateController';
import utils from '@/plugin/utils';
import status from '@/plugin/ui/statusBar';
import fly from '@/plugin/ui/fly';
import chase from '@/plugin/chase/chase';
import webglUtils from '@/plugin/webgl/utils';
import shadow from '@/plugin/shadow/shadow';
import gameShadow from '@/plugin/shadow/gameShadow';
import achievement from '@/plugin/ui/achievement';
import completion, { floors } from '@/plugin/completion';
import path from '@/plugin/fx/path';
import gameCanvas from '@/plugin/fx/gameCanvas';
import noise from '@/plugin/fx/noise';
import smooth from '@/plugin/fx/smoothView';
import frag from '@/plugin/fx/frag';
import { Mota } from '.';
// import pop from '@/plugin/pop';
// import use from '@/plugin/use';
// import animate from '@/plugin/animateController';
// import utils from '@/plugin/utils';
// import status from '@/plugin/ui/statusBar';
// import fly from '@/plugin/ui/fly';
// import chase from '@/plugin/chase/chase';
// import webglUtils from '@/plugin/webgl/utils';
// import shadow from '@/plugin/shadow/shadow';
// import gameShadow from '@/plugin/shadow/gameShadow';
// import achievement from '@/plugin/ui/achievement';
// import completion, { floors } from '@/plugin/completion';
// import path from '@/plugin/fx/path';
// import gameCanvas from '@/plugin/fx/gameCanvas';
// import noise from '@/plugin/fx/noise';
// import smooth from '@/plugin/fx/smoothView';
// import frag from '@/plugin/fx/frag';
// import { Mota } from '.';
// todo: 将插件更改为注册形式,分为渲染进程和游戏进程两部分,同时分配优先级
// // todo: 将插件更改为注册形式,分为渲染进程和游戏进程两部分,同时分配优先级
export function resolvePlugin() {
const toForward: [keyof Mota['plugin'], any][] = [
['pop', pop()],
['use', use()],
['animate', animate()],
['utils', utils()],
['status', status()],
['fly', fly()],
['chase', chase()],
['webglUtils', webglUtils()],
['shadow', shadow()],
['gameShadow', gameShadow()],
['achievement', achievement()],
['completion', completion()],
['path', path()],
['gameCanvas', gameCanvas()],
['noise', noise()],
['smooth', smooth()],
['frag', frag()]
];
// export function resolvePlugin() {
// const toForward: [keyof Mota['plugin'], any][] = [
// ['pop', pop()],
// ['use', use()],
// ['animate', animate()],
// ['utils', utils()],
// ['status', status()],
// ['fly', fly()],
// ['chase', chase()],
// ['webglUtils', webglUtils()],
// ['shadow', shadow()],
// ['gameShadow', gameShadow()],
// ['achievement', achievement()],
// ['completion', completion()],
// ['path', path()],
// ['gameCanvas', gameCanvas()],
// ['noise', noise()],
// ['smooth', smooth()],
// ['frag', frag()]
// ];
for (const [key, obj] of toForward) {
mota.plugin[key] = obj;
}
// for (const [key, obj] of toForward) {
// mota.plugin[key] = obj;
// }
// 完成度相关
Object.values(floors).forEach((v, i) => {
const from = core.floorIds.indexOf(v[0]);
const to = core.floorIds.indexOf(v[1]);
const all = core.floorIds.slice(from, to + 1);
floors[i + 1] = all;
});
}
// // 完成度相关
// Object.values(floors).forEach((v, i) => {
// const from = core.floorIds.indexOf(v[0]);
// const to = core.floorIds.indexOf(v[1]);
// const all = core.floorIds.slice(from, to + 1);
// floors[i + 1] = all;
// });
// }