You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

112 lines
3.3 KiB

import { EP } from "@/enmu";
import { Container } from "pixi.js";
import Game from "./Game";
type TStage = {
type: EP,
name: string,
stage: Container
}
export default class Stage {
private static instance: Stage = null
private _stages: Record<string,TStage>= {}
curStage: TStage = null
static getInstance(){
if(Stage.instance == null){
Stage.instance = new Stage()
}
return Stage.instance
}
gameManager: Game = null
constructor(){
this.gameManager = Game.getInstance();
}
private _wscb: ((stage: TStage, lastStage?:TStage)=>void)[]= []
watchStageChange(cb: (stage: TStage, lastStage?:TStage)=>void){
this._wscb.push(cb)
}
initStage(name: string){
if(!this._stages[name]){
throw new Error("不存在该舞台")
}
let curStage = this._stages[name];
this.curStage = curStage
// this.gameManager.stage.children.forEach(stage=>{
// stage.destroy()
// this.gameManager.stage.removeChild(stage)
// })
if(curStage&&curStage.type === EP.Normal){
this.gameManager.stage.addChild(curStage.stage)
}else if(curStage&&curStage.type === EP.Resident){
curStage.stage.visible = true
}
this._wscb.forEach(v=>v(curStage))
}
changeStage(name: string){
if(!this._stages[name]){
throw new Error("不存在该舞台")
}
if(!this.curStage){
console.warn("请先初始化一个场景")
return
}
let lastStage = this.curStage
if(lastStage&&lastStage.type === EP.Normal){
this.gameManager.stage.removeChild(lastStage.stage)
}else if(lastStage&&lastStage.type === EP.Resident){
lastStage.stage.visible = false
}
let curStage = this._stages[name];
if(curStage && typeof curStage === "string"){
this.create(name, EP.Normal)
curStage = this._stages[name]
console.log(curStage);
}
this.curStage = curStage
if(curStage&&curStage.type === EP.Normal){
this.gameManager.stage.addChild(curStage.stage)
}else if(curStage&&curStage.type === EP.Resident){
curStage.stage.visible = true
}
this._wscb.forEach(v=>v(curStage, lastStage))
if(lastStage&&lastStage.type === EP.Normal){
lastStage.stage.destroy()
this._stages[lastStage.name] = "已销毁" as any
}
}
getStage(name: string, type: EP = EP.Normal): Container{
if(name === "root"){
throw new Error("最好换过一个名字")
}
if(this._stages[name]){
throw new Error("已存在该舞台")
}
if(!this._stages[name]){
this.create(name, type)
}
return this._stages[name].stage
}
private create(name: string, type: EP = EP.Normal): Container{
const stage = new Container();
stage.name = name
this._stages[name] = {
type: type,
stage: stage,
name: name
}
if(type === EP.Resident){
stage.visible = false
this.gameManager.stage.addChild(stage)
}
return stage
}
}