mirror of
https://github.com/motajs/template.git
synced 2026-11-16 08:54:30 +08:00
refactor: 插件系统 & 数据端顶层移植
This commit is contained in:
1 parent
b0229c627f
commit
2f3d3387a0
74 files changed
+1322
-3321
No files matched your search
@@ -1,5 +1,16 @@
|
||||
import { CoreState } from '@user/data-state';
|
||||
import { IClientCore, IClientCoreConfig } from './types';
|
||||
import {
|
||||
CoreState,
|
||||
IPluginOrderResult,
|
||||
orderPlugins,
|
||||
PluginStatus
|
||||
} from '@user/data-state';
|
||||
import {
|
||||
IClientCore,
|
||||
IClientCoreConfig,
|
||||
IClientPlugin,
|
||||
IClientPluginDefine
|
||||
} from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
import {
|
||||
IMotaAudioContext,
|
||||
ISoundPlayer,
|
||||
@@ -37,7 +48,6 @@ import {
|
||||
MAIN_WIDTH,
|
||||
VARIATOR_DEBUG_SPEED
|
||||
} from './shared';
|
||||
import { WebLoadStarter } from '@motajs/loader';
|
||||
import { IRendererUsing, RendererUsing } from '@motajs/render-vue';
|
||||
import { IUIController, UIController } from '@motajs/system';
|
||||
import { createApp } from './render';
|
||||
@@ -63,37 +73,31 @@ export class ClientCore extends CoreState implements IClientCore {
|
||||
|
||||
// Layer 5 渲染顶层
|
||||
|
||||
/** 已注册的渲染端插件 */
|
||||
private readonly clientPlugins: Map<
|
||||
string,
|
||||
IClientPlugin<IClientPluginDefine>
|
||||
> = new Map();
|
||||
private readonly orderedClientPlugins: IPluginOrderResult<
|
||||
IClientPlugin<IClientPluginDefine>
|
||||
>;
|
||||
|
||||
constructor(config: IClientCoreConfig) {
|
||||
super({
|
||||
loadStarter: new WebLoadStarter(),
|
||||
coreURL: 'placeholder'
|
||||
});
|
||||
super(config);
|
||||
|
||||
//#region Layer 4
|
||||
|
||||
this.save = new SaveSystem();
|
||||
this.save.init(`@game/${core.firstData.name}`);
|
||||
|
||||
//#region 音频系统
|
||||
|
||||
this.save.init('');
|
||||
this.audioContext = new MotaAudioContext();
|
||||
this.soundPlayer = new SoundPlayer(this.audioContext);
|
||||
this.bgmPlayer = new BGMPlayer(this.audioContext);
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 素材系统
|
||||
|
||||
this.materials = new TextureManager(
|
||||
this,
|
||||
config.tilesetReserve,
|
||||
config.tilesetUnit
|
||||
);
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 渲染系统
|
||||
|
||||
const rafExcitation = new RafExcitation();
|
||||
const excitationDivider = new ExcitationDivider<number>();
|
||||
this.rafExcitation = rafExcitation;
|
||||
@@ -133,9 +137,92 @@ export class ClientCore extends CoreState implements IClientCore {
|
||||
|
||||
this.loader.addCoreConfig('client', config.clientURL);
|
||||
|
||||
this.init();
|
||||
// 渲染端插件注册表
|
||||
for (const define of config.clientPlugins) {
|
||||
if (this.clientPlugins.has(define.id)) {
|
||||
logger.error(78, define.id);
|
||||
continue;
|
||||
}
|
||||
this.clientPlugins.set(define.id, {
|
||||
status: PluginStatus.NotAwakened,
|
||||
plugin: define
|
||||
});
|
||||
}
|
||||
this.orderedClientPlugins = orderPlugins(this.clientPlugins);
|
||||
}
|
||||
|
||||
//#region 插件方法
|
||||
|
||||
getClientPlugin<T extends IClientPluginDefine>(
|
||||
id: string
|
||||
): Readonly<IClientPlugin<T>> | null {
|
||||
return (this.clientPlugins.get(id) as IClientPlugin<T>) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件同步初始化
|
||||
*/
|
||||
protected awake(): void {
|
||||
super.awake();
|
||||
const result = this.orderedClientPlugins;
|
||||
if (result.cyclic.size > 0) {
|
||||
logger.error(76, [...result.cyclic].join(', '));
|
||||
}
|
||||
for (const [pluginId, lacked] of result.missing) {
|
||||
for (const dependency of lacked) {
|
||||
logger.error(77, pluginId, dependency);
|
||||
}
|
||||
}
|
||||
for (const holder of result.order) {
|
||||
if (holder.status !== PluginStatus.NotAwakened) continue;
|
||||
const dependencies: IClientPlugin<IClientPluginDefine>[] = [];
|
||||
for (const dependency of holder.plugin.dependencies) {
|
||||
const plugin = this.clientPlugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
}
|
||||
holder.plugin.awake(this, dependencies);
|
||||
holder.status = PluginStatus.Awakened;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件异步初始化
|
||||
*/
|
||||
protected async initialize(): Promise<void> {
|
||||
await super.initialize();
|
||||
const result = this.orderedClientPlugins;
|
||||
for (const holder of result.order) {
|
||||
if (holder.status !== PluginStatus.Awakened) continue;
|
||||
const dependencies: IClientPlugin<IClientPluginDefine>[] = [];
|
||||
for (const dependency of holder.plugin.dependencies) {
|
||||
const plugin = this.clientPlugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
}
|
||||
await holder.plugin.initialize(this, dependencies);
|
||||
holder.status = PluginStatus.Initialized;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件加载初始化
|
||||
*/
|
||||
protected async afterLoad(): Promise<void> {
|
||||
await super.afterLoad();
|
||||
const result = this.orderedClientPlugins;
|
||||
for (const holder of result.order) {
|
||||
if (holder.status !== PluginStatus.Initialized) continue;
|
||||
const dependencies: IClientPlugin<IClientPluginDefine>[] = [];
|
||||
for (const dependency of holder.plugin.dependencies) {
|
||||
const plugin = this.clientPlugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
}
|
||||
await holder.plugin.afterLoad(this, dependencies);
|
||||
holder.status = PluginStatus.Loaded;
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
/**
|
||||
* 初始化渲染端主对象
|
||||
*/
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ClientCore } from './client';
|
||||
|
||||
// TODO: 逐渐弱化 ClientCore 的单例概念,每个接口都通过参数传入 IClientCore 对象
|
||||
|
||||
/** 客户端实例 */
|
||||
export const client = new ClientCore({
|
||||
clientURL: 'placeholder',
|
||||
// 这两个值不要随意调整,若需要调整,务必先在编辑器中调整“额外素材预留”与“额外素材单位”,否则必定会导致游戏出错
|
||||
tilesetReserve: 100000,
|
||||
tilesetUnit: 5000
|
||||
});
|
||||
@@ -9,6 +9,7 @@ export * from './render';
|
||||
export * from './ui';
|
||||
|
||||
export * from './client';
|
||||
export * from './common';
|
||||
export * from './core';
|
||||
export * from './shared';
|
||||
export * from './types';
|
||||
@@ -1,12 +1,75 @@
|
||||
import { IClientSystem } from '@user/client-system';
|
||||
import { ICoreStateConfig, PluginStatus } from '@user/data-state';
|
||||
|
||||
export interface IClientCoreConfig {
|
||||
/** 渲染端数据配置文件路径,相对于 `src/content` */
|
||||
readonly clientURL: string;
|
||||
/** Tileset 预留数量 */
|
||||
readonly tilesetReserve: number;
|
||||
/** Tileset 单元数量 */
|
||||
readonly tilesetUnit: number;
|
||||
//#region 插件系统
|
||||
|
||||
export interface IClientPlugin<T extends IClientPluginDefine> {
|
||||
/** 插件的当前状态 */
|
||||
status: PluginStatus;
|
||||
/** 插件内容 */
|
||||
plugin: T;
|
||||
}
|
||||
|
||||
export interface IClientCore extends IClientSystem {}
|
||||
export interface IClientPluginDefine {
|
||||
/** 插件标识符 */
|
||||
readonly id: string;
|
||||
/** 插件所依赖的其他插件 id */
|
||||
readonly dependencies: readonly string[];
|
||||
|
||||
/**
|
||||
* 插件同步初始化,在渲染端主对象创建完毕,初始化之前时调用
|
||||
* @param state 渲染端主对象
|
||||
* @param dependencies 此插件的依赖插件对象
|
||||
*/
|
||||
awake(
|
||||
state: IClientCore,
|
||||
dependencies: IClientPlugin<IClientPluginDefine>[]
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 插件异步初始化,在主对象创建完毕,初始化时,加载开始前调用。
|
||||
* 此方法具有阻塞效果,如果返回值迟迟不兑现,那么会导致依赖其的插件不会开始异步初始化
|
||||
* @param state 渲染端主对象
|
||||
* @param dependencies 此插件的依赖插件对象
|
||||
*/
|
||||
initialize(
|
||||
state: IClientCore,
|
||||
dependencies: IClientPlugin<IClientPluginDefine>[]
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当主对象加载完毕后调用,可以用于依赖加载内容的初始化。
|
||||
* 此方法具有阻塞效果,如果返回值迟迟不兑现,那么会导致依赖其的插件不会开始加载初始化
|
||||
* @param state 渲染端主对象
|
||||
* @param dependencies 此插件的依赖插件对象
|
||||
*/
|
||||
afterLoad(
|
||||
state: IClientCore,
|
||||
dependencies: IClientPlugin<IClientPluginDefine>[]
|
||||
): Promise<void>;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
export interface IClientCoreConfig extends ICoreStateConfig {
|
||||
/** 渲染端数据配置文件路径,相对于 `src/content` */
|
||||
readonly clientURL: string;
|
||||
/** Tileset 预留数量,用于控制最大允许注册多少个普通图块 */
|
||||
readonly tilesetReserve: number;
|
||||
/** Tileset 单元数量,用于控制一个 Tileset 会占用多少个图块 */
|
||||
readonly tilesetUnit: number;
|
||||
/** 渲染器挂载至的画布元素 */
|
||||
readonly rendererCanvas: string;
|
||||
/** 渲染端插件列表 */
|
||||
readonly clientPlugins: IClientPluginDefine[];
|
||||
}
|
||||
|
||||
export interface IClientCore extends IClientSystem {
|
||||
/**
|
||||
* 根据 id 获取已加装的渲染端插件
|
||||
* @param id 插件的标识符
|
||||
*/
|
||||
getClientPlugin<T extends IClientPluginDefine>(
|
||||
id: string
|
||||
): Readonly<IClientPlugin<T>> | null;
|
||||
}
|
||||
Reference in new issue
Block a user