统一World与Scene架构,SceneManager内部使用DefaultWorld

This commit is contained in:
YHH
2025-10-11 14:27:09 +08:00
parent 6e48f22540
commit c19b5ae9a7
3 changed files with 64 additions and 23 deletions

View File

@@ -248,6 +248,30 @@ export class Core {
return this._instance._serviceContainer;
}
/**
* 获取World管理器
*
* 用于管理多个独立的World实例高级用户
*
* @returns WorldManager实例
* @throws 如果Core实例未创建
*
* @example
* ```typescript
* // 创建多个游戏房间
* const wm = Core.worldManager;
* const room1 = wm.createWorld('room_001');
* room1.createScene('game', new GameScene());
* room1.start();
* ```
*/
public static get worldManager(): WorldManager {
if (!this._instance) {
throw new Error('Core实例未创建请先调用Core.create()');
}
return this._instance._worldManager;
}
/**
* 创建Core实例
*
@@ -629,9 +653,12 @@ export class Core {
// 更新对象池管理器
this._poolManager.update();
// 更新场景
// 更新默认场景(通过 SceneManager
this._sceneManager.update();
// 更新额外的 WorldManager
this._worldManager.updateAll();
// 更新调试管理器基于FPS的数据发送
if (this._debugManager) {
this._debugManager.onFrameUpdate(deltaTime);

View File

@@ -3,6 +3,7 @@ import { ECSFluentAPI, createECSAPI } from './Core/FluentAPI';
import { Time } from '../Utils/Time';
import { createLogger } from '../Utils/Logger';
import type { IService } from '../Core/ServiceContainer';
import { World } from './World';
/**
* 单场景管理器
@@ -48,9 +49,9 @@ import type { IService } from '../Core/ServiceContainer';
*/
export class SceneManager implements IService {
/**
* 当前活跃场景
* 内部默认World
*/
private _currentScene: IScene | null = null;
private _defaultWorld: World;
/**
* 待切换的下一个场景(延迟切换用)
@@ -72,6 +73,16 @@ export class SceneManager implements IService {
*/
private _onSceneChangedCallback?: () => void;
/**
* 默认场景ID
*/
private static readonly DEFAULT_SCENE_ID = '__main__';
constructor() {
this._defaultWorld = new World({ name: '__default__' });
this._defaultWorld.start();
}
/**
* 设置场景切换回调
*
@@ -97,16 +108,12 @@ export class SceneManager implements IService {
* ```
*/
public setScene<T extends IScene>(scene: T): T {
// 结束旧场景
if (this._currentScene) {
this._logger.info(`Ending scene: ${this._currentScene.name}`);
this._currentScene.end();
}
// 移除旧场景
this._defaultWorld.removeAllScenes();
// 设置并初始化新场景
this._currentScene = scene;
this._currentScene.initialize();
this._currentScene.begin();
// 通过 World 创建新场景
this._defaultWorld.createScene(SceneManager.DEFAULT_SCENE_ID, scene);
this._defaultWorld.setSceneActive(SceneManager.DEFAULT_SCENE_ID, true);
// 重建ECS API
if (scene.querySystem && scene.eventSystem) {
@@ -160,7 +167,7 @@ export class SceneManager implements IService {
* @returns 当前场景实例如果没有场景则返回null
*/
public get currentScene(): IScene | null {
return this._currentScene;
return this._defaultWorld.getScene(SceneManager.DEFAULT_SCENE_ID);
}
/**
@@ -207,10 +214,9 @@ export class SceneManager implements IService {
this._nextScene = null;
}
// 更新当前场景
if (this._currentScene) {
this._currentScene.update();
}
// 通过 World 统一更新
this._defaultWorld.updateGlobalSystems();
this._defaultWorld.updateScenes();
}
/**
@@ -220,12 +226,9 @@ export class SceneManager implements IService {
* 通常在应用程序关闭时调用。
*/
public destroy(): void {
if (this._currentScene) {
this._logger.info(`Destroying scene: ${this._currentScene.name}`);
this._currentScene.end();
this._currentScene = null;
}
this._logger.info('SceneManager destroying');
this._defaultWorld.destroy();
this._nextScene = null;
this._ecsAPI = null;
@@ -238,7 +241,7 @@ export class SceneManager implements IService {
* @returns 如果有活跃场景返回true否则返回false
*/
public get hasScene(): boolean {
return this._currentScene !== null;
return this._defaultWorld.getScene(SceneManager.DEFAULT_SCENE_ID) !== null;
}
/**

View File

@@ -183,6 +183,17 @@ export class World {
return Array.from(this._scenes.values());
}
/**
* 移除所有Scene
*/
public removeAllScenes(): void {
const sceneIds = Array.from(this._scenes.keys());
for (const sceneId of sceneIds) {
this.removeScene(sceneId);
}
logger.info(`从World '${this.name}' 中移除所有Scene`);
}
/**
* 设置Scene激活状态
*/