2020-06-08 11:49:45 +08:00
|
|
|
|
/** 运行时的场景管理。 */
|
|
|
|
|
|
class SceneManager {
|
2020-06-21 10:27:15 +08:00
|
|
|
|
private static _scene: Scene;
|
|
|
|
|
|
private static _nextScene: Scene;
|
|
|
|
|
|
public static sceneTransition: SceneTransition;
|
2020-06-22 15:27:58 +08:00
|
|
|
|
public static stage: egret.Stage;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
|
constructor(stage: egret.Stage) {
|
|
|
|
|
|
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
|
|
|
|
|
|
|
2020-06-22 15:27:58 +08:00
|
|
|
|
SceneManager.stage = stage;
|
2020-06-21 10:27:15 +08:00
|
|
|
|
SceneManager.initialize(stage);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static get scene() {
|
|
|
|
|
|
return this._scene;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static set scene(value: Scene) {
|
|
|
|
|
|
if (!value)
|
|
|
|
|
|
throw new Error("场景不能为空");
|
|
|
|
|
|
|
|
|
|
|
|
if (this._scene == null) {
|
|
|
|
|
|
this._scene = value;
|
|
|
|
|
|
this._scene.begin();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this._nextScene = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static initialize(stage: egret.Stage) {
|
|
|
|
|
|
Input.initialize(stage);
|
2020-06-08 11:49:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
|
public static update() {
|
|
|
|
|
|
Time.update(egret.getTimer());
|
|
|
|
|
|
|
|
|
|
|
|
if (SceneManager._scene) {
|
|
|
|
|
|
for (let i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
|
|
|
|
|
|
if (GlobalManager.globalManagers[i].enabled)
|
|
|
|
|
|
GlobalManager.globalManagers[i].update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!SceneManager.sceneTransition ||
|
|
|
|
|
|
(SceneManager.sceneTransition && (!SceneManager.sceneTransition.loadsNewScene || SceneManager.sceneTransition.isNewSceneLoaded))) {
|
|
|
|
|
|
SceneManager._scene.update();
|
|
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
|
if (SceneManager._nextScene) {
|
|
|
|
|
|
SceneManager._scene.end();
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
|
|
|
|
|
|
let entity = SceneManager._scene.entities.buffer[i];
|
2020-06-29 15:41:02 +08:00
|
|
|
|
entity.destroy();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SceneManager._scene = SceneManager._nextScene;
|
|
|
|
|
|
SceneManager._nextScene = null;
|
|
|
|
|
|
|
|
|
|
|
|
SceneManager._scene.begin();
|
|
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
|
SceneManager.render();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static render() {
|
2020-06-22 15:27:58 +08:00
|
|
|
|
if (this.sceneTransition){
|
2020-06-21 10:27:15 +08:00
|
|
|
|
this.sceneTransition.preRender();
|
|
|
|
|
|
|
2020-06-22 15:27:58 +08:00
|
|
|
|
if (this._scene && !this.sceneTransition.hasPreviousSceneRender){
|
2020-06-23 09:10:40 +08:00
|
|
|
|
this._scene.render();
|
|
|
|
|
|
this._scene.postRender();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
this.sceneTransition.onBeginTransition();
|
2020-06-22 15:27:58 +08:00
|
|
|
|
} else if (this.sceneTransition) {
|
|
|
|
|
|
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
|
|
|
|
|
|
this._scene.render();
|
2020-06-23 09:10:40 +08:00
|
|
|
|
this._scene.postRender();
|
2020-06-22 15:27:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.sceneTransition.render();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
}
|
2020-06-23 09:10:40 +08:00
|
|
|
|
} else if (this._scene) {
|
|
|
|
|
|
this._scene.render();
|
2020-07-09 16:16:04 +08:00
|
|
|
|
|
|
|
|
|
|
Debug.render();
|
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
|
this._scene.postRender();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 临时运行SceneTransition,允许一个场景过渡到另一个平滑的自定义效果。
|
|
|
|
|
|
* @param sceneTransition
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T {
|
2020-06-22 15:27:58 +08:00
|
|
|
|
if (this.sceneTransition) {
|
2020-06-23 17:36:39 +08:00
|
|
|
|
console.warn("在前一个场景完成之前,不能开始一个新的场景转换。");
|
2020-06-23 16:18:14 +08:00
|
|
|
|
return;
|
2020-06-21 10:27:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.sceneTransition = sceneTransition;
|
|
|
|
|
|
return sceneTransition;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
}
|