116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
/**
|
|
* Pinball 启动管理器
|
|
* 负责维护游戏启动状态和调度具体启动器
|
|
*/
|
|
|
|
import { Component } from 'cc';
|
|
import { PinballBootConfig, PinballBootMode, PinballBootResult } from './BootTypes';
|
|
import { ClientMultiplayerBooter } from './Mode/ClientMultiplayerBooter';
|
|
import { ServerMultiplayerBooter } from './Mode/ServerMultiplayerBooter';
|
|
import { StandaloneBooter } from './Mode/StandaloneBooter';
|
|
|
|
export class PinballBootstrap {
|
|
private static instance: PinballBootstrap;
|
|
private currentResult: PinballBootResult | null = null;
|
|
|
|
/** 获取单例实例 */
|
|
public static getInstance(): PinballBootstrap {
|
|
if (!PinballBootstrap.instance) {
|
|
PinballBootstrap.instance = new PinballBootstrap();
|
|
}
|
|
return PinballBootstrap.instance;
|
|
}
|
|
|
|
private constructor() { }
|
|
|
|
/**
|
|
* 启动 Pinball 游戏
|
|
*/
|
|
public async boot(hostComponent: Component, config: PinballBootConfig): Promise<PinballBootResult> {
|
|
const startTime = Date.now();
|
|
|
|
try {
|
|
console.log(`[PinballBootstrap] 开始启动 ${config.mode} 模式`);
|
|
|
|
// 获取对应的启动器
|
|
const booter = this.getBooter(config.mode);
|
|
|
|
// 执行启动
|
|
const pinballManager = await booter.boot(hostComponent, config);
|
|
|
|
// 记录启动结果
|
|
this.currentResult = {
|
|
success: true,
|
|
mode: config.mode,
|
|
pinballManager,
|
|
timestamp: startTime
|
|
};
|
|
|
|
console.log(`[PinballBootstrap] ${config.mode} 模式启动成功`);
|
|
return this.currentResult;
|
|
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
console.error(`[PinballBootstrap] 启动失败:`, error);
|
|
|
|
this.currentResult = {
|
|
success: false,
|
|
error: errorMessage,
|
|
mode: config.mode,
|
|
timestamp: startTime
|
|
};
|
|
|
|
return this.currentResult;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取启动器实例
|
|
*/
|
|
private getBooter(mode: PinballBootMode) {
|
|
switch (mode) {
|
|
case PinballBootMode.STANDALONE:
|
|
return new StandaloneBooter();
|
|
case PinballBootMode.CLIENT_MULTIPLAYER:
|
|
return new ClientMultiplayerBooter();
|
|
case PinballBootMode.SERVER_MULTIPLAYER:
|
|
return new ServerMultiplayerBooter();
|
|
default:
|
|
throw new Error(`不支持的启动模式: ${mode}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前启动结果
|
|
*/
|
|
public getCurrentResult(): PinballBootResult | null {
|
|
return this.currentResult;
|
|
}
|
|
|
|
/**
|
|
* 清除启动状态
|
|
*/
|
|
public clearState(): void {
|
|
this.currentResult = null;
|
|
}
|
|
|
|
/**
|
|
* 创建默认配置
|
|
*/
|
|
public static createDefaultConfig(mode: PinballBootMode): PinballBootConfig {
|
|
return {
|
|
mode,
|
|
debugMode: true,
|
|
autoStart: true,
|
|
physicsConfig: {
|
|
gravity: { x: 0, y: -9.81 },
|
|
timeStep: 1 / 60
|
|
},
|
|
renderConfig: {
|
|
enableEffects: true,
|
|
maxParticles: 500
|
|
},
|
|
wasmPath: 'assets/wasm/pinball_physics.wasm'
|
|
};
|
|
}
|
|
} |