Files
shooter-demo/client-cocos/assets/scripts/Modules/Pinball/Boot/PinballBootUtils.ts
2025-11-28 18:10:10 +08:00

186 lines
5.7 KiB
TypeScript

/**
* Pinball 启动工具类
* 提供常用的启动配置和便捷方法
*/
import { Component } from 'cc';
import { PinballBootConfig, PinballBootMode, PinballBootResult, PinballBootstrap } from './index';
export class PinballBootUtils {
/**
* 创建高性能配置(适合移动设备)
*/
public static createMobileConfig(mode: PinballBootMode): PinballBootConfig {
const config = PinballBootstrap.createDefaultConfig(mode);
// 移动设备优化配置
config.renderConfig = {
enableEffects: false, // 关闭粒子效果以提升性能
maxParticles: 100 // 降低粒子数量
};
config.physicsConfig = {
gravity: { x: 0, y: -9.81 },
timeStep: 1 / 30 // 降低物理步进频率
};
return config;
}
/**
* 创建高质量配置(适合桌面设备)
*/
public static createDesktopConfig(mode: PinballBootMode): PinballBootConfig {
const config = PinballBootstrap.createDefaultConfig(mode);
// 桌面设备高质量配置
config.renderConfig = {
enableEffects: true, // 启用所有粒子效果
maxParticles: 1000 // 更多粒子数量
};
config.physicsConfig = {
gravity: { x: 0, y: -9.81 },
timeStep: 1 / 120 // 更高的物理步进频率
};
return config;
}
/**
* 创建调试配置
*/
public static createDebugConfig(mode: PinballBootMode): PinballBootConfig {
const config = PinballBootstrap.createDefaultConfig(mode);
config.debugMode = true;
return config;
}
/**
* 创建生产配置
*/
public static createProductionConfig(mode: PinballBootMode): PinballBootConfig {
const config = PinballBootstrap.createDefaultConfig(mode);
config.debugMode = false;
return config;
}
/**
* 批量启动多个模式(用于测试)
*/
public static async batchBoot(
hostComponent: Component,
configs: PinballBootConfig[]
): Promise<PinballBootResult[]> {
const bootstrap = PinballBootstrap.getInstance();
const results: PinballBootResult[] = [];
for (const config of configs) {
console.log(`[PinballBootUtils] 批量启动: ${config.mode}`);
const result = await bootstrap.boot(hostComponent, config);
results.push(result);
// 如果启动失败,停止批量启动
if (!result.success) {
console.error(`[PinballBootUtils] 批量启动在 ${config.mode} 模式失败: ${result.error}`);
break;
}
}
return results;
}
/**
* 性能基准测试启动
*/
public static async benchmarkBoot(
hostComponent: Component,
config: PinballBootConfig,
iterations: number = 5
): Promise<{ averageTime: number; results: PinballBootResult[] }> {
const bootstrap = PinballBootstrap.getInstance();
const results: PinballBootResult[] = [];
let totalTime = 0;
console.log(`[PinballBootUtils] 开始性能基准测试,迭代次数: ${iterations}`);
for (let i = 0; i < iterations; i++) {
const startTime = performance.now();
const result = await bootstrap.boot(hostComponent, config);
const endTime = performance.now();
const duration = endTime - startTime;
totalTime += duration;
results.push(result);
console.log(`[PinballBootUtils] 第 ${i + 1} 次启动耗时: ${duration.toFixed(2)}ms`);
// 清理资源准备下一次测试
if (result.success && result.pinballManager) {
await result.pinballManager.stopCurrentGame();
}
}
const averageTime = totalTime / iterations;
console.log(`[PinballBootUtils] 平均启动时间: ${averageTime.toFixed(2)}ms`);
return { averageTime, results };
}
/**
* 验证启动结果
*/
public static validateBootResult(result: PinballBootResult): {
isValid: boolean;
issues: string[];
} {
const issues: string[] = [];
if (!result.success) {
issues.push(`启动失败: ${result.error}`);
}
if (!result.pinballManager) {
issues.push('PinballManager 实例不存在');
}
if (!result.mode) {
issues.push('启动模式未定义');
}
if (!result.timestamp || result.timestamp <= 0) {
issues.push('时间戳无效');
}
return {
isValid: issues.length === 0,
issues
};
}
/**
* 生成启动报告
*/
public static generateBootReport(results: PinballBootResult[]): string {
const successCount = results.filter(r => r.success).length;
const failureCount = results.length - successCount;
let report = '=== Pinball 启动报告 ===\n';
report += `总启动次数: ${results.length}\n`;
report += `成功启动: ${successCount}\n`;
report += `启动失败: ${failureCount}\n`;
report += `成功率: ${((successCount / results.length) * 100).toFixed(1)}%\n\n`;
if (failureCount > 0) {
report += '失败详情:\n';
results.filter(r => !r.success).forEach((result, index) => {
report += `${index + 1}. ${result.mode}: ${result.error}\n`;
});
}
return report;
}
}