import * as THREE from "three"; import { BaseScene } from "./scenes/BaseScene"; import type { SceneKey } from "./SceneKeys"; /** * 场景生命周期管理器。 * * 管理多个 BaseScene 实例的注册、切换和更新。 * 同一时刻只有一个活跃场景,切换时自动调用 exit/enter。 */ export class SceneManager { private scenes = new Map(); private _activeScene: BaseScene | null = null; private _camera: THREE.PerspectiveCamera; constructor(camera: THREE.PerspectiveCamera) { this._camera = camera; } get activeScene(): BaseScene | null { return this._activeScene; } get activeThreeScene(): THREE.Scene | null { return this._activeScene?.scene ?? null; } register(...scenes: BaseScene[]): void { for (const s of scenes) { s._bind((key) => this.switchTo(key)); this.scenes.set(s.key, s); } } switchTo(key: SceneKey): void { const next = this.scenes.get(key); if (!next) { console.warn(`[SceneManager] Scene "${key}" not registered`); return; } if (this._activeScene) { this._activeScene.exit(); } if (!next["_initialized"]) { next.init(this._camera); } next.enter(); this._activeScene = next; if (typeof __DEV__ !== "undefined" && __DEV__) { console.log(`[SceneManager] → ${key}`); } } update(dt: number, elapsed: number): void { this._activeScene?.update(dt, elapsed); } dispose(): void { for (const s of this.scenes.values()) { s.dispose(); } this.scenes.clear(); this._activeScene = null; } }