getWorldManager允许传入可选配置用于覆盖默认配置
This commit is contained in:
@@ -7,7 +7,7 @@ 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 } from './ECS/WorldManager';
|
||||
import { WorldManager, IWorldManagerConfig } from './ECS/WorldManager';
|
||||
import { DebugManager } from './Utils/Debug';
|
||||
import { ICoreConfig, IECSDebugConfig } from './Types';
|
||||
import { createLogger } from './Utils/Logger';
|
||||
@@ -432,21 +432,27 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 获取WorldManager实例
|
||||
*
|
||||
*
|
||||
* @param config 可选的WorldManager配置,用于覆盖默认配置
|
||||
* @returns WorldManager实例,如果未初始化则自动创建
|
||||
*/
|
||||
public static getWorldManager(): WorldManager {
|
||||
public static getWorldManager(config?: Partial<IWorldManagerConfig>): WorldManager {
|
||||
if (!this._instance) {
|
||||
throw new Error("Core实例未创建,请先调用Core.create()");
|
||||
}
|
||||
|
||||
if (!this._instance._worldManager) {
|
||||
// 多World模式的配置(用户主动获取WorldManager)
|
||||
this._instance._worldManager = WorldManager.getInstance({
|
||||
const defaultConfig = {
|
||||
maxWorlds: 50,
|
||||
autoCleanup: true,
|
||||
cleanupInterval: 60000,
|
||||
debug: this._instance._config.debug
|
||||
};
|
||||
|
||||
this._instance._worldManager = WorldManager.getInstance({
|
||||
...defaultConfig,
|
||||
...config // 用户传入的配置会覆盖默认配置
|
||||
});
|
||||
}
|
||||
|
||||
@@ -455,11 +461,13 @@ export class Core {
|
||||
|
||||
/**
|
||||
* 启用World管理
|
||||
*
|
||||
*
|
||||
* 显式启用World功能,用于多房间/多世界架构
|
||||
*
|
||||
* @param config 可选的WorldManager配置,用于覆盖默认配置
|
||||
*/
|
||||
public static enableWorldManager(): WorldManager {
|
||||
return this.getWorldManager();
|
||||
public static enableWorldManager(config?: Partial<IWorldManagerConfig>): WorldManager {
|
||||
return this.getWorldManager(config);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,11 +14,35 @@ const originalConsoleError = console.error;
|
||||
|
||||
// 在测试环境中可以选择性地静默某些日志
|
||||
beforeAll(() => {
|
||||
// 可以在这里设置全局的模拟或配置
|
||||
// 在测试开始前确保 WorldManager 使用无定时器配置
|
||||
// 这样后续的 Core.getWorldManager() 调用都会使用这个已创建的实例
|
||||
try {
|
||||
const { Core } = require('../src/Core');
|
||||
// 确保 Core 实例已创建
|
||||
if (!Core._instance) {
|
||||
Core.create();
|
||||
}
|
||||
Core.getWorldManager({
|
||||
autoCleanup: false,
|
||||
cleanupInterval: 0
|
||||
});
|
||||
} catch (error) {
|
||||
// 忽略初始化错误
|
||||
}
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// 清理全局资源
|
||||
// 清理WorldManager以避免定时器阻止Jest退出
|
||||
try {
|
||||
const { Core } = require('../src/Core');
|
||||
const worldManager = Core.getWorldManager();
|
||||
if (worldManager) {
|
||||
worldManager.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
// 忽略清理错误,避免影响测试结果
|
||||
}
|
||||
});
|
||||
|
||||
// 每个测试前的清理
|
||||
@@ -30,6 +54,25 @@ beforeEach(() => {
|
||||
afterEach(() => {
|
||||
// 恢复所有模拟
|
||||
jest.restoreAllMocks();
|
||||
|
||||
// 清理WorldManager状态以避免测试间的状态污染
|
||||
try {
|
||||
const { Core } = require('../src/Core');
|
||||
const { WorldManager } = require('../src/ECS/WorldManager');
|
||||
|
||||
// 重置 Core 和 WorldManager 单例
|
||||
if (Core._instance) {
|
||||
Core.reset();
|
||||
}
|
||||
if (WorldManager._instance) {
|
||||
if (WorldManager._instance.destroy) {
|
||||
WorldManager._instance.destroy();
|
||||
}
|
||||
WorldManager.reset();
|
||||
}
|
||||
} catch (error) {
|
||||
// 忽略清理错误
|
||||
}
|
||||
});
|
||||
|
||||
// 导出测试工具函数
|
||||
|
||||
Reference in New Issue
Block a user