统一的World管理路径
This commit is contained in:
@@ -13,6 +13,7 @@ import { ServiceContainer } from './Core/ServiceContainer';
|
||||
import { PluginManager } from './Core/PluginManager';
|
||||
import { IPlugin } from './Core/Plugin';
|
||||
import { WorldManager } from './ECS/WorldManager';
|
||||
import { registerInjectable } from './Core/DI';
|
||||
|
||||
/**
|
||||
* 游戏引擎核心类
|
||||
@@ -177,9 +178,14 @@ export class Core {
|
||||
this._poolManager = new PoolManager();
|
||||
this._serviceContainer.registerInstance(PoolManager, this._poolManager);
|
||||
|
||||
// 初始化场景管理器
|
||||
this._sceneManager = new SceneManager();
|
||||
this._serviceContainer.registerInstance(SceneManager, this._sceneManager);
|
||||
// 使用依赖注入自动注册WorldManager和SceneManager
|
||||
// WorldManager会在构造时创建默认World
|
||||
registerInjectable(this._serviceContainer, WorldManager);
|
||||
this._worldManager = this._serviceContainer.resolve(WorldManager);
|
||||
|
||||
// SceneManager会通过@Inject自动获取WorldManager
|
||||
registerInjectable(this._serviceContainer, SceneManager);
|
||||
this._sceneManager = this._serviceContainer.resolve(SceneManager);
|
||||
|
||||
// 设置场景切换回调,通知调试管理器
|
||||
this._sceneManager.setSceneChangedCallback(() => {
|
||||
@@ -188,10 +194,6 @@ export class Core {
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化World管理器
|
||||
this._worldManager = new WorldManager();
|
||||
this._serviceContainer.registerInstance(WorldManager, this._worldManager);
|
||||
|
||||
// 初始化插件管理器
|
||||
this._pluginManager = new PluginManager();
|
||||
this._pluginManager.initialize(this, this._serviceContainer);
|
||||
@@ -645,7 +647,7 @@ export class Core {
|
||||
this._performanceMonitor.updateFPS(Time.deltaTime);
|
||||
}
|
||||
|
||||
// 更新所有可更新的服务
|
||||
// 更新所有@Updatable服务(包括TimerManager, WorldManager, SceneManager等)
|
||||
const servicesStartTime = this._performanceMonitor.startMonitoring('Services.update');
|
||||
this._serviceContainer.updateAll(deltaTime);
|
||||
this._performanceMonitor.endMonitoring('Services.update', servicesStartTime, this._serviceContainer.getUpdatableCount());
|
||||
@@ -653,12 +655,6 @@ export class Core {
|
||||
// 更新对象池管理器
|
||||
this._poolManager.update();
|
||||
|
||||
// 更新默认场景(通过 SceneManager)
|
||||
this._sceneManager.update();
|
||||
|
||||
// 更新额外的 WorldManager
|
||||
this._worldManager.updateAll();
|
||||
|
||||
// 更新调试管理器(基于FPS的数据发送)
|
||||
if (this._debugManager) {
|
||||
this._debugManager.onFrameUpdate(deltaTime);
|
||||
|
||||
@@ -3,7 +3,10 @@ import { ECSFluentAPI, createECSAPI } from './Core/FluentAPI';
|
||||
import { Time } from '../Utils/Time';
|
||||
import { createLogger } from '../Utils/Logger';
|
||||
import type { IService } from '../Core/ServiceContainer';
|
||||
import type { IUpdatable } from '../Types/IUpdatable';
|
||||
import { World } from './World';
|
||||
import { WorldManager } from './WorldManager';
|
||||
import { Injectable, Inject, Updatable } from '../Core/DI';
|
||||
|
||||
/**
|
||||
* 单场景管理器
|
||||
@@ -18,15 +21,13 @@ import { World } from './World';
|
||||
* - 简单直观的API
|
||||
* - 支持延迟场景切换
|
||||
* - 自动管理ECS API
|
||||
* - 通过依赖注入获取WorldManager的默认World
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 初始化Core
|
||||
* Core.create({ debug: true });
|
||||
*
|
||||
* // 创建场景管理器
|
||||
* const sceneManager = new SceneManager();
|
||||
*
|
||||
* // 设置场景
|
||||
* class GameScene extends Scene {
|
||||
* initialize() {
|
||||
@@ -35,21 +36,22 @@ import { World } from './World';
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* sceneManager.setScene(new GameScene());
|
||||
* Core.setScene(new GameScene());
|
||||
*
|
||||
* // 游戏循环
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime); // 更新全局服务
|
||||
* sceneManager.update(); // 更新场景
|
||||
* Core.update(deltaTime); // 自动更新所有服务(包括SceneManager)
|
||||
* }
|
||||
*
|
||||
* // 延迟切换场景(下一帧生效)
|
||||
* sceneManager.loadScene(new MenuScene());
|
||||
* Core.loadScene(new MenuScene());
|
||||
* ```
|
||||
*/
|
||||
export class SceneManager implements IService {
|
||||
@Injectable()
|
||||
@Updatable(10)
|
||||
export class SceneManager implements IService, IUpdatable {
|
||||
/**
|
||||
* 内部默认World
|
||||
* 内部默认World(从WorldManager获取)
|
||||
*/
|
||||
private _defaultWorld: World;
|
||||
|
||||
@@ -78,9 +80,18 @@ export class SceneManager implements IService {
|
||||
*/
|
||||
private static readonly DEFAULT_SCENE_ID = '__main__';
|
||||
|
||||
constructor() {
|
||||
this._defaultWorld = new World({ name: '__default__' });
|
||||
this._defaultWorld.start();
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* WorldManager通过依赖注入自动传入
|
||||
*
|
||||
* @param worldManager WorldManager实例,用于获取默认World
|
||||
*/
|
||||
constructor(
|
||||
@Inject(WorldManager) worldManager: WorldManager
|
||||
) {
|
||||
// 使用WorldManager管理的默认World
|
||||
this._defaultWorld = worldManager.getDefaultWorld();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { World, IWorldConfig } from './World';
|
||||
import { createLogger } from '../Utils/Logger';
|
||||
import type { IService } from '../Core/ServiceContainer';
|
||||
import type { IUpdatable } from '../Types/IUpdatable';
|
||||
import { Injectable, Updatable } from '../Core/DI';
|
||||
|
||||
const logger = createLogger('WorldManager');
|
||||
|
||||
@@ -12,21 +14,28 @@ export interface IWorldManagerConfig {
|
||||
* 最大World数量
|
||||
*/
|
||||
maxWorlds?: number;
|
||||
|
||||
|
||||
/**
|
||||
* 是否自动清理空World
|
||||
*/
|
||||
autoCleanup?: boolean;
|
||||
|
||||
|
||||
/**
|
||||
* 清理间隔(毫秒)
|
||||
*/
|
||||
cleanupInterval?: number;
|
||||
|
||||
|
||||
/**
|
||||
* 是否启用调试模式
|
||||
*/
|
||||
debug?: boolean;
|
||||
|
||||
/**
|
||||
* 是否创建默认World(默认true)
|
||||
*
|
||||
* 当通过Core使用时应该为true,直接使用WorldManager时可设为false
|
||||
*/
|
||||
createDefaultWorld?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,12 +66,18 @@ export interface IWorldManagerConfig {
|
||||
*
|
||||
* // 游戏循环
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime);
|
||||
* worldManager.updateAll(); // 更新所有活跃World
|
||||
* Core.update(deltaTime); // 自动更新所有@Updatable服务(包括WorldManager)
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class WorldManager implements IService {
|
||||
@Injectable()
|
||||
@Updatable(5)
|
||||
export class WorldManager implements IService, IUpdatable {
|
||||
/**
|
||||
* 默认World的ID
|
||||
*/
|
||||
public static readonly DEFAULT_WORLD_ID = '__default__';
|
||||
|
||||
private readonly _config: IWorldManagerConfig;
|
||||
private readonly _worlds: Map<string, World> = new Map();
|
||||
private readonly _activeWorlds: Set<string> = new Set();
|
||||
@@ -75,16 +90,26 @@ export class WorldManager implements IService {
|
||||
autoCleanup: true,
|
||||
cleanupInterval: 30000, // 30秒
|
||||
debug: false,
|
||||
createDefaultWorld: true, // 默认创建
|
||||
...config
|
||||
};
|
||||
|
||||
// 默认启动运行状态
|
||||
this._isRunning = true;
|
||||
|
||||
// 如果配置要求,创建并注册默认World
|
||||
if (this._config.createDefaultWorld) {
|
||||
const defaultWorld = new World({ name: WorldManager.DEFAULT_WORLD_ID });
|
||||
this._worlds.set(WorldManager.DEFAULT_WORLD_ID, defaultWorld);
|
||||
this._activeWorlds.add(WorldManager.DEFAULT_WORLD_ID);
|
||||
defaultWorld.start();
|
||||
}
|
||||
|
||||
logger.info('WorldManager已初始化', {
|
||||
maxWorlds: this._config.maxWorlds,
|
||||
autoCleanup: this._config.autoCleanup,
|
||||
cleanupInterval: this._config.cleanupInterval
|
||||
cleanupInterval: this._config.cleanupInterval,
|
||||
defaultWorldCreated: this._config.createDefaultWorld
|
||||
});
|
||||
|
||||
this.startCleanupTimer();
|
||||
@@ -92,6 +117,22 @@ export class WorldManager implements IService {
|
||||
|
||||
// ===== World管理 =====
|
||||
|
||||
/**
|
||||
* 获取默认World
|
||||
*
|
||||
* 默认World由WorldManager自动创建,供SceneManager使用。
|
||||
* 此方法主要供SceneManager内部使用。
|
||||
*
|
||||
* @returns 默认World实例
|
||||
*/
|
||||
public getDefaultWorld(): World {
|
||||
const defaultWorld = this._worlds.get(WorldManager.DEFAULT_WORLD_ID);
|
||||
if (!defaultWorld) {
|
||||
throw new Error('默认World不存在,这不应该发生');
|
||||
}
|
||||
return defaultWorld;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新World
|
||||
*/
|
||||
@@ -123,8 +164,16 @@ export class WorldManager implements IService {
|
||||
|
||||
/**
|
||||
* 移除World
|
||||
*
|
||||
* 注意:默认World不能被删除
|
||||
*/
|
||||
public removeWorld(worldId: string): boolean {
|
||||
// 防止删除默认World
|
||||
if (worldId === WorldManager.DEFAULT_WORLD_ID) {
|
||||
logger.warn('无法删除默认World');
|
||||
return false;
|
||||
}
|
||||
|
||||
const world = this._worlds.get(worldId);
|
||||
if (!world) {
|
||||
return false;
|
||||
@@ -197,18 +246,12 @@ export class WorldManager implements IService {
|
||||
/**
|
||||
* 更新所有活跃的World
|
||||
*
|
||||
* 应该在每帧的游戏循环中调用。
|
||||
* 此方法由ServiceContainer自动调用(@Updatable装饰器)
|
||||
* 会自动更新所有活跃World的全局系统和场景。
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime); // 更新全局服务
|
||||
* worldManager.updateAll(); // 更新所有World
|
||||
* }
|
||||
* ```
|
||||
* @param deltaTime 帧时间间隔(未使用,保留用于接口兼容)
|
||||
*/
|
||||
public updateAll(): void {
|
||||
public update(deltaTime?: number): void {
|
||||
if (!this._isRunning) return;
|
||||
|
||||
for (const worldId of this._activeWorlds) {
|
||||
|
||||
Reference in New Issue
Block a user