diff --git a/src/init.ts b/src/init.ts index c2a0e5a..98ffa18 100644 --- a/src/init.ts +++ b/src/init.ts @@ -9,30 +9,37 @@ import eventBus from "./core/EventBus"; const game = Game.getInstance(); const sceneManager = SceneManager.getInstance(); +// 定义构造函数类型 +type Constructor = new (...args: any[]) => T; + export async function initApp(): Promise { await assetManager.init(); await game.init(); - // 自动导入所有场景文件 src/scenes/**/page_*.ts - const sceneModules = import.meta.glob("./scenes/**/page_*.ts", { eager: true }); + // 自动导入所有场景文件 src/stages/**/page_*.ts + const sceneModules = import.meta.glob("./stages/**/page_*.ts", { eager: true }); for (const path in sceneModules) { - const mod = sceneModules[path]; - // 文件名匹配提取场景名称 - const match = path.match(/page_(.*?)\.ts$/); - if (!match) continue; - const sceneName = match[1]; - - const sceneClass = (mod as { default: Constructor }).default; - const scene = new sceneClass(); - - // 如果模块默认导出不是场景构造函数,尝试其他格式(兼容旧定义) - if (!scene || typeof scene !== "object" || !("stage" in scene)) { - logger.warn(`initApp: invalid scene file ${path}, skipping`); - continue; - } + try { + const mod = sceneModules[path]; + // 文件名匹配提取场景名称 + const match = path.match(/page_(.*?)\.ts$/); + if (!match) continue; + const sceneName = match[1]; + + const sceneClass = (mod as { default: Constructor }).default; + const scene = new sceneClass(); + + // 如果模块默认导出不是场景构造函数,尝试其他格式(兼容旧定义) + if (!scene || typeof scene !== "object" || !("stage" in scene)) { + logger.warn(`initApp: invalid scene file ${path}, skipping`); + continue; + } - sceneManager.registerScene(scene); + sceneManager.registerScene(scene); + } catch (error) { + logger.error(`initApp: failed to load scene file ${path}`, error); + } } // 启动更新循环 @@ -41,11 +48,11 @@ export async function initApp(): Promise { const current = sceneManager.currentScene; // 更新常驻场景 - sceneManager["scenes"].forEach((scene) => { + for (const scene of sceneManager.getAllScenes()) { if (scene.type === SceneType.Resident && scene.stage.visible) { scene.update?.(dt, ticker); } - }); + } // 更新当前场景 if (current) { @@ -55,27 +62,28 @@ export async function initApp(): Promise { game.render(); // lateUpdate - sceneManager["scenes"].forEach((scene) => { + for (const scene of sceneManager.getAllScenes()) { if (scene.type === SceneType.Resident && scene.stage.visible && scene.lateUpdate) { scene.lateUpdate(dt, ticker); } - }); + } if (current?.lateUpdate) { current.lateUpdate(dt, ticker); } }); - // 处理常驻场景初始化 + // 初始化常驻场景 const residentScenes: BaseScene[] = []; - sceneManager["scenes"].forEach((scene) => { + for (const scene of sceneManager.getAllScenes()) { if (scene.type === SceneType.Resident) { - residentScenes.push(scene); + residentScenes.push(scene as BaseScene); } - }); + } - (async () => { - for (const scene of residentScenes) { + // 初始化常驻场景 + for (const scene of residentScenes) { + try { if (!scene._assetsLoaded) { await scene.loadBundle?.(); scene._assetsLoaded = true; @@ -85,8 +93,18 @@ export async function initApp(): Promise { scene._layoutDone = true; } await scene.onLoad?.(); + } catch (error) { + logger.error(`initApp: failed to initialize resident scene ${scene.name}`, error); } - })(); + } + + // 初始化入口场景 + const entryScene = "init"; + if (sceneManager.hasScene(entryScene)) { + sceneManager.initScene(entryScene); + } else { + logger.error(`initApp: entry scene "${entryScene}" not found`); + } // 场景变化监听 - 日志 sceneManager.onStageChange((current) => { diff --git a/src/scene/SceneManager.ts b/src/scene/SceneManager.ts index 0b4f18f..bb296cd 100644 --- a/src/scene/SceneManager.ts +++ b/src/scene/SceneManager.ts @@ -222,6 +222,11 @@ class SceneManager { return container; } + /** 获取所有已注册场景 */ + getAllScenes(): IBaseScene[] { + return Array.from(this.scenes.values()); + } + hasScene(name: string): boolean { if (!name) { logger.warn("SceneManager: scene name cannot be empty");