|
|
@ -1,10 +1,12 @@ |
|
|
import Game from "./core/Game"; |
|
|
import Game from "./core/Game"; |
|
|
import SceneManager from "./scene/SceneManager"; |
|
|
import SceneManager from "./scene/SceneManager"; |
|
|
import { BaseScene } from "./scene/BaseScene"; |
|
|
import { BaseScene } from "./scene/BaseScene"; |
|
|
|
|
|
import type { IBaseScene } from "./scene/types"; |
|
|
import { SceneType } from "./enums/SceneType"; |
|
|
import { SceneType } from "./enums/SceneType"; |
|
|
import { assetManager } from "./core/AssetManager"; |
|
|
import { assetManager } from "./core/AssetManager"; |
|
|
import { logger } from "./core/Logger"; |
|
|
import { logger } from "./core/Logger"; |
|
|
import eventBus from "./core/EventBus"; |
|
|
import eventBus from "./core/EventBus"; |
|
|
|
|
|
import { Container } from "pixi.js"; |
|
|
|
|
|
|
|
|
const game = Game.getInstance(); |
|
|
const game = Game.getInstance(); |
|
|
const sceneManager = SceneManager.getInstance(); |
|
|
const sceneManager = SceneManager.getInstance(); |
|
|
@ -12,6 +14,14 @@ const sceneManager = SceneManager.getInstance(); |
|
|
// 定义构造函数类型
|
|
|
// 定义构造函数类型
|
|
|
type Constructor<T> = new (...args: any[]) => T; |
|
|
type Constructor<T> = new (...args: any[]) => T; |
|
|
|
|
|
|
|
|
|
|
|
/** defineWindow() 返回的旧版模块形状 */ |
|
|
|
|
|
type DefineWindowModule = { |
|
|
|
|
|
abstractClass: Constructor<IBaseScene & Record<string, unknown>>; |
|
|
|
|
|
name?: string; |
|
|
|
|
|
/** 与 SceneType 数值一致(旧 EP) */ |
|
|
|
|
|
type?: SceneType; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
export async function initApp(): Promise<void> { |
|
|
export async function initApp(): Promise<void> { |
|
|
await assetManager.init(); |
|
|
await assetManager.init(); |
|
|
await game.init(); |
|
|
await game.init(); |
|
|
@ -25,17 +35,52 @@ export async function initApp(): Promise<void> { |
|
|
// 文件名匹配提取场景名称
|
|
|
// 文件名匹配提取场景名称
|
|
|
const match = path.match(/page_(.*?)\.ts$/); |
|
|
const match = path.match(/page_(.*?)\.ts$/); |
|
|
if (!match) continue; |
|
|
if (!match) continue; |
|
|
|
|
|
const fileSceneName = match[1]; |
|
|
|
|
|
|
|
|
|
|
|
const raw = (mod as { default: unknown }).default; |
|
|
|
|
|
let SceneCtor: Constructor<IBaseScene & Record<string, unknown>>; |
|
|
|
|
|
let defineWindowName: string | undefined; |
|
|
|
|
|
let defineWindowType: SceneType | undefined; |
|
|
|
|
|
|
|
|
|
|
|
if (typeof raw === "function") { |
|
|
|
|
|
SceneCtor = raw as Constructor<IBaseScene & Record<string, unknown>>; |
|
|
|
|
|
} else if ( |
|
|
|
|
|
raw && |
|
|
|
|
|
typeof raw === "object" && |
|
|
|
|
|
"abstractClass" in raw && |
|
|
|
|
|
typeof (raw as DefineWindowModule).abstractClass === "function" |
|
|
|
|
|
) { |
|
|
|
|
|
const dw = raw as DefineWindowModule; |
|
|
|
|
|
SceneCtor = dw.abstractClass; |
|
|
|
|
|
defineWindowName = dw.name; |
|
|
|
|
|
defineWindowType = dw.type; |
|
|
|
|
|
} else { |
|
|
|
|
|
logger.warn(`initApp: invalid scene file ${path}, skipping`); |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const sceneClass = (mod as { default: Constructor<BaseScene> }).default; |
|
|
const scene = new SceneCtor(); |
|
|
const scene = new sceneClass(); |
|
|
|
|
|
|
|
|
|
|
|
// 如果模块默认导出不是场景构造函数,尝试其他格式(兼容旧定义)
|
|
|
|
|
|
if (!scene || typeof scene !== "object" || !("stage" in scene)) { |
|
|
if (!scene || typeof scene !== "object" || !("stage" in scene)) { |
|
|
logger.warn(`initApp: invalid scene file ${path}, skipping`); |
|
|
logger.warn(`initApp: invalid scene file ${path}, skipping`); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
sceneManager.registerScene(scene); |
|
|
const legacy = scene as IBaseScene & |
|
|
|
|
|
Record<string, unknown> & { name?: string; type?: SceneType; _type?: SceneType }; |
|
|
|
|
|
// defineWindow / PWindow 实例缺少只读字段的运行时值,用可变写入兼容旧场景
|
|
|
|
|
|
const mutable = legacy as { name?: string; type?: SceneType; stage: Container | null }; |
|
|
|
|
|
if (!mutable.name) { |
|
|
|
|
|
mutable.name = defineWindowName ?? fileSceneName; |
|
|
|
|
|
} |
|
|
|
|
|
if (mutable.type === undefined) { |
|
|
|
|
|
mutable.type = defineWindowType ?? legacy._type ?? SceneType.Normal; |
|
|
|
|
|
} |
|
|
|
|
|
if (!mutable.stage || mutable.stage === null) { |
|
|
|
|
|
mutable.stage = new Container(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sceneManager.registerScene(legacy as IBaseScene); |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
logger.error(`initApp: failed to load scene file ${path}`, error); |
|
|
logger.error(`initApp: failed to load scene file ${path}`, error); |
|
|
} |
|
|
} |
|
|
@ -100,7 +145,7 @@ export async function initApp(): Promise<void> { |
|
|
// 初始化入口场景
|
|
|
// 初始化入口场景
|
|
|
const entryScene = "init"; |
|
|
const entryScene = "init"; |
|
|
if (sceneManager.hasScene(entryScene)) { |
|
|
if (sceneManager.hasScene(entryScene)) { |
|
|
sceneManager.initScene(entryScene); |
|
|
await sceneManager.initScene(entryScene); |
|
|
} else { |
|
|
} else { |
|
|
logger.error(`initApp: entry scene "${entryScene}" not found`); |
|
|
logger.error(`initApp: entry scene "${entryScene}" not found`); |
|
|
} |
|
|
} |
|
|
|