refactor & fix: 插件与进程分离

This commit is contained in:
AncTe committed 2024-02-02 17:10:21 +08:00
1 parent 325a1e85ec
commit a9c64565e1
41 files changed
+473 -467

No files matched your search

+6 -1
View File
@@ -1,5 +1,8 @@
// todo: 更改utils.ts的形式,使common文件夹可以同时在渲染进程和游戏进程使用
import { has } from '@/plugin/utils';
function has<T>(value: T): value is NonNullable<T> {
return value !== null && value !== undefined;
}
export interface EmitableEvent {
[event: string]: (...params: any) => any;
@@ -21,6 +24,8 @@ type EmitFn<F extends (...params: any) => any> = (
...params: Parameters<F>
) => any;
console.log(1);
export class EventEmitter<T extends EmitableEvent = {}> {
protected events: {
[P in keyof T]?: Listener<T[P]>[];
+38 -35
View File
@@ -1,49 +1,33 @@
import { BgmController, bgm } from './audio/bgm';
import { SoundController, sound } from './audio/sound';
import { EventEmitter } from './common/eventEmitter';
import { loading, readyAllResource } from './loader/load';
import { SoundController, SoundEffect, sound } from './audio/sound';
import { readyAllResource } from './loader/load';
import {
Resource,
ResourceStore,
ResourceType,
ZippedResource,
resource,
zipResource
} from './loader/resource';
import { UiController } from './main/custom/ui';
import { GameEvent, gameListener, hook } from './main/game';
import { Focus, GameUi, UiController } from './main/custom/ui';
import { GameStorage } from './main/storage';
// 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 {
MotaSetting,
SettingDisplayer,
mainSetting,
settingStorage
} from './main/setting';
import { KeyCode } from '@/plugin/keyCodes';
import { status } from '@/plugin/ui/statusBar';
import './plugin';
import './package';
// interface AncTePlugin {
// pop: ReturnType<typeof import('../plugin/pop').default>;
// use: ReturnType<typeof import('../plugin/use').default>;
// animate: ReturnType<typeof import('../plugin/animateController').default>;
// utils: ReturnType<typeof import('../plugin/utils').default>;
// status: ReturnType<typeof import('../plugin/ui/statusBar').default>;
// fly: ReturnType<typeof import('../plugin/ui/fly').default>;
// chase: ReturnType<typeof import('../plugin/chase/chase').default>;
// webglUtils: ReturnType<typeof import('../plugin/webgl/utils').default>;
// shadow: ReturnType<typeof import('../plugin/shadow/shadow').default>;
// gameShadow: ReturnType<
// typeof import('../plugin/shadow/gameShadow').default
// >;
// achievement: ReturnType<typeof import('../plugin/ui/achievement').default>;
// completion: ReturnType<typeof import('../plugin/completion').default>;
// path: ReturnType<typeof import('../plugin/fx/path').default>;
// gameCanvas: ReturnType<typeof import('../plugin/fx/gameCanvas').default>;
// noise: ReturnType<typeof import('../plugin/fx/noise').default>;
// smooth: ReturnType<typeof import('../plugin/fx/smoothView').default>;
// frag: ReturnType<typeof import('../plugin/fx/frag').default>;
// }
import { AudioPlayer } from './audio/audio';
import { CustomToolbar } from './main/custom/toolbar';
import { Hotkey } from './main/custom/hotkey';
import { Keyboard } from './main/custom/keyboard';
// export interface Mota {
// sound: SoundController;
@@ -81,19 +65,38 @@ function ready() {
readyAllResource();
// loading.once('coreInit', resolvePlugin);
}
ready();
// ----- 类注册
Mota.register('class', 'AudioPlayer', AudioPlayer);
Mota.register('class', 'BgmController', BgmController);
Mota.register('class', 'CustomToolbar', CustomToolbar);
Mota.register('class', 'Focus', Focus);
Mota.register('class', 'GameStorage', GameStorage);
Mota.register('class', 'GameUi', GameUi);
Mota.register('class', 'Hotkey', Hotkey);
Mota.register('class', 'Keyboard', Keyboard);
Mota.register('class', 'MotaSetting', MotaSetting);
Mota.register('class', 'Resource', Resource);
Mota.register('class', 'ResourceStore', ResourceStore);
Mota.register('class', 'SettingDisplayer', SettingDisplayer);
Mota.register('class', 'SoundController', SoundController);
Mota.register('class', 'SoundEffect', SoundEffect);
Mota.register('class', 'UiController', UiController);
Mota.register('class', 'ZippedResource', ZippedResource);
// ----- 函数注册
// ----- 变量注册
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);
Mota.register('var', 'status', status);
// ----- 模块注册
ready();
+1 -83
View File
@@ -7,13 +7,6 @@ import {
resource as res
} from './resource';
interface GameLoadEvent extends EmitableEvent {
coreLoaded: () => void;
autotileLoaded: () => void;
coreInit: () => void;
materialLoaded: () => void;
}
const info = resource;
/**
@@ -35,6 +28,7 @@ export function readyAllResource() {
* 开发时的加载
*/
/* @__PURE__ */ async function readyDevResource() {
const loading = Mota.require('var', 'loading');
const loadData = (await import('../../data/resource-dev.json')).default;
loadData.forEach(v => {
@@ -50,79 +44,3 @@ export function readyAllResource() {
animates.active();
});
}
class GameLoading extends EventEmitter<GameLoadEvent> {
private autotileLoaded: number = 0;
private autotileNum?: number;
private autotileListened: boolean = false;
private materialsNum: number = main.materials.length;
private materialsLoaded: number = 0;
constructor() {
super();
this.on(
'coreInit',
() => {
this.autotileNum = Object.keys(
core.material.icons.autotile
).length;
},
{ immediate: true }
);
this.on('materialLoaded', () => {
core.loader._loadMaterials_afterLoad();
});
}
addMaterialLoaded() {
this.once('coreInit', () => {
this.materialsLoaded++;
if (this.materialsLoaded === this.materialsNum) {
this.emit('materialLoaded');
}
});
}
addAutotileLoaded() {
this.once('coreInit', () => {
this.autotileLoaded++;
if (this.autotileLoaded === this.autotileNum) {
this.emit('autotileLoaded');
}
});
}
/**
* 当自动原件加载完毕时
* @param autotiles 自动原件数组
*/
onAutotileLoaded(
autotiles: Partial<Record<AllIdsOf<'autotile'>, HTMLImageElement>>
) {
if (this.autotileListened) return;
this.autotileListened = true;
this.on('autotileLoaded', () => {
const keys = Object.keys(
core.material.icons.autotile
) as AllIdsOf<'autotile'>[];
keys.forEach(v => {
core.material.images.autotile[v] = autotiles[v]!;
});
setTimeout(() => {
core.maps._makeAutotileEdges();
});
});
}
}
export const loading = new GameLoading();
declare global {
interface Main {
loading: GameLoading;
}
}
main.loading = loading;
+1 -1
View File
@@ -4,7 +4,6 @@ import { ensureArray } from '@/plugin/utils';
import { has } from '@/plugin/utils';
import JSZip from 'jszip';
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
import { loading } from './load';
// todo: 应当用register去注册资源类型,然后进行分块处理
@@ -59,6 +58,7 @@ export class Resource<
}
protected onLoad(v: ResourceData[T]) {
const loading = Mota.require('var', 'loading');
// 资源类型处理
if (this.type === 'fonts') {
document.fonts.add(new FontFace(this.name, v as ArrayBuffer));
+2
View File
@@ -40,6 +40,8 @@ export interface HotkeyJSON {
assist: number;
}
console.log(2);
export class Hotkey extends EventEmitter<HotkeyEvent> {
static list: Hotkey[];
-1
View File
@@ -15,7 +15,6 @@ import {
import { gameKey } from '../init/hotkey';
import { unwarpBinary } from './hotkey';
import { fixedUi } from '../init/ui';
import { hook } from '../game';
interface CustomToolbarEvent extends EmitableEvent {
add: (item: ValueOf<ToolbarItemMap>) => void;
-120
View File
@@ -1,120 +0,0 @@
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
import { loading } from '../loader/load';
export interface GameEvent extends EmitableEvent {
/** Emitted in events.prototype.resetGame. */
reset: () => void;
/** Emitted in src/App.vue setup. */
mounted: () => void;
/** Emitted in plugin/ui.js */
statusBarUpdate: () => void;
/** Emitted in libs/events.js */
afterGetItem: (
itemId: AllIdsOf<'items'>,
x: number,
y: number,
isGentleClick: boolean
) => void;
afterOpenDoor: (doorId: AllIdsOf<'animates'>, x: number, y: number) => void;
}
export const hook = new EventEmitter<GameEvent>();
interface ListenerEvent extends EmitableEvent {
// block
hoverBlock: (block: Block, ev: MouseEvent) => void;
leaveBlock: (block: Block, ev: MouseEvent, leaveGame: boolean) => void;
clickBlock: (block: Block, ev: MouseEvent) => void;
// mouse
mouseMove: (ev: MouseEvent) => void;
}
class GameListener extends EventEmitter<ListenerEvent> {
static num: number = 0;
num: number = GameListener.num++;
constructor() {
super();
if (!!window.core) {
this.init();
} else {
loading.once('coreInit', () => {
this.init();
});
}
}
private init() {
// ----- block
let lastHoverX = -1;
let lastHoverY = -1;
const data = core.canvas.data.canvas;
const getBlockLoc = (px: number, py: number, size: number) => {
return [
Math.floor(((px * 32) / size - core.bigmap.offsetX) / 32),
Math.floor(((py * 32) / size - core.bigmap.offsetY) / 32)
];
};
// hover & leave & mouseMove
data.addEventListener('mousemove', e => {
if (core.status.lockControl || !core.isPlaying()) return;
this.emit('mouseMove', e);
const {
x: px,
y: py,
size
} = core.actions._getClickLoc(e.clientX, e.clientY);
const [bx, by] = getBlockLoc(px, py, size);
const blocks = core.getMapBlocksObj();
if (lastHoverX !== bx || lastHoverY !== by) {
const lastBlock = blocks[`${lastHoverX},${lastHoverY}`];
const block = blocks[`${bx},${by}`];
if (!!lastBlock) {
this.emit('leaveBlock', lastBlock, e, false);
}
if (!!block) {
this.emit('hoverBlock', block, e);
lastHoverX = bx;
lastHoverY = by;
} else {
lastHoverX = -1;
lastHoverY = -1;
}
}
});
data.addEventListener('mouseleave', e => {
if (core.status.lockControl || !core.isPlaying()) return;
const blocks = core.getMapBlocksObj();
const lastBlock = blocks[`${lastHoverX},${lastHoverY}`];
if (!!lastBlock) {
this.emit('leaveBlock', lastBlock, e, true);
}
lastHoverX = -1;
lastHoverY = -1;
});
// click
data.addEventListener('click', e => {
if (core.status.lockControl || !core.isPlaying()) return;
const {
x: px,
y: py,
size
} = core.actions._getClickLoc(e.clientX, e.clientY);
const [bx, by] = getBlockLoc(px, py, size);
const blocks = core.getMapBlocksObj();
const block = blocks[`${bx},${by}`];
if (!!block) {
this.emit('clickBlock', block, e);
}
});
// ----- mouse
}
}
export const gameListener = new GameListener();
+1 -1
View File
@@ -1,5 +1,4 @@
import { debounce } from 'lodash-es';
import { gameListener, hook } from '../game';
import { fixedUi, mainUi } from './ui';
import { ref } from 'vue';
import { sleep } from 'mutate-animate';
@@ -38,6 +37,7 @@ const closeFixed = () => {
// todo: 应当在这里实现查看临界与特殊属性的功能
export let hovered: Block | null;
const { hook, gameListener } = Mota.requireAll('var');
gameListener.on('hoverBlock', block => {
closeFixed();
hovered = block;
+1 -1
View File
@@ -1,7 +1,6 @@
import * as UI from '@ui/.';
import * as MiscUI from './misc';
import { GameUi, UiController } from '../custom/ui';
import { hook } from '../game';
export const mainUi = new UiController();
mainUi.register(
@@ -36,6 +35,7 @@ fixedUi.register(
);
fixedUi.showAll();
const hook = Mota.require('var', 'hook');
hook.once('mounted', () => {
const ui = document.getElementById('ui-main')!;
const fixed = document.getElementById('ui-fixed')!;
+2 -2
View File
@@ -1,7 +1,5 @@
import { FunctionalComponent, reactive } from 'vue';
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
import { loading } from '../loader/load';
import { hook } from './game';
import { GameStorage } from './storage';
import { has, triggerFullscreen } from '@/plugin/utils';
import { createSettingComponents } from './init/settings';
@@ -478,6 +476,7 @@ mainSetting
)
);
const loading = Mota.require('var', 'loading');
loading.once('coreInit', () => {
mainSetting.reset({
'screen.fullscreen': !!document.fullscreenElement,
@@ -497,6 +496,7 @@ loading.once('coreInit', () => {
});
});
const { hook } = Mota.requireAll('var');
hook.on('reset', () => {
mainSetting.reset({
'action.autoSkill': flags.autoSkill ?? true
-5
View File
@@ -27,7 +27,6 @@ import * as frag from '@/plugin/fx/frag';
import * as use from '@/plugin/use';
import * as gameCanvas from '@/plugin/fx/gameCanvas';
import * as smooth from '@/plugin/fx/smoothView';
import { loading } from './loader/load';
Mota.Plugin.register('shadow_r', shadow, shadow.init);
Mota.Plugin.register('gameShadow_r', gameShadow, gameShadow.init);
@@ -40,10 +39,6 @@ Mota.Plugin.register('use_r', use);
Mota.Plugin.register('gameCanvas_r', gameCanvas);
Mota.Plugin.register('smooth_r', smooth, smooth.init);
loading.once('coreInit', () => {
Mota.Plugin.init();
});
// // todo: 将插件更改为注册形式,分为渲染进程和游戏进程两部分,同时分配优先级
// export function resolvePlugin() {