feat(core): 为 World 添加独立的服务容器 (#222)

* feat(core): 为 World 添加独立的服务容器

* test(core): 为 World 服务容器添加完整测试覆盖
This commit is contained in:
YHH
2025-11-14 09:55:31 +08:00
committed by GitHub
parent 609baace73
commit 3a0544629d
3 changed files with 135 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ import { IScene } from './IScene';
import { Scene } from './Scene';
import { createLogger } from '../Utils/Logger';
import { PerformanceMonitor } from '../Utils/PerformanceMonitor';
import { ServiceContainer } from '../Core/ServiceContainer';
const logger = createLogger('World');
@@ -65,6 +66,13 @@ export interface IWorldConfig {
* World类 - ECS世界管理器
*
* World是Scene的容器每个World可以管理多个Scene。
* World拥有独立的服务容器用于管理World级别的全局服务。
*
* 服务容器层级:
* - Core.services: 应用程序全局服务
* - World.services: World级别服务每个World独立
* - Scene.services: Scene级别服务每个Scene独立
*
* 这种设计允许创建独立的游戏世界,如:
* - 游戏房间每个房间一个World
* - 不同的游戏模式
@@ -75,10 +83,16 @@ export interface IWorldConfig {
* // 创建游戏房间的World
* const roomWorld = new World({ name: 'Room_001' });
*
* // 注册World级别的服务
* roomWorld.services.registerSingleton(RoomManager);
*
* // 在World中创建Scene
* const gameScene = roomWorld.createScene('game', new Scene());
* const uiScene = roomWorld.createScene('ui', new Scene());
*
* // 在Scene中使用World级别的服务
* const roomManager = roomWorld.services.resolve(RoomManager);
*
* // 更新整个World
* roomWorld.update(deltaTime);
* ```
@@ -89,6 +103,7 @@ export class World {
private readonly _scenes: Map<string, IScene> = new Map();
private readonly _activeScenes: Set<string> = new Set();
private readonly _globalSystems: IGlobalSystem[] = [];
private readonly _services: ServiceContainer;
private _isActive: boolean = false;
private _createdAt: number;
@@ -103,6 +118,17 @@ export class World {
this.name = this._config.name!;
this._createdAt = Date.now();
this._services = new ServiceContainer();
}
// ===== 服务容器 =====
/**
* World级别的服务容器
* 用于管理World范围内的全局服务
*/
public get services(): ServiceContainer {
return this._services;
}
// ===== Scene管理 =====
@@ -398,6 +424,9 @@ export class World {
}
this._globalSystems.length = 0;
// 清空服务容器
this._services.clear();
this._scenes.clear();
this._activeScenes.clear();
}