Skip to content

Instantly share code, notes, and snippets.

@yangfch3 yangfch3/SceneManager.ts
Last active Nov 8, 2018

Embed
What would you like to do?
[白鹭场景管理] 快捷有序地进行白鹭场景的管理 #白鹭 #游戏开发
///<reference path="./utils/array.ts" />
/**
* 控制场景的切换
* 与 Main 协同工作
*/
class SceneManager {
private static instance: SceneManager = null
static getInstance() {
if (!SceneManager.instance) {
new SceneManager()
}
return SceneManager.instance
}
private stackLayer: egret.DisplayObjectContainer[] = []
private sceneNameMap = {}
private gameLayer: Main = null
constructor () {
if (!SceneManager.instance) {
SceneManager.instance = this
}
return SceneManager.instance
}
public initWithMain(m: Main) {
if (this.gameLayer === null) {
this.gameLayer = m
}
}
public getCurScene(): egret.DisplayObjectContainer {
return this.stackLayer[this.stackLayer.length - 1]
}
public getSceneByName(name: string): egret.DisplayObjectContainer {
return this.sceneNameMap[name]
}
/**
* 替换 scene
* all 为 true 时会清除现有场景栈里的所有场景
*/
public repleaceScene(layer: egret.DisplayObjectContainer, all?: boolean) {
if (this.gameLayer !== null && layer !== null) {
if (all) {
this.gameLayer.removeChildren()
this.stackLayer = []
this.sceneNameMap = {}
} else {
this.popScene()
}
this.pushScene(layer)
}
}
public pushScene(layer: egret.DisplayObjectContainer) {
if (this.gameLayer !== null && layer !== null) {
this.gameLayer.addChild(layer)
let coveredLayer = this.stackLayer[this.stackLayer.length - 1]
// 告知被盖住的场景
coveredLayer && coveredLayer.dispatchEvent(new egret.Event('coverd'))
// 防止点穿
if (coveredLayer) {
coveredLayer.touchEnabled = false
coveredLayer.touchChildren = false
coveredLayer.visible = false
}
let layerId = this.stackLayer.push(layer)
if (typeof layer.name === 'string') {
this.sceneNameMap[layer.name] = layer
} else {
this.sceneNameMap[layerId] = layer
}
}
}
public popScene() {
if (this.gameLayer !== null) {
let len = this.stackLayer.length
if (len > 0) {
let layer = this.stackLayer[len - 1]
let liftLayer = this.stackLayer[len - 2]
if (layer.parent === this.gameLayer) {
this.gameLayer.removeChild(layer)
if (liftLayer) {
liftLayer.dispatchEvent(new egret.Event('lift'))
liftLayer.touchEnabled = true
liftLayer.touchChildren = true
liftLayer.visible = true
}
this.stackLayer.pop()
if (typeof layer.name === 'string') {
delete this.sceneNameMap[layer.name]
} else {
delete this.sceneNameMap[len - 1]
}
}
}
}
}
/**
* 以下为特殊定制的接口
* 可以根据需要自行定制
*/
resetToIndex() {
this.repleaceScene(this.stackLayer[0], true)
this.stackLayer[0].dispatchEvent(new egret.Event('lift'))
this.stackLayer[0].touchEnabled = true
this.stackLayer[0].touchChildren = true
this.stackLayer[0].visible = true
SoundManager.getInstance().bgmStop()
}
refresh() {
this.gameLayer && this.gameLayer.refresh && this.gameLayer.refresh()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.