|
|
@ -1,6 +1,6 @@ |
|
|
|
import Game from "./Game"; |
|
|
|
import Stage from "./Stage"; |
|
|
|
import {Container} from "pixi.js" |
|
|
|
import { Container, LoaderResource, Loader } from "pixi.js"; |
|
|
|
import { EP } from "@/enmu"; |
|
|
|
import TWEEN from "@tweenjs/tween.js"; |
|
|
|
|
|
|
@ -10,11 +10,21 @@ const stageManager = Stage.getInstance(); |
|
|
|
const windows: Record<string, IWindow>= {} |
|
|
|
const residentList: IWindow[] = [] |
|
|
|
|
|
|
|
function defineLoadResource(resources: {name: string, url: string, callback?: LoaderResource.OnCompleteSignal}[]) { |
|
|
|
return resources |
|
|
|
} |
|
|
|
|
|
|
|
function defineWindow(name: string, abstractClass: Constructor<IWindow>, type?: EP) { |
|
|
|
return { name, abstractClass, type} |
|
|
|
} |
|
|
|
|
|
|
|
const t = import.meta.globEager("../stages/**/page_*.ts"); |
|
|
|
for (const tKey in t) { |
|
|
|
const mod = t[tKey].default || t[tKey] |
|
|
|
const {name, abstractClass, type} = mod as ReturnType<typeof defineWindow> |
|
|
|
const stage = stageManager.getStage(name, type); |
|
|
|
const stageClass = new abstractClass(stage) |
|
|
|
if(EP.Resident === type) { |
|
|
|
stageClass.onLoad&&stageClass.onLoad() |
|
|
|
residentList.push(stageClass) |
|
|
|
} |
|
|
|
windows[name] = stageClass |
|
|
@ -24,44 +34,49 @@ function defineWindow(name: string, abstractClass: Constructor<IWindow>, type?: |
|
|
|
type Constructor<T> = new (...args: any[]) => T; |
|
|
|
interface IWindow { |
|
|
|
stage: Container; |
|
|
|
loader?: Loader; |
|
|
|
onLoad?(): void; |
|
|
|
onUnLoad?(): void; |
|
|
|
lateUpdate?(dt: number, name: string): void; |
|
|
|
update?(dt: number, name: string): void; |
|
|
|
} |
|
|
|
|
|
|
|
// 场景切换监听
|
|
|
|
stageManager.watchStageChange((stage, lastStage) => { |
|
|
|
if (lastStage && lastStage.type === EP.Normal) { |
|
|
|
let lastWindow = windows[lastStage.name]; |
|
|
|
lastWindow.onUnLoad && lastWindow.onUnLoad(); |
|
|
|
} |
|
|
|
if (stage && stage.type === EP.Normal) { |
|
|
|
let curWindow = windows[stage.name]; |
|
|
|
curWindow.onLoad && curWindow.onLoad(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
gameManager.ticker.add((dt: number) => { |
|
|
|
if (!stageManager.curStage) return; |
|
|
|
let curWindow = windows[stageManager.curStage.name]; |
|
|
|
TWEEN.update(); |
|
|
|
residentList.forEach((v) => v.update && v.update(dt, v.stage.name)); |
|
|
|
stageManager.curStage && |
|
|
|
function initGame() { |
|
|
|
// 场景切换监听
|
|
|
|
stageManager.watchStageChange((stage, lastStage) => { |
|
|
|
if (lastStage && lastStage.type === EP.Normal) { |
|
|
|
let lastWindow = windows[lastStage.name]; |
|
|
|
lastWindow.onUnLoad && lastWindow.onUnLoad(); |
|
|
|
} |
|
|
|
if (stage && stage.type === EP.Normal) { |
|
|
|
let curWindow = windows[stage.name]; |
|
|
|
curWindow.onLoad && curWindow.onLoad(); |
|
|
|
} |
|
|
|
}); |
|
|
|
residentList.forEach((v) => v.onLoad && v.onLoad()); |
|
|
|
gameManager.ticker.add((dt: number) => { |
|
|
|
if (!stageManager.curStage) return; |
|
|
|
let curWindow = windows[stageManager.curStage.name]; |
|
|
|
TWEEN.update(); |
|
|
|
residentList.forEach((v) => v.update && v.update(dt, v.stage.name)); |
|
|
|
stageManager.curStage && |
|
|
|
curWindow && |
|
|
|
curWindow.update && |
|
|
|
curWindow.update(dt, curWindow.stage.name); |
|
|
|
gameManager.render(dt); |
|
|
|
stageManager.curStage && |
|
|
|
gameManager.render(); |
|
|
|
stageManager.curStage && |
|
|
|
curWindow && |
|
|
|
curWindow.lateUpdate && |
|
|
|
curWindow.lateUpdate(dt, curWindow.stage.name); |
|
|
|
residentList.forEach((v) => v.lateUpdate && v.lateUpdate(dt, v.stage.name)); |
|
|
|
}); |
|
|
|
residentList.forEach((v) => v.lateUpdate && v.lateUpdate(dt, v.stage.name)); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
export { |
|
|
|
initGame, |
|
|
|
gameManager, |
|
|
|
stageManager, |
|
|
|
defineLoadResource, |
|
|
|
defineWindow, |
|
|
|
windows, |
|
|
|
residentList, |
|
|
|