新增scenemanager,重构core类减少多世界造成的性能压力
This commit is contained in:
@@ -5,61 +5,53 @@ import { Timer } from './Utils/Timers/Timer';
|
||||
import { Time } from './Utils/Time';
|
||||
import { PerformanceMonitor } from './Utils/PerformanceMonitor';
|
||||
import { PoolManager } from './Utils/Pool/PoolManager';
|
||||
import { ECSFluentAPI, createECSAPI } from './ECS/Core/FluentAPI';
|
||||
import { IScene } from './ECS/IScene';
|
||||
import { WorldManager, IWorldManagerConfig } from './ECS/WorldManager';
|
||||
import { DebugManager } from './Utils/Debug';
|
||||
import { ICoreConfig, IECSDebugConfig } from './Types';
|
||||
import { createLogger } from './Utils/Logger';
|
||||
import { SceneManager } from './ECS/SceneManager';
|
||||
import { IScene } from './ECS/IScene';
|
||||
|
||||
/**
|
||||
* 游戏引擎核心类
|
||||
*
|
||||
* 负责管理游戏的生命周期、场景切换、全局管理器和定时器系统。
|
||||
* 提供统一的游戏循环管理。
|
||||
*
|
||||
*
|
||||
* 职责:
|
||||
* - 提供全局服务(Timer、Performance、Pool等)
|
||||
* - 管理场景生命周期(内置SceneManager)
|
||||
* - 管理全局管理器的生命周期
|
||||
* - 提供统一的游戏循环更新入口
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 创建核心实例
|
||||
* const core = Core.create(true);
|
||||
*
|
||||
* // 设置场景
|
||||
* Core.scene = new MyScene();
|
||||
*
|
||||
* // 在游戏循环中更新(Laya引擎示例)
|
||||
* Laya.timer.frameLoop(1, this, () => {
|
||||
* const deltaTime = Laya.timer.delta / 1000;
|
||||
* // 初始化并设置场景
|
||||
* Core.create({ debug: true });
|
||||
* Core.setScene(new GameScene());
|
||||
*
|
||||
* // 游戏循环(自动更新全局服务和场景)
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime);
|
||||
* });
|
||||
*
|
||||
* // 调度定时器
|
||||
* }
|
||||
*
|
||||
* // 使用定时器
|
||||
* Core.schedule(1.0, false, null, (timer) => {
|
||||
* Core._logger.info("1秒后执行");
|
||||
* console.log("1秒后执行");
|
||||
* });
|
||||
*
|
||||
* // 切换场景
|
||||
* Core.loadScene(new MenuScene()); // 延迟切换
|
||||
* Core.setScene(new GameScene()); // 立即切换
|
||||
*
|
||||
* // 获取当前场景
|
||||
* const currentScene = Core.scene;
|
||||
* ```
|
||||
*/
|
||||
export class Core {
|
||||
/**
|
||||
* 游戏暂停状态
|
||||
*
|
||||
*
|
||||
* 当设置为true时,游戏循环将暂停执行。
|
||||
*/
|
||||
public static paused = false;
|
||||
|
||||
/**
|
||||
* 默认World ID
|
||||
*
|
||||
* 用于单Scene模式的默认World标识
|
||||
*/
|
||||
private static readonly DEFAULT_WORLD_ID = '__default__';
|
||||
|
||||
/**
|
||||
* 默认Scene ID
|
||||
*
|
||||
* 用于单Scene模式的默认Scene标识
|
||||
*/
|
||||
private static readonly DEFAULT_SCENE_ID = '__main__';
|
||||
|
||||
/**
|
||||
* 全局核心实例
|
||||
*/
|
||||
@@ -69,81 +61,71 @@ export class Core {
|
||||
* Core专用日志器
|
||||
*/
|
||||
private static _logger = createLogger('Core');
|
||||
|
||||
|
||||
/**
|
||||
* 实体系统启用状态
|
||||
*
|
||||
*
|
||||
* 控制是否启用ECS实体系统功能。
|
||||
*/
|
||||
public static entitySystemsEnabled: boolean;
|
||||
|
||||
|
||||
/**
|
||||
* 调试模式标志
|
||||
*
|
||||
*
|
||||
* 在调试模式下会启用额外的性能监控和错误检查。
|
||||
*/
|
||||
public readonly debug: boolean;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 全局管理器集合
|
||||
*
|
||||
*
|
||||
* 存储所有注册的全局管理器实例。
|
||||
*/
|
||||
public _globalManagers: GlobalManager[] = [];
|
||||
|
||||
|
||||
/**
|
||||
* 定时器管理器
|
||||
*
|
||||
*
|
||||
* 负责管理所有的游戏定时器。
|
||||
*/
|
||||
public _timerManager: TimerManager;
|
||||
|
||||
/**
|
||||
* 性能监控器
|
||||
*
|
||||
*
|
||||
* 监控游戏性能并提供优化建议。
|
||||
*/
|
||||
public _performanceMonitor: PerformanceMonitor;
|
||||
|
||||
/**
|
||||
* 对象池管理器
|
||||
*
|
||||
*
|
||||
* 管理所有对象池的生命周期。
|
||||
*/
|
||||
public _poolManager: PoolManager;
|
||||
|
||||
/**
|
||||
* ECS流式API
|
||||
*
|
||||
* 提供便捷的ECS操作接口。
|
||||
*/
|
||||
public _ecsAPI?: ECSFluentAPI;
|
||||
|
||||
|
||||
/**
|
||||
* 调试管理器
|
||||
*
|
||||
*
|
||||
* 负责收集和发送调试数据。
|
||||
*/
|
||||
public _debugManager?: DebugManager;
|
||||
|
||||
/**
|
||||
* World管理器
|
||||
*
|
||||
* 管理多个World实例,支持多房间/多世界架构。
|
||||
* 场景管理器
|
||||
*
|
||||
* 管理当前场景的生命周期。
|
||||
*/
|
||||
public _worldManager?: WorldManager;
|
||||
private _sceneManager: SceneManager;
|
||||
|
||||
/**
|
||||
* Core配置
|
||||
*/
|
||||
private _config: ICoreConfig;
|
||||
|
||||
|
||||
/**
|
||||
* 创建核心实例
|
||||
*
|
||||
*
|
||||
* @param config - Core配置对象
|
||||
*/
|
||||
private constructor(config: ICoreConfig = {}) {
|
||||
@@ -156,14 +138,13 @@ export class Core {
|
||||
...config
|
||||
};
|
||||
|
||||
|
||||
// 初始化管理器
|
||||
// 初始化定时器管理器
|
||||
this._timerManager = new TimerManager();
|
||||
Core.registerGlobalManager(this._timerManager);
|
||||
|
||||
// 初始化性能监控器
|
||||
this._performanceMonitor = PerformanceMonitor.instance;
|
||||
|
||||
|
||||
// 在调试模式下启用性能监控
|
||||
if (this._config.debug) {
|
||||
this._performanceMonitor.enable();
|
||||
@@ -171,7 +152,10 @@ export class Core {
|
||||
|
||||
// 初始化对象池管理器
|
||||
this._poolManager = PoolManager.getInstance();
|
||||
|
||||
|
||||
// 初始化场景管理器
|
||||
this._sceneManager = new SceneManager();
|
||||
|
||||
Core.entitySystemsEnabled = this._config.enableEntitySystems ?? true;
|
||||
this.debug = this._config.debug ?? true;
|
||||
|
||||
@@ -180,118 +164,161 @@ export class Core {
|
||||
this._debugManager = new DebugManager(this, this._config.debugConfig);
|
||||
}
|
||||
|
||||
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取核心实例
|
||||
*
|
||||
*
|
||||
* @returns 全局核心实例
|
||||
*/
|
||||
public static get Instance() {
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前活动的场景(属性访问器)
|
||||
*
|
||||
* @returns 当前场景实例,如果没有则返回null
|
||||
*/
|
||||
public static get scene(): IScene | null {
|
||||
return this.getScene();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前活动的场景(方法调用)
|
||||
*
|
||||
* @returns 当前场景实例,如果没有则返回null
|
||||
*/
|
||||
public static getScene<T extends IScene>(): T | null {
|
||||
if (!this._instance) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 确保默认World存在
|
||||
this._instance.ensureDefaultWorld();
|
||||
|
||||
const defaultWorld = this._instance._worldManager!.getWorld(this.DEFAULT_WORLD_ID);
|
||||
return defaultWorld?.getScene(this.DEFAULT_SCENE_ID) as T || null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置当前场景
|
||||
*
|
||||
* @param scene - 要设置的场景实例
|
||||
* @returns 设置的场景实例,便于链式调用
|
||||
*/
|
||||
public static setScene<T extends IScene>(scene: T): T {
|
||||
if (!this._instance) {
|
||||
throw new Error("Core实例未创建,请先调用Core.create()");
|
||||
}
|
||||
|
||||
// 确保默认World存在
|
||||
this._instance.ensureDefaultWorld();
|
||||
|
||||
const defaultWorld = this._instance._worldManager!.getWorld(this.DEFAULT_WORLD_ID)!;
|
||||
|
||||
// 移除旧的主Scene(如果存在)
|
||||
if (defaultWorld.getScene(this.DEFAULT_SCENE_ID)) {
|
||||
defaultWorld.removeScene(this.DEFAULT_SCENE_ID);
|
||||
}
|
||||
|
||||
// 添加新Scene到默认World
|
||||
defaultWorld.createScene(this.DEFAULT_SCENE_ID, scene);
|
||||
defaultWorld.setSceneActive(this.DEFAULT_SCENE_ID, true);
|
||||
|
||||
// 触发场景切换回调
|
||||
this._instance.onSceneChanged();
|
||||
|
||||
return scene;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建Core实例
|
||||
*
|
||||
*
|
||||
* 如果实例已存在,则返回现有实例。
|
||||
*
|
||||
*
|
||||
* @param config - Core配置,也可以直接传入boolean表示debug模式(向后兼容)
|
||||
* @returns Core实例
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 方式1:使用配置对象
|
||||
* Core.create({
|
||||
* debug: true,
|
||||
* enableEntitySystems: true,
|
||||
* debugConfig: {
|
||||
* enabled: true,
|
||||
* websocketUrl: 'ws://localhost:9229'
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* // 方式2:简单模式(向后兼容)
|
||||
* Core.create(true); // debug = true
|
||||
* ```
|
||||
*/
|
||||
public static create(config: ICoreConfig | boolean = true): Core {
|
||||
if (this._instance == null) {
|
||||
// 向后兼容:如果传入boolean,转换为配置对象
|
||||
const coreConfig: ICoreConfig = typeof config === 'boolean'
|
||||
const coreConfig: ICoreConfig = typeof config === 'boolean'
|
||||
? { debug: config, enableEntitySystems: true }
|
||||
: config;
|
||||
this._instance = new Core(coreConfig);
|
||||
} else {
|
||||
this._logger.warn('Core实例已创建,返回现有实例');
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新游戏逻辑
|
||||
*
|
||||
* 此方法应该在游戏引擎的更新循环中调用。
|
||||
*
|
||||
* @param deltaTime - 外部引擎提供的帧时间间隔(秒)
|
||||
*
|
||||
* 设置当前场景
|
||||
*
|
||||
* @param scene - 要设置的场景
|
||||
* @returns 设置的场景实例
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Laya引擎
|
||||
* Core.create({ debug: true });
|
||||
*
|
||||
* // 创建并设置场景
|
||||
* const gameScene = new GameScene();
|
||||
* Core.setScene(gameScene);
|
||||
* ```
|
||||
*/
|
||||
public static setScene<T extends IScene>(scene: T): T {
|
||||
if (!this._instance) {
|
||||
Core._logger.warn("Core实例未创建,请先调用Core.create()");
|
||||
throw new Error("Core实例未创建");
|
||||
}
|
||||
|
||||
return this._instance._sceneManager.setScene(scene);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前场景
|
||||
*
|
||||
* @returns 当前场景,如果没有场景则返回null
|
||||
*/
|
||||
public static get scene(): IScene | null {
|
||||
if (!this._instance) {
|
||||
return null;
|
||||
}
|
||||
return this._instance._sceneManager.currentScene;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ECS流式API
|
||||
*
|
||||
* @returns ECS API实例,如果当前没有场景则返回null
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 使用流式API创建实体
|
||||
* const player = Core.ecsAPI?.createEntity('Player')
|
||||
* .addComponent(Position, 100, 100)
|
||||
* .addComponent(Velocity, 50, 0);
|
||||
*
|
||||
* // 查询实体
|
||||
* const enemies = Core.ecsAPI?.query(Enemy, Transform);
|
||||
*
|
||||
* // 发射事件
|
||||
* Core.ecsAPI?.emit('game:start', { level: 1 });
|
||||
* ```
|
||||
*/
|
||||
public static get ecsAPI() {
|
||||
if (!this._instance) {
|
||||
return null;
|
||||
}
|
||||
return this._instance._sceneManager.api;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟加载场景(下一帧切换)
|
||||
*
|
||||
* @param scene - 要加载的场景
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 延迟切换场景(在下一帧生效)
|
||||
* Core.loadScene(new MenuScene());
|
||||
* ```
|
||||
*/
|
||||
public static loadScene<T extends IScene>(scene: T): void {
|
||||
if (!this._instance) {
|
||||
Core._logger.warn("Core实例未创建,请先调用Core.create()");
|
||||
return;
|
||||
}
|
||||
|
||||
this._instance._sceneManager.loadScene(scene);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新游戏逻辑
|
||||
*
|
||||
* 此方法应该在游戏引擎的更新循环中调用。
|
||||
* 会自动更新全局服务和当前场景。
|
||||
*
|
||||
* @param deltaTime - 外部引擎提供的帧时间间隔(秒)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 初始化
|
||||
* Core.create({ debug: true });
|
||||
* Core.setScene(new GameScene());
|
||||
*
|
||||
* // Laya引擎集成
|
||||
* Laya.timer.frameLoop(1, this, () => {
|
||||
* const deltaTime = Laya.timer.delta / 1000;
|
||||
* Core.update(deltaTime);
|
||||
* Core.update(deltaTime); // 自动更新全局服务和场景
|
||||
* });
|
||||
*
|
||||
* // Cocos Creator
|
||||
*
|
||||
* // Cocos Creator集成
|
||||
* update(deltaTime: number) {
|
||||
* Core.update(deltaTime);
|
||||
* Core.update(deltaTime); // 自动更新全局服务和场景
|
||||
* }
|
||||
*
|
||||
|
||||
* ```
|
||||
*/
|
||||
public static update(deltaTime: number): void {
|
||||
@@ -299,15 +326,15 @@ export class Core {
|
||||
Core._logger.warn("Core实例未创建,请先调用Core.create()");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
this._instance.updateInternal(deltaTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册全局管理器
|
||||
*
|
||||
*
|
||||
* 将管理器添加到全局管理器列表中,并启用它。
|
||||
*
|
||||
*
|
||||
* @param manager - 要注册的全局管理器
|
||||
*/
|
||||
public static registerGlobalManager(manager: GlobalManager) {
|
||||
@@ -317,9 +344,9 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 注销全局管理器
|
||||
*
|
||||
*
|
||||
* 从全局管理器列表中移除管理器,并禁用它。
|
||||
*
|
||||
*
|
||||
* @param manager - 要注销的全局管理器
|
||||
*/
|
||||
public static unregisterGlobalManager(manager: GlobalManager) {
|
||||
@@ -329,7 +356,7 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 获取指定类型的全局管理器
|
||||
*
|
||||
*
|
||||
* @param type - 管理器类型构造函数
|
||||
* @returns 管理器实例,如果未找到则返回null
|
||||
*/
|
||||
@@ -343,14 +370,27 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 调度定时器
|
||||
*
|
||||
*
|
||||
* 创建一个定时器,在指定时间后执行回调函数。
|
||||
*
|
||||
*
|
||||
* @param timeInSeconds - 延迟时间(秒)
|
||||
* @param repeats - 是否重复执行,默认为false
|
||||
* @param context - 回调函数的上下文,默认为null
|
||||
* @param onTime - 定时器触发时的回调函数
|
||||
* @returns 创建的定时器实例
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 一次性定时器
|
||||
* Core.schedule(1.0, false, null, (timer) => {
|
||||
* console.log("1秒后执行一次");
|
||||
* });
|
||||
*
|
||||
* // 重复定时器
|
||||
* Core.schedule(0.5, true, null, (timer) => {
|
||||
* console.log("每0.5秒执行一次");
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
public static schedule<TContext = unknown>(timeInSeconds: number, repeats: boolean = false, context?: TContext, onTime?: (timer: ITimer<TContext>) => void): Timer<TContext> {
|
||||
if (!onTime) {
|
||||
@@ -359,18 +399,9 @@ export class Core {
|
||||
return this._instance._timerManager.schedule(timeInSeconds, repeats, context as TContext, onTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ECS流式API
|
||||
*
|
||||
* @returns ECS API实例,如果未初始化则返回null
|
||||
*/
|
||||
public static get ecsAPI(): ECSFluentAPI | null {
|
||||
return this._instance?._ecsAPI || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用调试功能
|
||||
*
|
||||
*
|
||||
* @param config 调试配置
|
||||
*/
|
||||
public static enableDebug(config: IECSDebugConfig): void {
|
||||
@@ -408,7 +439,7 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 获取调试数据
|
||||
*
|
||||
*
|
||||
* @returns 当前调试数据,如果调试未启用则返回null
|
||||
*/
|
||||
public static getDebugData(): unknown {
|
||||
@@ -421,118 +452,30 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 检查调试是否启用
|
||||
*
|
||||
*
|
||||
* @returns 调试状态
|
||||
*/
|
||||
public static get isDebugEnabled(): boolean {
|
||||
return this._instance?._config.debugConfig?.enabled || false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取WorldManager实例
|
||||
*
|
||||
* @param config 可选的WorldManager配置,用于覆盖默认配置
|
||||
* @returns WorldManager实例,如果未初始化则自动创建
|
||||
*/
|
||||
public static getWorldManager(config?: Partial<IWorldManagerConfig>): WorldManager {
|
||||
if (!this._instance) {
|
||||
throw new Error("Core实例未创建,请先调用Core.create()");
|
||||
}
|
||||
|
||||
if (!this._instance._worldManager) {
|
||||
// 多World模式的配置(用户主动获取WorldManager)
|
||||
const defaultConfig = {
|
||||
maxWorlds: 50,
|
||||
autoCleanup: true,
|
||||
cleanupInterval: 60000,
|
||||
debug: this._instance._config.debug
|
||||
};
|
||||
|
||||
this._instance._worldManager = WorldManager.getInstance({
|
||||
...defaultConfig,
|
||||
...config // 用户传入的配置会覆盖默认配置
|
||||
});
|
||||
}
|
||||
|
||||
return this._instance._worldManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用World管理
|
||||
*
|
||||
* 显式启用World功能,用于多房间/多世界架构
|
||||
*
|
||||
* @param config 可选的WorldManager配置,用于覆盖默认配置
|
||||
*/
|
||||
public static enableWorldManager(config?: Partial<IWorldManagerConfig>): WorldManager {
|
||||
return this.getWorldManager(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保默认World存在
|
||||
*
|
||||
* 内部方法,用于懒初始化默认World
|
||||
*/
|
||||
private ensureDefaultWorld(): void {
|
||||
if (!this._worldManager) {
|
||||
this._worldManager = WorldManager.getInstance({
|
||||
maxWorlds: 1, // 单场景用户只需要1个World
|
||||
autoCleanup: false, // 单场景不需要自动清理
|
||||
cleanupInterval: 0, // 禁用清理定时器
|
||||
debug: this._config.debug
|
||||
});
|
||||
}
|
||||
|
||||
// 检查默认World是否存在
|
||||
if (!this._worldManager.getWorld(Core.DEFAULT_WORLD_ID)) {
|
||||
this._worldManager.createWorld(Core.DEFAULT_WORLD_ID, {
|
||||
name: 'DefaultWorld',
|
||||
maxScenes: 1,
|
||||
autoCleanup: false
|
||||
});
|
||||
this._worldManager.setWorldActive(Core.DEFAULT_WORLD_ID, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景切换回调
|
||||
*
|
||||
* 在场景切换时调用,用于重置时间系统等。
|
||||
*/
|
||||
public onSceneChanged() {
|
||||
Time.sceneChanged();
|
||||
|
||||
// 获取当前Scene(从默认World)
|
||||
const currentScene = Core.getScene();
|
||||
|
||||
// 初始化ECS API(如果场景支持)
|
||||
if (currentScene && currentScene.querySystem && currentScene.eventSystem) {
|
||||
this._ecsAPI = createECSAPI(currentScene, currentScene.querySystem, currentScene.eventSystem);
|
||||
}
|
||||
|
||||
// 延迟调试管理器通知,避免在场景初始化过程中干扰属性
|
||||
if (this._debugManager) {
|
||||
queueMicrotask(() => {
|
||||
this._debugManager?.onSceneChanged();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化核心系统
|
||||
*
|
||||
*
|
||||
* 执行核心系统的初始化逻辑。
|
||||
*/
|
||||
protected initialize() {
|
||||
// 核心系统初始化
|
||||
Core._logger.info('Core initialized', {
|
||||
debug: this.debug,
|
||||
entitySystemsEnabled: Core.entitySystemsEnabled,
|
||||
debugEnabled: this._config.debugConfig?.enabled || false
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 内部更新方法
|
||||
*
|
||||
*
|
||||
* @param deltaTime - 帧时间间隔(秒)
|
||||
*/
|
||||
private updateInternal(deltaTime: number): void {
|
||||
@@ -560,26 +503,8 @@ export class Core {
|
||||
// 更新对象池管理器
|
||||
this._poolManager.update();
|
||||
|
||||
// 更新所有World
|
||||
if (this._worldManager) {
|
||||
const worldsStartTime = this._performanceMonitor.startMonitoring('Worlds.update');
|
||||
const activeWorlds = this._worldManager.getActiveWorlds();
|
||||
let totalWorldEntities = 0;
|
||||
|
||||
for (const world of activeWorlds) {
|
||||
// 更新World的全局System
|
||||
world.updateGlobalSystems();
|
||||
|
||||
// 更新World中的所有Scene
|
||||
world.updateScenes();
|
||||
|
||||
// 统计实体数量(用于性能监控)
|
||||
const worldStats = world.getStats();
|
||||
totalWorldEntities += worldStats.totalEntities;
|
||||
}
|
||||
|
||||
this._performanceMonitor.endMonitoring('Worlds.update', worldsStartTime, totalWorldEntities);
|
||||
}
|
||||
// 更新场景
|
||||
this._sceneManager.update();
|
||||
|
||||
// 更新调试管理器(基于FPS的数据发送)
|
||||
if (this._debugManager) {
|
||||
@@ -589,4 +514,29 @@ export class Core {
|
||||
// 结束性能监控
|
||||
this._performanceMonitor.endMonitoring('Core.update', frameStartTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁Core实例
|
||||
*
|
||||
* 清理所有资源,通常在应用程序关闭时调用。
|
||||
*/
|
||||
public static destroy(): void {
|
||||
if (!this._instance) return;
|
||||
|
||||
// 停止调试管理器
|
||||
if (this._instance._debugManager) {
|
||||
this._instance._debugManager.stop();
|
||||
}
|
||||
|
||||
// 清理全局管理器
|
||||
for (const manager of this._instance._globalManagers) {
|
||||
manager.enabled = false;
|
||||
}
|
||||
this._instance._globalManagers = [];
|
||||
|
||||
Core._logger.info('Core destroyed');
|
||||
|
||||
// @ts-ignore - 清空实例引用
|
||||
this._instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
238
packages/core/src/ECS/SceneManager.ts
Normal file
238
packages/core/src/ECS/SceneManager.ts
Normal file
@@ -0,0 +1,238 @@
|
||||
import { IScene } from './IScene';
|
||||
import { ECSFluentAPI, createECSAPI } from './Core/FluentAPI';
|
||||
import { Time } from '../Utils/Time';
|
||||
import { Core } from '../Core';
|
||||
import { createLogger } from '../Utils/Logger';
|
||||
|
||||
/**
|
||||
* 单场景管理器
|
||||
*
|
||||
* 适用场景:
|
||||
* - 单人游戏
|
||||
* - 简单场景切换
|
||||
* - 不需要多World隔离的项目
|
||||
*
|
||||
* 特点:
|
||||
* - 轻量级,零额外开销
|
||||
* - 简单直观的API
|
||||
* - 支持延迟场景切换
|
||||
* - 自动管理ECS API
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 初始化Core
|
||||
* Core.create({ debug: true });
|
||||
*
|
||||
* // 创建场景管理器
|
||||
* const sceneManager = new SceneManager();
|
||||
*
|
||||
* // 设置场景
|
||||
* class GameScene extends Scene {
|
||||
* initialize() {
|
||||
* const player = this.createEntity('Player');
|
||||
* player.addComponent(new Transform(100, 100));
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* sceneManager.setScene(new GameScene());
|
||||
*
|
||||
* // 游戏循环
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime); // 更新全局服务
|
||||
* sceneManager.update(); // 更新场景
|
||||
* }
|
||||
*
|
||||
* // 延迟切换场景(下一帧生效)
|
||||
* sceneManager.loadScene(new MenuScene());
|
||||
* ```
|
||||
*/
|
||||
export class SceneManager {
|
||||
/**
|
||||
* 当前活跃场景
|
||||
*/
|
||||
private _currentScene: IScene | null = null;
|
||||
|
||||
/**
|
||||
* 待切换的下一个场景(延迟切换用)
|
||||
*/
|
||||
private _nextScene: IScene | null = null;
|
||||
|
||||
/**
|
||||
* ECS流式API
|
||||
*/
|
||||
private _ecsAPI: ECSFluentAPI | null = null;
|
||||
|
||||
/**
|
||||
* 日志器
|
||||
*/
|
||||
private _logger = createLogger('SceneManager');
|
||||
|
||||
/**
|
||||
* 设置当前场景(立即切换)
|
||||
*
|
||||
* 会自动处理旧场景的结束和新场景的初始化。
|
||||
*
|
||||
* @param scene - 要设置的场景实例
|
||||
* @returns 返回设置的场景实例,便于链式调用
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const gameScene = sceneManager.setScene(new GameScene());
|
||||
* console.log(gameScene.name); // 可以立即使用返回的场景
|
||||
* ```
|
||||
*/
|
||||
public setScene<T extends IScene>(scene: T): T {
|
||||
// 结束旧场景
|
||||
if (this._currentScene) {
|
||||
this._logger.info(`Ending scene: ${this._currentScene.name}`);
|
||||
this._currentScene.end();
|
||||
}
|
||||
|
||||
// 设置并初始化新场景
|
||||
this._currentScene = scene;
|
||||
this._currentScene.initialize();
|
||||
this._currentScene.begin();
|
||||
|
||||
// 重建ECS API
|
||||
if (scene.querySystem && scene.eventSystem) {
|
||||
this._ecsAPI = createECSAPI(scene, scene.querySystem, scene.eventSystem);
|
||||
} else {
|
||||
this._ecsAPI = null;
|
||||
}
|
||||
|
||||
// 触发场景切换回调
|
||||
Time.sceneChanged();
|
||||
|
||||
// 通知调试管理器
|
||||
const coreInstance = Core.Instance;
|
||||
if (coreInstance && coreInstance._debugManager) {
|
||||
coreInstance._debugManager.onSceneChanged();
|
||||
}
|
||||
|
||||
this._logger.info(`Scene changed to: ${scene.name}`);
|
||||
|
||||
return scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟加载场景(下一帧切换)
|
||||
*
|
||||
* 场景不会立即切换,而是在下一次调用 update() 时切换。
|
||||
* 这对于避免在当前帧的中途切换场景很有用。
|
||||
*
|
||||
* @param scene - 要加载的场景实例
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 在某个System中触发场景切换
|
||||
* class GameOverSystem extends EntitySystem {
|
||||
* process(entities: readonly Entity[]) {
|
||||
* if (playerHealth <= 0) {
|
||||
* sceneManager.loadScene(new GameOverScene());
|
||||
* // 当前帧继续执行,场景将在下一帧切换
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
public loadScene<T extends IScene>(scene: T): void {
|
||||
this._nextScene = scene;
|
||||
this._logger.info(`Scheduled scene load: ${scene.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前活跃的场景
|
||||
*
|
||||
* @returns 当前场景实例,如果没有场景则返回null
|
||||
*/
|
||||
public get currentScene(): IScene | null {
|
||||
return this._currentScene;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ECS流式API
|
||||
*
|
||||
* 提供便捷的实体查询、事件发射等功能。
|
||||
*
|
||||
* @returns ECS API实例,如果当前没有场景则返回null
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const api = sceneManager.api;
|
||||
* if (api) {
|
||||
* // 查询所有敌人
|
||||
* const enemies = api.find(Enemy, Transform);
|
||||
*
|
||||
* // 发射事件
|
||||
* api.emit('game:start', { level: 1 });
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
public get api(): ECSFluentAPI | null {
|
||||
return this._ecsAPI;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新场景
|
||||
*
|
||||
* 应该在每帧的游戏循环中调用。
|
||||
* 会自动处理延迟场景切换。
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime);
|
||||
* sceneManager.update(); // 每帧调用
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
public update(): void {
|
||||
// 处理延迟场景切换
|
||||
if (this._nextScene) {
|
||||
this.setScene(this._nextScene);
|
||||
this._nextScene = null;
|
||||
}
|
||||
|
||||
// 更新当前场景
|
||||
if (this._currentScene) {
|
||||
this._currentScene.update();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁场景管理器
|
||||
*
|
||||
* 会自动结束当前场景并清理所有资源。
|
||||
* 通常在应用程序关闭时调用。
|
||||
*/
|
||||
public destroy(): void {
|
||||
if (this._currentScene) {
|
||||
this._logger.info(`Destroying scene: ${this._currentScene.name}`);
|
||||
this._currentScene.end();
|
||||
this._currentScene = null;
|
||||
}
|
||||
|
||||
this._nextScene = null;
|
||||
this._ecsAPI = null;
|
||||
|
||||
this._logger.info('SceneManager destroyed');
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有活跃场景
|
||||
*
|
||||
* @returns 如果有活跃场景返回true,否则返回false
|
||||
*/
|
||||
public get hasScene(): boolean {
|
||||
return this._currentScene !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有待切换的场景
|
||||
*
|
||||
* @returns 如果有待切换场景返回true,否则返回false
|
||||
*/
|
||||
public get hasPendingScene(): boolean {
|
||||
return this._nextScene !== null;
|
||||
}
|
||||
}
|
||||
@@ -30,40 +30,45 @@ export interface IWorldManagerConfig {
|
||||
|
||||
/**
|
||||
* World管理器 - 管理所有World实例
|
||||
*
|
||||
* WorldManager是全局单例,负责管理所有World的生命周期。
|
||||
*
|
||||
* WorldManager负责管理多个独立的World实例。
|
||||
* 每个World都是独立的ECS环境,可以包含多个Scene。
|
||||
*
|
||||
* 设计理念:
|
||||
* - Core负责单Scene的传统ECS管理
|
||||
* - World负责多Scene的管理和协调
|
||||
* - WorldManager负责多World的全局管理
|
||||
*
|
||||
*
|
||||
* 适用场景:
|
||||
* - MMO游戏的多房间管理
|
||||
* - 服务器端的多游戏实例
|
||||
* - 需要完全隔离的多个游戏环境
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 获取全局WorldManager
|
||||
* const worldManager = WorldManager.getInstance();
|
||||
*
|
||||
* // 创建WorldManager实例
|
||||
* const worldManager = new WorldManager({
|
||||
* maxWorlds: 100,
|
||||
* autoCleanup: true
|
||||
* });
|
||||
*
|
||||
* // 创建游戏房间World
|
||||
* const roomWorld = worldManager.createWorld('room_001', {
|
||||
* const room1 = worldManager.createWorld('room_001', {
|
||||
* name: 'GameRoom_001',
|
||||
* maxScenes: 5
|
||||
* });
|
||||
*
|
||||
* // 在游戏循环中更新所有World
|
||||
* worldManager.updateAll(deltaTime);
|
||||
* room1.setActive(true);
|
||||
*
|
||||
* // 游戏循环
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime);
|
||||
* worldManager.updateAll(); // 更新所有活跃World
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class WorldManager {
|
||||
private static _instance: WorldManager | null = null;
|
||||
|
||||
private readonly _config: IWorldManagerConfig;
|
||||
private readonly _worlds: Map<string, World> = new Map();
|
||||
private readonly _activeWorlds: Set<string> = new Set();
|
||||
private _cleanupTimer: NodeJS.Timeout | null = null;
|
||||
private _cleanupTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private _isRunning: boolean = false;
|
||||
|
||||
private constructor(config: IWorldManagerConfig = {}) {
|
||||
public constructor(config: IWorldManagerConfig = {}) {
|
||||
this._config = {
|
||||
maxWorlds: 50,
|
||||
autoCleanup: true,
|
||||
@@ -72,6 +77,9 @@ export class WorldManager {
|
||||
...config
|
||||
};
|
||||
|
||||
// 默认启动运行状态
|
||||
this._isRunning = true;
|
||||
|
||||
logger.info('WorldManager已初始化', {
|
||||
maxWorlds: this._config.maxWorlds,
|
||||
autoCleanup: this._config.autoCleanup,
|
||||
@@ -81,26 +89,6 @@ export class WorldManager {
|
||||
this.startCleanupTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取WorldManager单例实例
|
||||
*/
|
||||
public static getInstance(config?: IWorldManagerConfig): WorldManager {
|
||||
if (!this._instance) {
|
||||
this._instance = new WorldManager(config);
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置WorldManager实例(主要用于测试)
|
||||
*/
|
||||
public static reset(): void {
|
||||
if (this._instance) {
|
||||
this._instance.destroy();
|
||||
this._instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== World管理 =====
|
||||
|
||||
/**
|
||||
@@ -205,9 +193,37 @@ export class WorldManager {
|
||||
|
||||
// ===== 批量操作 =====
|
||||
|
||||
/**
|
||||
* 更新所有活跃的World
|
||||
*
|
||||
* 应该在每帧的游戏循环中调用。
|
||||
* 会自动更新所有活跃World的全局系统和场景。
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime); // 更新全局服务
|
||||
* worldManager.updateAll(); // 更新所有World
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
public updateAll(): void {
|
||||
if (!this._isRunning) return;
|
||||
|
||||
for (const worldId of this._activeWorlds) {
|
||||
const world = this._worlds.get(worldId);
|
||||
if (world && world.isActive) {
|
||||
// 更新World的全局System
|
||||
world.updateGlobalSystems();
|
||||
|
||||
// 更新World中的所有Scene
|
||||
world.updateScenes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有激活的World
|
||||
* 注意:此方法供Core.update()使用
|
||||
*/
|
||||
public getActiveWorlds(): World[] {
|
||||
const activeWorlds: World[] = [];
|
||||
|
||||
@@ -6,6 +6,7 @@ export * from './Utils';
|
||||
export * from './Decorators';
|
||||
export { Scene } from './Scene';
|
||||
export { IScene, ISceneFactory, ISceneConfig } from './IScene';
|
||||
export { SceneManager } from './SceneManager';
|
||||
export { World, IWorldConfig } from './World';
|
||||
export { WorldManager, IWorldManagerConfig } from './WorldManager';
|
||||
export * from './Core/Events';
|
||||
|
||||
@@ -9,7 +9,7 @@ export class WebSocketManager {
|
||||
private reconnectInterval: number = 2000;
|
||||
private url: string;
|
||||
private autoReconnect: boolean;
|
||||
private reconnectTimer?: NodeJS.Timeout;
|
||||
private reconnectTimer?: ReturnType<typeof setTimeout>;
|
||||
private onOpen?: (event: Event) => void;
|
||||
private onClose?: (event: CloseEvent) => void;
|
||||
private onError?: (error: Event | any) => void;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Core } from '../src/Core';
|
||||
import { Scene } from '../src/ECS/Scene';
|
||||
import { SceneManager } from '../src/ECS/SceneManager';
|
||||
import { Entity } from '../src/ECS/Entity';
|
||||
import { Component } from '../src/ECS/Component';
|
||||
import { GlobalManager } from '../src/Utils/GlobalManager';
|
||||
import { ITimer } from '../src/Utils/Timers/ITimer';
|
||||
import { WorldManager } from '../src/ECS/WorldManager';
|
||||
|
||||
// 测试组件
|
||||
class TestComponent extends Component {
|
||||
@@ -65,10 +65,9 @@ describe('Core - 核心管理系统测试', () => {
|
||||
beforeEach(() => {
|
||||
// 清除之前的实例
|
||||
(Core as any)._instance = null;
|
||||
|
||||
// 重置WorldManager全局状态
|
||||
WorldManager.reset();
|
||||
|
||||
|
||||
// 注意:WorldManager不再是单例,无需reset
|
||||
|
||||
// 模拟console.warn以避免测试输出
|
||||
originalConsoleWarn = console.warn;
|
||||
console.warn = jest.fn();
|
||||
@@ -77,12 +76,11 @@ describe('Core - 核心管理系统测试', () => {
|
||||
afterEach(() => {
|
||||
// 恢复console.warn
|
||||
console.warn = originalConsoleWarn;
|
||||
|
||||
|
||||
// 清理Core实例
|
||||
(Core as any)._instance = null;
|
||||
|
||||
// 重置WorldManager全局状态
|
||||
WorldManager.reset();
|
||||
if (Core.Instance) {
|
||||
Core.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
describe('实例创建和管理', () => {
|
||||
@@ -128,96 +126,36 @@ describe('Core - 核心管理系统测试', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('场景管理', () => {
|
||||
// 注意:场景管理功能已移至SceneManager
|
||||
// 相关测试请查看 SceneManager.test.ts
|
||||
|
||||
describe('更新循环 - 全局服务', () => {
|
||||
let core: Core;
|
||||
let testScene: TestScene;
|
||||
|
||||
beforeEach(() => {
|
||||
core = Core.create(true);
|
||||
testScene = new TestScene();
|
||||
});
|
||||
|
||||
test('应该能够设置场景', () => {
|
||||
Core.setScene(testScene);
|
||||
|
||||
expect(Core.scene).toBe(testScene);
|
||||
expect(testScene.beginCalled).toBe(true);
|
||||
});
|
||||
|
||||
test('应该能够使用推荐的setScene方法设置场景', () => {
|
||||
const scene = Core.setScene(testScene);
|
||||
|
||||
expect(Core.scene).toBe(testScene);
|
||||
expect(testScene.beginCalled).toBe(true);
|
||||
expect(scene).toBe(testScene); // 应该返回场景实例
|
||||
});
|
||||
|
||||
test('设置新场景应该触发场景切换', () => {
|
||||
const firstScene = new TestScene();
|
||||
const secondScene = new TestScene();
|
||||
|
||||
// 设置第一个场景
|
||||
Core.setScene(firstScene);
|
||||
expect(firstScene.beginCalled).toBe(true);
|
||||
|
||||
// 设置第二个场景(应该在下一帧切换)
|
||||
Core.setScene(secondScene);
|
||||
|
||||
// 模拟更新循环触发场景切换
|
||||
Core.update(0.016);
|
||||
|
||||
expect(firstScene.endCalled).toBe(true);
|
||||
expect(secondScene.beginCalled).toBe(true);
|
||||
expect(Core.scene).toBe(secondScene);
|
||||
});
|
||||
|
||||
test('获取场景在未设置时应该返回null', () => {
|
||||
// 创建全新的Core实例,确保没有场景设置
|
||||
const core = Core.create(false);
|
||||
expect(Core.scene).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('更新循环', () => {
|
||||
let core: Core;
|
||||
let testScene: TestScene;
|
||||
let globalManager: TestGlobalManager;
|
||||
|
||||
beforeEach(() => {
|
||||
core = Core.create(true);
|
||||
testScene = new TestScene();
|
||||
globalManager = new TestGlobalManager();
|
||||
|
||||
Core.registerGlobalManager(globalManager);
|
||||
Core.setScene(testScene);
|
||||
});
|
||||
|
||||
test('应该能够执行更新循环', () => {
|
||||
const deltaTime = 0.016;
|
||||
|
||||
Core.update(deltaTime);
|
||||
|
||||
expect(testScene.updateCallCount).toBe(1);
|
||||
test('应该能够更新全局管理器', () => {
|
||||
Core.update(0.016);
|
||||
expect(globalManager.updateCallCount).toBe(1);
|
||||
});
|
||||
|
||||
test('暂停状态下不应该执行更新', () => {
|
||||
Core.paused = true;
|
||||
|
||||
Core.update(0.016);
|
||||
|
||||
expect(testScene.updateCallCount).toBe(0);
|
||||
expect(globalManager.updateCallCount).toBe(0);
|
||||
|
||||
|
||||
// 恢复状态
|
||||
Core.paused = false;
|
||||
});
|
||||
|
||||
test('禁用的全局管理器不应该被更新', () => {
|
||||
globalManager.enabled = false;
|
||||
|
||||
Core.update(0.016);
|
||||
|
||||
expect(globalManager.updateCallCount).toBe(0);
|
||||
});
|
||||
|
||||
@@ -225,8 +163,6 @@ describe('Core - 核心管理系统测试', () => {
|
||||
Core.update(0.016);
|
||||
Core.update(0.016);
|
||||
Core.update(0.016);
|
||||
|
||||
expect(testScene.updateCallCount).toBe(3);
|
||||
expect(globalManager.updateCallCount).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -410,41 +346,8 @@ describe('Core - 核心管理系统测试', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('ECS API集成', () => {
|
||||
let core: Core;
|
||||
let testScene: TestScene;
|
||||
|
||||
beforeEach(() => {
|
||||
core = Core.create(true);
|
||||
testScene = new TestScene();
|
||||
});
|
||||
|
||||
test('设置支持ECS的场景应该初始化ECS API', () => {
|
||||
// 模拟带有querySystem和eventSystem的场景
|
||||
const ecsScene = Object.assign(testScene, {
|
||||
querySystem: { query: jest.fn() },
|
||||
eventSystem: { emit: jest.fn() }
|
||||
});
|
||||
|
||||
Core.setScene(ecsScene);
|
||||
|
||||
expect(Core.ecsAPI).toBeDefined();
|
||||
});
|
||||
|
||||
test('设置普通场景不应该初始化ECS API', () => {
|
||||
// 创建一个普通场景对象(不继承Scene)
|
||||
const plainScene = {
|
||||
initialize: () => {},
|
||||
begin: () => {},
|
||||
end: () => {},
|
||||
update: () => {}
|
||||
};
|
||||
|
||||
Core.setScene(plainScene as any);
|
||||
|
||||
expect(Core.ecsAPI).toBeNull();
|
||||
});
|
||||
});
|
||||
// ECS API 现在由 SceneManager 管理
|
||||
// 相关测试请查看 SceneManager.test.ts
|
||||
|
||||
describe('性能监控集成', () => {
|
||||
let core: Core;
|
||||
@@ -455,52 +358,21 @@ describe('Core - 核心管理系统测试', () => {
|
||||
|
||||
test('调试模式下应该启用性能监控', () => {
|
||||
const performanceMonitor = (core as any)._performanceMonitor;
|
||||
|
||||
|
||||
expect(performanceMonitor).toBeDefined();
|
||||
// 性能监控器应该在调试模式下被启用
|
||||
expect(performanceMonitor.isEnabled).toBe(true);
|
||||
});
|
||||
|
||||
test('更新循环应该包含性能监控', () => {
|
||||
const scene = new TestScene();
|
||||
Core.setScene(scene);
|
||||
|
||||
const performanceMonitor = (core as any)._performanceMonitor;
|
||||
const startMonitoringSpy = jest.spyOn(performanceMonitor, 'startMonitoring');
|
||||
const endMonitoringSpy = jest.spyOn(performanceMonitor, 'endMonitoring');
|
||||
|
||||
|
||||
Core.update(0.016);
|
||||
|
||||
|
||||
expect(startMonitoringSpy).toHaveBeenCalled();
|
||||
expect(endMonitoringSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('错误处理', () => {
|
||||
test('设置null场景应该被忽略', () => {
|
||||
const core = Core.create(false);
|
||||
|
||||
// Core的新架构中场景不能直接设置为null
|
||||
// 默认情况下Core.scene应该为null(没有设置场景时)
|
||||
expect(Core.scene).toBeNull();
|
||||
});
|
||||
|
||||
test('应该处理场景更新中的异常', () => {
|
||||
const core = Core.create(true);
|
||||
const errorScene = new TestScene();
|
||||
|
||||
// 模拟场景更新抛出异常
|
||||
errorScene.update = () => {
|
||||
throw new Error('Test error');
|
||||
};
|
||||
|
||||
Core.setScene(errorScene);
|
||||
|
||||
// 由于Core目前不捕获场景异常,我们预期它会抛出异常
|
||||
// 这是一个已知的行为,可以在未来版本中改进
|
||||
expect(() => {
|
||||
Core.update(0.016);
|
||||
}).toThrow('Test error');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { Core } from '../../../src/Core';
|
||||
import { Scene } from '../../../src/ECS/Scene';
|
||||
import { World, IGlobalSystem } from '../../../src/ECS/World';
|
||||
import { WorldManager } from '../../../src/ECS/WorldManager';
|
||||
import { SceneManager } from '../../../src/ECS/SceneManager';
|
||||
import { EntitySystem } from '../../../src/ECS/Systems/EntitySystem';
|
||||
import { Component } from '../../../src/ECS/Component';
|
||||
import { Matcher } from '../../../src/ECS/Utils/Matcher';
|
||||
@@ -10,580 +11,185 @@ import { Entity } from '../../../src/ECS/Entity';
|
||||
// 测试用组件
|
||||
class TestComponent extends Component {
|
||||
public value: number = 0;
|
||||
|
||||
|
||||
constructor(value: number = 0) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
public reset(): void {
|
||||
this.value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
class NetworkComponent extends Component {
|
||||
public playerId: string;
|
||||
|
||||
constructor(playerId: string) {
|
||||
super();
|
||||
this.playerId = playerId;
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.playerId = '';
|
||||
}
|
||||
}
|
||||
|
||||
// 测试用系统
|
||||
class TestGlobalSystem extends EntitySystem {
|
||||
public processedEntities: Entity[] = [];
|
||||
public updateCount: number = 0;
|
||||
|
||||
constructor() {
|
||||
super(Matcher.empty().all(TestComponent));
|
||||
}
|
||||
|
||||
protected override process(entities: Entity[]): void {
|
||||
this.processedEntities = [...entities];
|
||||
this.updateCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 正确的全局系统实现
|
||||
class NetworkSyncGlobalSystem implements IGlobalSystem {
|
||||
public readonly name = 'NetworkSyncSystem';
|
||||
public updateCount: number = 0;
|
||||
|
||||
public initialize(): void {
|
||||
// 初始化网络连接等
|
||||
}
|
||||
|
||||
public update(): void {
|
||||
this.updateCount++;
|
||||
// 同步网络数据等全局逻辑
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.updateCount = 0;
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
// 清理网络连接等
|
||||
}
|
||||
}
|
||||
|
||||
// Scene级别的EntitySystem(正确的用法)
|
||||
class NetworkSyncSystem extends EntitySystem {
|
||||
public syncCount: number = 0;
|
||||
|
||||
constructor() {
|
||||
super(Matcher.empty().all(NetworkComponent));
|
||||
}
|
||||
|
||||
protected override process(entities: Entity[]): void {
|
||||
this.syncCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// World级别的网络同步全局系统
|
||||
// 全局系统实现
|
||||
class NetworkGlobalSystem implements IGlobalSystem {
|
||||
public readonly name = 'NetworkGlobalSystem';
|
||||
public syncCount: number = 0;
|
||||
|
||||
|
||||
public initialize(): void {
|
||||
// 初始化网络连接
|
||||
}
|
||||
|
||||
|
||||
public update(): void {
|
||||
this.syncCount++;
|
||||
// 全局网络同步逻辑
|
||||
}
|
||||
|
||||
|
||||
public reset(): void {
|
||||
this.syncCount = 0;
|
||||
}
|
||||
|
||||
|
||||
public destroy(): void {
|
||||
// 清理网络连接
|
||||
}
|
||||
}
|
||||
|
||||
// 测试用Scene
|
||||
class TestScene extends Scene {
|
||||
public updateCallCount: number = 0;
|
||||
|
||||
public override update(): void {
|
||||
super.update();
|
||||
this.updateCallCount++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* World与Core集成测试
|
||||
*
|
||||
* 注意:v3.0重构后,Core不再直接管理Scene/World
|
||||
* - 场景管理由 SceneManager 负责
|
||||
* - 多世界管理由 WorldManager 负责
|
||||
* - Core 仅负责全局服务(Timer、Performance等)
|
||||
*
|
||||
* 大部分旧的集成测试已移至 SceneManager.test.ts 和 WorldManager.test.ts
|
||||
*/
|
||||
describe('World与Core集成测试', () => {
|
||||
let worldManager: WorldManager;
|
||||
let sceneManager: SceneManager;
|
||||
|
||||
beforeEach(() => {
|
||||
// 重置Core和WorldManager
|
||||
if ((Core as any)._instance) {
|
||||
(Core as any)._instance = null;
|
||||
// 重置Core
|
||||
if (Core.Instance) {
|
||||
Core.destroy();
|
||||
}
|
||||
WorldManager['_instance'] = null;
|
||||
Core.create({ debug: false });
|
||||
|
||||
// WorldManager和SceneManager不再是单例
|
||||
worldManager = new WorldManager();
|
||||
sceneManager = new SceneManager();
|
||||
});
|
||||
|
||||
|
||||
afterEach(() => {
|
||||
// 清理资源
|
||||
if ((Core as any)._instance) {
|
||||
const worldManager = Core.getWorldManager?.();
|
||||
if (worldManager) {
|
||||
const worldIds = worldManager.getWorldIds();
|
||||
worldIds.forEach(id => {
|
||||
worldManager.removeWorld(id);
|
||||
});
|
||||
}
|
||||
(Core as any)._instance = null;
|
||||
if (sceneManager) {
|
||||
sceneManager.destroy();
|
||||
}
|
||||
if (worldManager) {
|
||||
worldManager.destroy();
|
||||
}
|
||||
if (Core.Instance) {
|
||||
Core.destroy();
|
||||
}
|
||||
WorldManager['_instance'] = null;
|
||||
});
|
||||
|
||||
describe('融合设计基础功能', () => {
|
||||
test('单Scene模式应该保持向后兼容', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
// 传统单Scene用法
|
||||
|
||||
describe('基础功能', () => {
|
||||
test('Core应该能够独立运行', () => {
|
||||
expect(Core.Instance).toBeDefined();
|
||||
|
||||
// Core.update 仅更新全局服务
|
||||
Core.update(0.016);
|
||||
});
|
||||
|
||||
test('SceneManager应该能够独立管理场景', () => {
|
||||
const scene = new Scene();
|
||||
scene.name = 'TestScene';
|
||||
|
||||
Core.setScene(scene);
|
||||
|
||||
const retrievedScene = Core.getScene();
|
||||
expect(retrievedScene).toBe(scene);
|
||||
expect(retrievedScene?.name).toBe('TestScene');
|
||||
|
||||
sceneManager.setScene(scene);
|
||||
|
||||
expect(sceneManager.currentScene).toBe(scene);
|
||||
expect(sceneManager.hasScene).toBe(true);
|
||||
|
||||
// 场景更新独立于Core
|
||||
sceneManager.update();
|
||||
});
|
||||
|
||||
test('启用WorldManager后应该支持多World功能', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
expect(worldManager).toBeDefined();
|
||||
|
||||
const world = worldManager.createWorld('TestWorld');
|
||||
expect(world).toBeDefined();
|
||||
expect(world.name).toBe('TestWorld');
|
||||
});
|
||||
|
||||
test('getWorldManager应该自动创建WorldManager', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
// 获取WorldManager会自动创建实例
|
||||
const worldManager = Core.getWorldManager();
|
||||
expect(worldManager).toBeDefined();
|
||||
|
||||
// 多次获取应该返回同一个实例
|
||||
const worldManager2 = Core.getWorldManager();
|
||||
expect(worldManager2).toBe(worldManager);
|
||||
});
|
||||
|
||||
test('单Scene模式下Core.update应该正常工作', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
const scene = new TestScene();
|
||||
Core.setScene(scene);
|
||||
|
||||
// 模拟更新
|
||||
Core.update(0.016);
|
||||
|
||||
expect(scene.updateCallCount).toBeGreaterThan(0);
|
||||
|
||||
test('WorldManager应该能够独立管理多个World', () => {
|
||||
const world1 = worldManager.createWorld('world1');
|
||||
const world2 = worldManager.createWorld('world2');
|
||||
|
||||
expect(worldManager.worldCount).toBe(2);
|
||||
expect(worldManager.getWorld('world1')).toBe(world1);
|
||||
expect(worldManager.getWorld('world2')).toBe(world2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('默认World机制', () => {
|
||||
test('设置Scene应该自动创建默认World', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
|
||||
describe('组合使用', () => {
|
||||
test('Core + SceneManager 应该正确协作', () => {
|
||||
const scene = new Scene();
|
||||
Core.setScene(scene);
|
||||
|
||||
// 启用WorldManager后应该能看到默认World
|
||||
Core.enableWorldManager();
|
||||
const worldManager = Core.getWorldManager();
|
||||
|
||||
expect(worldManager.getWorld('__default__')).toBeDefined();
|
||||
|
||||
const defaultWorld = worldManager.getWorld('__default__');
|
||||
expect(defaultWorld).toBeDefined();
|
||||
expect(defaultWorld?.getScene('__main__')).toBe(scene);
|
||||
sceneManager.setScene(scene);
|
||||
|
||||
const entity = scene.createEntity('TestEntity');
|
||||
entity.addComponent(new TestComponent(42));
|
||||
|
||||
// 游戏循环
|
||||
Core.update(0.016); // 更新全局服务
|
||||
sceneManager.update(); // 更新场景
|
||||
|
||||
expect(scene.entities.count).toBe(1);
|
||||
});
|
||||
|
||||
test('默认World的Scene应该正确激活', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
const scene = new Scene();
|
||||
Core.setScene(scene);
|
||||
|
||||
Core.enableWorldManager();
|
||||
const worldManager = Core.getWorldManager();
|
||||
const defaultWorld = worldManager.getWorld('__default__');
|
||||
|
||||
expect(defaultWorld?.isSceneActive('__main__')).toBe(true);
|
||||
|
||||
test('Core + WorldManager 应该正确协作', () => {
|
||||
const world = worldManager.createWorld('test-world');
|
||||
const scene = world.createScene('main', new Scene());
|
||||
world.setActive(true);
|
||||
|
||||
// 游戏循环
|
||||
Core.update(0.016); // 更新全局服务
|
||||
worldManager.updateAll(); // 更新所有World
|
||||
|
||||
expect(world.isActive).toBe(true);
|
||||
});
|
||||
|
||||
test('替换默认Scene应该正确处理', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
const scene1 = new Scene();
|
||||
scene1.name = 'Scene1';
|
||||
Core.setScene(scene1);
|
||||
|
||||
const scene2 = new Scene();
|
||||
scene2.name = 'Scene2';
|
||||
Core.setScene(scene2);
|
||||
|
||||
const currentScene = Core.getScene();
|
||||
expect(currentScene).toBe(scene2);
|
||||
expect(currentScene?.name).toBe('Scene2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('多World更新机制', () => {
|
||||
test('Core.update应该更新所有活跃World', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
|
||||
// 创建多个World
|
||||
const world1 = worldManager.createWorld('World1');
|
||||
const world2 = worldManager.createWorld('World2');
|
||||
const world3 = worldManager.createWorld('World3');
|
||||
|
||||
// 为每个World创建Scene和System
|
||||
const scene1 = world1.createScene('scene1', new TestScene());
|
||||
const scene2 = world2.createScene('scene2', new TestScene());
|
||||
const scene3 = world3.createScene('scene3', new TestScene());
|
||||
|
||||
// 启动部分World
|
||||
worldManager.setWorldActive('World1', true);
|
||||
worldManager.setWorldActive('World2', true);
|
||||
// world3保持未启动
|
||||
|
||||
world1.setSceneActive('scene1', true);
|
||||
world2.setSceneActive('scene2', true);
|
||||
|
||||
// 执行更新
|
||||
Core.update(0.016);
|
||||
|
||||
// 检查只有激活的World被更新
|
||||
expect(scene1.updateCallCount).toBeGreaterThan(0);
|
||||
expect(scene2.updateCallCount).toBeGreaterThan(0);
|
||||
expect(scene3.updateCallCount).toBe(0);
|
||||
});
|
||||
|
||||
test('全局系统应该在Scene更新前执行', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
const world = worldManager.createWorld('TestWorld');
|
||||
|
||||
// 添加正确设计的全局系统(业务逻辑系统,不是EntitySystem)
|
||||
const globalSystem = new NetworkSyncGlobalSystem();
|
||||
|
||||
test('World的全局系统应该能够正常工作', () => {
|
||||
const world = worldManager.createWorld('test-world');
|
||||
const globalSystem = new NetworkGlobalSystem();
|
||||
|
||||
world.addGlobalSystem(globalSystem);
|
||||
|
||||
// 创建Scene
|
||||
const scene = world.createScene('testScene');
|
||||
|
||||
worldManager.setWorldActive('TestWorld', true);
|
||||
world.setSceneActive('testScene', true);
|
||||
|
||||
// 执行更新
|
||||
Core.update(0.016);
|
||||
|
||||
// 验证全局System被正确更新
|
||||
expect(globalSystem.updateCount).toBeGreaterThan(0);
|
||||
world.setActive(true);
|
||||
|
||||
// 更新World
|
||||
worldManager.updateAll();
|
||||
|
||||
expect(globalSystem.syncCount).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('多房间游戏服务器场景', () => {
|
||||
test('多个游戏房间应该独立运行', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
|
||||
// 创建两个游戏房间
|
||||
const room1 = worldManager.createWorld('Room_001');
|
||||
const room2 = worldManager.createWorld('Room_002');
|
||||
|
||||
// 为每个房间设置Scene
|
||||
const gameScene1 = room1.createScene('game');
|
||||
const gameScene2 = room2.createScene('game');
|
||||
|
||||
// 为每个房间添加全局网络系统
|
||||
const netSystem1 = new NetworkGlobalSystem();
|
||||
const netSystem2 = new NetworkGlobalSystem();
|
||||
|
||||
room1.addGlobalSystem(netSystem1);
|
||||
room2.addGlobalSystem(netSystem2);
|
||||
|
||||
// 在每个房间创建玩家
|
||||
const player1 = gameScene1.createEntity('Player1');
|
||||
player1.addComponent(new NetworkComponent('player_123'));
|
||||
|
||||
const player2 = gameScene2.createEntity('Player2');
|
||||
player2.addComponent(new NetworkComponent('player_456'));
|
||||
|
||||
// 启动房间
|
||||
worldManager.setWorldActive('Room_001', true);
|
||||
worldManager.setWorldActive('Room_002', true);
|
||||
room1.setSceneActive('game', true);
|
||||
room2.setSceneActive('game', true);
|
||||
|
||||
// 模拟游戏循环
|
||||
for (let i = 0; i < 5; i++) {
|
||||
Core.update(0.016);
|
||||
}
|
||||
|
||||
// 验证每个房间独立运行
|
||||
expect(netSystem1.syncCount).toBeGreaterThan(0);
|
||||
expect(netSystem2.syncCount).toBeGreaterThan(0);
|
||||
expect(room1.getActiveSceneCount()).toBe(1);
|
||||
expect(room2.getActiveSceneCount()).toBe(1);
|
||||
|
||||
describe('隔离性测试', () => {
|
||||
test('多个WorldManager实例应该完全隔离', () => {
|
||||
const manager1 = new WorldManager();
|
||||
const manager2 = new WorldManager();
|
||||
|
||||
manager1.createWorld('world1');
|
||||
manager2.createWorld('world2');
|
||||
|
||||
expect(manager1.getWorld('world1')).toBeDefined();
|
||||
expect(manager1.getWorld('world2')).toBeNull();
|
||||
expect(manager2.getWorld('world2')).toBeDefined();
|
||||
expect(manager2.getWorld('world1')).toBeNull();
|
||||
|
||||
// 清理
|
||||
manager1.destroy();
|
||||
manager2.destroy();
|
||||
});
|
||||
|
||||
test('房间销毁应该完全清理资源', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
|
||||
// 创建房间
|
||||
const room = worldManager.createWorld('TempRoom');
|
||||
const scene = room.createScene('game');
|
||||
|
||||
// 添加内容
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const entity = scene.createEntity(`Entity${i}`);
|
||||
entity.addComponent(new TestComponent(i));
|
||||
}
|
||||
|
||||
room.addGlobalSystem(new NetworkSyncGlobalSystem());
|
||||
worldManager.setWorldActive('TempRoom', true);
|
||||
room.setSceneActive('game', true);
|
||||
|
||||
// 验证房间正常运行
|
||||
Core.update(0.016);
|
||||
|
||||
const beforeDestroy = worldManager.getStats();
|
||||
expect(beforeDestroy.totalWorlds).toBe(1);
|
||||
expect(beforeDestroy.activeWorlds).toBe(1);
|
||||
|
||||
// 销毁房间
|
||||
worldManager.removeWorld('TempRoom');
|
||||
|
||||
const afterDestroy = worldManager.getStats();
|
||||
expect(afterDestroy.totalWorlds).toBe(0);
|
||||
expect(afterDestroy.activeWorlds).toBe(0);
|
||||
|
||||
test('多个SceneManager实例应该完全隔离', () => {
|
||||
const sm1 = new SceneManager();
|
||||
const sm2 = new SceneManager();
|
||||
|
||||
const scene1 = new Scene();
|
||||
const scene2 = new Scene();
|
||||
|
||||
sm1.setScene(scene1);
|
||||
sm2.setScene(scene2);
|
||||
|
||||
expect(sm1.currentScene).toBe(scene1);
|
||||
expect(sm2.currentScene).toBe(scene2);
|
||||
|
||||
// 清理
|
||||
sm1.destroy();
|
||||
sm2.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('客户端多层Scene架构', () => {
|
||||
test('分层Scene应该同时运行', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
const clientWorld = worldManager.createWorld('ClientWorld');
|
||||
|
||||
// 创建不同层的Scene
|
||||
const gameplayScene = clientWorld.createScene('gameplay', new TestScene());
|
||||
const uiScene = clientWorld.createScene('ui', new TestScene());
|
||||
const effectsScene = clientWorld.createScene('effects', new TestScene());
|
||||
|
||||
// 启动世界并激活所有Scene
|
||||
worldManager.setWorldActive('ClientWorld', true);
|
||||
clientWorld.setSceneActive('gameplay', true);
|
||||
clientWorld.setSceneActive('ui', true);
|
||||
clientWorld.setSceneActive('effects', true);
|
||||
|
||||
// 执行更新
|
||||
Core.update(0.016);
|
||||
|
||||
// 验证所有Scene都被更新
|
||||
expect(gameplayScene.updateCallCount).toBeGreaterThan(0);
|
||||
expect(uiScene.updateCallCount).toBeGreaterThan(0);
|
||||
expect(effectsScene.updateCallCount).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('Scene的动态激活和停用', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
const world = worldManager.createWorld('DynamicWorld');
|
||||
|
||||
const gameScene = world.createScene('game', new TestScene());
|
||||
const menuScene = world.createScene('menu', new TestScene());
|
||||
|
||||
worldManager.setWorldActive('DynamicWorld', true);
|
||||
|
||||
// 初始状态:只有游戏Scene激活
|
||||
world.setSceneActive('game', true);
|
||||
world.setSceneActive('menu', false);
|
||||
|
||||
Core.update(0.016);
|
||||
|
||||
const gameCount1 = gameScene.updateCallCount;
|
||||
const menuCount1 = menuScene.updateCallCount;
|
||||
|
||||
// 切换到菜单
|
||||
world.setSceneActive('game', false);
|
||||
world.setSceneActive('menu', true);
|
||||
|
||||
Core.update(0.016);
|
||||
|
||||
const gameCount2 = gameScene.updateCallCount;
|
||||
const menuCount2 = menuScene.updateCallCount;
|
||||
|
||||
// 验证Scene状态切换
|
||||
expect(gameCount2).toBe(gameCount1); // 游戏Scene停止更新
|
||||
expect(menuCount2).toBeGreaterThan(menuCount1); // 菜单Scene开始更新
|
||||
});
|
||||
});
|
||||
|
||||
describe('性能和稳定性', () => {
|
||||
test('大量World和Scene应该稳定运行', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
const worldCount = 20;
|
||||
const scenePerWorld = 3;
|
||||
|
||||
// 创建大量World和Scene
|
||||
for (let i = 0; i < worldCount; i++) {
|
||||
const world = worldManager.createWorld(`World${i}`);
|
||||
|
||||
for (let j = 0; j < scenePerWorld; j++) {
|
||||
const scene = world.createScene(`Scene${j}`, new TestScene());
|
||||
|
||||
// 添加一些实体
|
||||
for (let k = 0; k < 5; k++) {
|
||||
const entity = scene.createEntity(`Entity${k}`);
|
||||
entity.addComponent(new TestComponent(k));
|
||||
}
|
||||
|
||||
world.setSceneActive(`Scene${j}`, true);
|
||||
}
|
||||
|
||||
worldManager.setWorldActive(`World${i}`, true);
|
||||
}
|
||||
|
||||
// 验证所有资源创建成功
|
||||
expect(worldManager.getWorldIds()).toHaveLength(worldCount);
|
||||
expect(worldManager.getActiveWorlds()).toHaveLength(worldCount);
|
||||
|
||||
// 执行多次更新测试稳定性
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(() => {
|
||||
Core.update(0.016);
|
||||
}).not.toThrow();
|
||||
}
|
||||
|
||||
// 验证更新正常工作
|
||||
const activeWorlds = worldManager.getActiveWorlds();
|
||||
activeWorlds.forEach(world => {
|
||||
const scenes = world.getAllScenes();
|
||||
scenes.forEach(scene => {
|
||||
if (scene instanceof TestScene && world.isSceneActive(scene.name)) {
|
||||
expect(scene.updateCallCount).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('频繁的World创建和销毁应该不影响性能', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
|
||||
// 频繁创建和销毁World
|
||||
for (let cycle = 0; cycle < 10; cycle++) {
|
||||
// 创建批次World
|
||||
const worldIds: string[] = [];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const worldId = `Cycle${cycle}_World${i}`;
|
||||
worldIds.push(worldId);
|
||||
|
||||
const world = worldManager.createWorld(worldId);
|
||||
const scene = world.createScene('test');
|
||||
scene.createEntity('entity');
|
||||
|
||||
worldManager.setWorldActive(worldId, true);
|
||||
world.setSceneActive('test', true);
|
||||
}
|
||||
|
||||
// 更新一次
|
||||
Core.update(0.016);
|
||||
|
||||
// 销毁批次World
|
||||
worldIds.forEach(id => {
|
||||
worldManager.removeWorld(id);
|
||||
});
|
||||
|
||||
// 验证清理完成
|
||||
expect(worldManager.getWorldIds()).toHaveLength(0);
|
||||
expect(worldManager.getActiveWorlds()).toHaveLength(0);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('错误处理和边界情况', () => {
|
||||
test('Core未初始化时操作应该抛出合适错误', () => {
|
||||
// getScene 会返回 null 而不是抛出错误
|
||||
expect(Core.getScene()).toBeNull();
|
||||
|
||||
expect(() => {
|
||||
Core.setScene(new Scene());
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('在World销毁后继续操作应该安全', () => {
|
||||
Core.create({ debug: false });
|
||||
Core.enableWorldManager();
|
||||
|
||||
const worldManager = Core.getWorldManager();
|
||||
const world = worldManager.createWorld('DestroyTest');
|
||||
|
||||
worldManager.setWorldActive('DestroyTest', true);
|
||||
worldManager.removeWorld('DestroyTest');
|
||||
|
||||
// 对已销毁的World进行操作应该不会崩溃
|
||||
expect(() => {
|
||||
world.updateGlobalSystems();
|
||||
world.updateScenes();
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
test('混合使用单Scene和多World模式', () => {
|
||||
Core.create({ debug: false });
|
||||
|
||||
// 直接启用WorldManager(避免先使用单Scene创建限制性配置)
|
||||
const worldManager = Core.getWorldManager();
|
||||
|
||||
// 然后使用单Scene模式
|
||||
const singleScene = new Scene();
|
||||
Core.setScene(singleScene);
|
||||
|
||||
// 验证默认World被创建
|
||||
expect(worldManager.getWorld('__default__')).toBeDefined();
|
||||
|
||||
// 创建额外的World
|
||||
const extraWorld = worldManager.createWorld('ExtraWorld');
|
||||
worldManager.setWorldActive('ExtraWorld', true);
|
||||
|
||||
// 两种模式应该能共存
|
||||
expect(() => {
|
||||
Core.update(0.016);
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -39,13 +39,12 @@ class TestGlobalSystem {
|
||||
|
||||
describe('WorldManager', () => {
|
||||
let worldManager: WorldManager;
|
||||
|
||||
|
||||
beforeEach(() => {
|
||||
// 重置单例
|
||||
WorldManager['_instance'] = null;
|
||||
worldManager = WorldManager.getInstance();
|
||||
// WorldManager不再是单例,直接创建新实例
|
||||
worldManager = new WorldManager();
|
||||
});
|
||||
|
||||
|
||||
afterEach(() => {
|
||||
// 清理所有World
|
||||
if (worldManager) {
|
||||
@@ -56,33 +55,40 @@ describe('WorldManager', () => {
|
||||
// 清理定时器
|
||||
worldManager.destroy();
|
||||
}
|
||||
WorldManager['_instance'] = null;
|
||||
});
|
||||
|
||||
describe('单例模式', () => {
|
||||
test('获取实例应该返回相同的实例', () => {
|
||||
const instance1 = WorldManager.getInstance();
|
||||
const instance2 = WorldManager.getInstance();
|
||||
|
||||
expect(instance1).toBe(instance2);
|
||||
|
||||
describe('实例化', () => {
|
||||
test('可以创建多个独立的WorldManager实例', () => {
|
||||
const manager1 = new WorldManager();
|
||||
const manager2 = new WorldManager();
|
||||
|
||||
expect(manager1).not.toBe(manager2);
|
||||
|
||||
manager1.createWorld('world1');
|
||||
expect(manager2.getWorld('world1')).toBeNull();
|
||||
|
||||
// 清理
|
||||
manager1.destroy();
|
||||
manager2.destroy();
|
||||
});
|
||||
|
||||
|
||||
test('使用配置创建实例应该正确', () => {
|
||||
WorldManager['_instance'] = null;
|
||||
|
||||
const config: IWorldManagerConfig = {
|
||||
maxWorlds: 10,
|
||||
autoCleanup: true,
|
||||
debug: false
|
||||
};
|
||||
|
||||
const instance = WorldManager.getInstance(config);
|
||||
|
||||
|
||||
const instance = new WorldManager(config);
|
||||
|
||||
expect(instance).toBeDefined();
|
||||
expect(instance).toBe(WorldManager.getInstance());
|
||||
expect(instance.worldCount).toBe(0);
|
||||
|
||||
// 清理
|
||||
instance.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('World管理', () => {
|
||||
test('创建World应该成功', () => {
|
||||
const world = worldManager.createWorld('test-world');
|
||||
@@ -115,19 +121,17 @@ describe('WorldManager', () => {
|
||||
});
|
||||
|
||||
test('超出最大World数量应该抛出错误', () => {
|
||||
WorldManager['_instance'] = null;
|
||||
const limitedManager = WorldManager.getInstance({ maxWorlds: 2 });
|
||||
|
||||
const limitedManager = new WorldManager({ maxWorlds: 2 });
|
||||
|
||||
limitedManager.createWorld('world1');
|
||||
limitedManager.createWorld('world2');
|
||||
|
||||
|
||||
expect(() => {
|
||||
limitedManager.createWorld('world3');
|
||||
}).toThrow("已达到最大World数量限制: 2");
|
||||
|
||||
|
||||
// 清理
|
||||
limitedManager.removeWorld('world1');
|
||||
limitedManager.removeWorld('world2');
|
||||
limitedManager.destroy();
|
||||
});
|
||||
|
||||
test('获取World应该正确', () => {
|
||||
@@ -430,35 +434,31 @@ describe('WorldManager', () => {
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
WorldManager.getInstance(invalidConfig);
|
||||
new WorldManager(invalidConfig);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
test('配置更新应该影响后续操作', () => {
|
||||
WorldManager['_instance'] = null;
|
||||
|
||||
|
||||
test('配置应该正确应用于新实例', () => {
|
||||
const config: IWorldManagerConfig = {
|
||||
maxWorlds: 3,
|
||||
autoCleanup: true,
|
||||
debug: true
|
||||
};
|
||||
|
||||
const manager = WorldManager.getInstance(config);
|
||||
|
||||
|
||||
const manager = new WorldManager(config);
|
||||
|
||||
// 创建到限制数量的World
|
||||
manager.createWorld('world1');
|
||||
manager.createWorld('world2');
|
||||
manager.createWorld('world3');
|
||||
|
||||
|
||||
// 第四个应该失败
|
||||
expect(() => {
|
||||
manager.createWorld('world4');
|
||||
}).toThrow();
|
||||
|
||||
|
||||
// 清理
|
||||
manager.removeWorld('world1');
|
||||
manager.removeWorld('world2');
|
||||
manager.removeWorld('world3');
|
||||
manager.destroy();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user