getWorldManager允许传入可选配置用于覆盖默认配置
This commit is contained in:
@@ -7,7 +7,7 @@ import { PerformanceMonitor } from './Utils/PerformanceMonitor';
|
|||||||
import { PoolManager } from './Utils/Pool/PoolManager';
|
import { PoolManager } from './Utils/Pool/PoolManager';
|
||||||
import { ECSFluentAPI, createECSAPI } from './ECS/Core/FluentAPI';
|
import { ECSFluentAPI, createECSAPI } from './ECS/Core/FluentAPI';
|
||||||
import { IScene } from './ECS/IScene';
|
import { IScene } from './ECS/IScene';
|
||||||
import { WorldManager } from './ECS/WorldManager';
|
import { WorldManager, IWorldManagerConfig } from './ECS/WorldManager';
|
||||||
import { DebugManager } from './Utils/Debug';
|
import { DebugManager } from './Utils/Debug';
|
||||||
import { ICoreConfig, IECSDebugConfig } from './Types';
|
import { ICoreConfig, IECSDebugConfig } from './Types';
|
||||||
import { createLogger } from './Utils/Logger';
|
import { createLogger } from './Utils/Logger';
|
||||||
@@ -433,20 +433,26 @@ export class Core {
|
|||||||
/**
|
/**
|
||||||
* 获取WorldManager实例
|
* 获取WorldManager实例
|
||||||
*
|
*
|
||||||
|
* @param config 可选的WorldManager配置,用于覆盖默认配置
|
||||||
* @returns WorldManager实例,如果未初始化则自动创建
|
* @returns WorldManager实例,如果未初始化则自动创建
|
||||||
*/
|
*/
|
||||||
public static getWorldManager(): WorldManager {
|
public static getWorldManager(config?: Partial<IWorldManagerConfig>): WorldManager {
|
||||||
if (!this._instance) {
|
if (!this._instance) {
|
||||||
throw new Error("Core实例未创建,请先调用Core.create()");
|
throw new Error("Core实例未创建,请先调用Core.create()");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this._instance._worldManager) {
|
if (!this._instance._worldManager) {
|
||||||
// 多World模式的配置(用户主动获取WorldManager)
|
// 多World模式的配置(用户主动获取WorldManager)
|
||||||
this._instance._worldManager = WorldManager.getInstance({
|
const defaultConfig = {
|
||||||
maxWorlds: 50,
|
maxWorlds: 50,
|
||||||
autoCleanup: true,
|
autoCleanup: true,
|
||||||
cleanupInterval: 60000,
|
cleanupInterval: 60000,
|
||||||
debug: this._instance._config.debug
|
debug: this._instance._config.debug
|
||||||
|
};
|
||||||
|
|
||||||
|
this._instance._worldManager = WorldManager.getInstance({
|
||||||
|
...defaultConfig,
|
||||||
|
...config // 用户传入的配置会覆盖默认配置
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,9 +463,11 @@ export class Core {
|
|||||||
* 启用World管理
|
* 启用World管理
|
||||||
*
|
*
|
||||||
* 显式启用World功能,用于多房间/多世界架构
|
* 显式启用World功能,用于多房间/多世界架构
|
||||||
|
*
|
||||||
|
* @param config 可选的WorldManager配置,用于覆盖默认配置
|
||||||
*/
|
*/
|
||||||
public static enableWorldManager(): WorldManager {
|
public static enableWorldManager(config?: Partial<IWorldManagerConfig>): WorldManager {
|
||||||
return this.getWorldManager();
|
return this.getWorldManager(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,11 +14,35 @@ const originalConsoleError = console.error;
|
|||||||
|
|
||||||
// 在测试环境中可以选择性地静默某些日志
|
// 在测试环境中可以选择性地静默某些日志
|
||||||
beforeAll(() => {
|
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(() => {
|
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(() => {
|
afterEach(() => {
|
||||||
// 恢复所有模拟
|
// 恢复所有模拟
|
||||||
jest.restoreAllMocks();
|
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