Browse Source

feat: update getAllScenes to return array instead of iterator

master
npmrun 3 weeks ago
parent
commit
208bb6d208
  1. 74
      src/init.ts
  2. 5
      src/scene/SceneManager.ts

74
src/init.ts

@ -9,30 +9,37 @@ import eventBus from "./core/EventBus";
const game = Game.getInstance(); const game = Game.getInstance();
const sceneManager = SceneManager.getInstance(); const sceneManager = SceneManager.getInstance();
// 定义构造函数类型
type Constructor<T> = new (...args: any[]) => T;
export async function initApp(): Promise<void> { export async function initApp(): Promise<void> {
await assetManager.init(); await assetManager.init();
await game.init(); await game.init();
// 自动导入所有场景文件 src/scenes/**/page_*.ts // 自动导入所有场景文件 src/stages/**/page_*.ts
const sceneModules = import.meta.glob("./scenes/**/page_*.ts", { eager: true }); const sceneModules = import.meta.glob("./stages/**/page_*.ts", { eager: true });
for (const path in sceneModules) { for (const path in sceneModules) {
const mod = sceneModules[path]; try {
// 文件名匹配提取场景名称 const mod = sceneModules[path];
const match = path.match(/page_(.*?)\.ts$/); // 文件名匹配提取场景名称
if (!match) continue; const match = path.match(/page_(.*?)\.ts$/);
const sceneName = match[1]; if (!match) continue;
const sceneName = match[1];
const sceneClass = (mod as { default: Constructor<BaseScene> }).default;
const scene = new sceneClass(); const sceneClass = (mod as { default: Constructor<BaseScene> }).default;
const scene = new sceneClass();
// 如果模块默认导出不是场景构造函数,尝试其他格式(兼容旧定义)
if (!scene || typeof scene !== "object" || !("stage" in scene)) { // 如果模块默认导出不是场景构造函数,尝试其他格式(兼容旧定义)
logger.warn(`initApp: invalid scene file ${path}, skipping`); if (!scene || typeof scene !== "object" || !("stage" in scene)) {
continue; 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<void> {
const current = sceneManager.currentScene; const current = sceneManager.currentScene;
// 更新常驻场景 // 更新常驻场景
sceneManager["scenes"].forEach((scene) => { for (const scene of sceneManager.getAllScenes()) {
if (scene.type === SceneType.Resident && scene.stage.visible) { if (scene.type === SceneType.Resident && scene.stage.visible) {
scene.update?.(dt, ticker); scene.update?.(dt, ticker);
} }
}); }
// 更新当前场景 // 更新当前场景
if (current) { if (current) {
@ -55,27 +62,28 @@ export async function initApp(): Promise<void> {
game.render(); game.render();
// lateUpdate // lateUpdate
sceneManager["scenes"].forEach((scene) => { for (const scene of sceneManager.getAllScenes()) {
if (scene.type === SceneType.Resident && scene.stage.visible && scene.lateUpdate) { if (scene.type === SceneType.Resident && scene.stage.visible && scene.lateUpdate) {
scene.lateUpdate(dt, ticker); scene.lateUpdate(dt, ticker);
} }
}); }
if (current?.lateUpdate) { if (current?.lateUpdate) {
current.lateUpdate(dt, ticker); current.lateUpdate(dt, ticker);
} }
}); });
// 处理常驻场景初始化 // 初始化常驻场景
const residentScenes: BaseScene[] = []; const residentScenes: BaseScene[] = [];
sceneManager["scenes"].forEach((scene) => { for (const scene of sceneManager.getAllScenes()) {
if (scene.type === SceneType.Resident) { 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) { if (!scene._assetsLoaded) {
await scene.loadBundle?.(); await scene.loadBundle?.();
scene._assetsLoaded = true; scene._assetsLoaded = true;
@ -85,8 +93,18 @@ export async function initApp(): Promise<void> {
scene._layoutDone = true; scene._layoutDone = true;
} }
await scene.onLoad?.(); 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) => { sceneManager.onStageChange((current) => {

5
src/scene/SceneManager.ts

@ -222,6 +222,11 @@ class SceneManager {
return container; return container;
} }
/** 获取所有已注册场景 */
getAllScenes(): IBaseScene[] {
return Array.from(this.scenes.values());
}
hasScene(name: string): boolean { hasScene(name: string): boolean {
if (!name) { if (!name) {
logger.warn("SceneManager: scene name cannot be empty"); logger.warn("SceneManager: scene name cannot be empty");

Loading…
Cancel
Save