assassin-bug/framework/scene/manager.js

34 lines
906 B
JavaScript
Raw Normal View History

2022-11-26 01:22:02 +00:00
export class SceneManager {
constructor() {
this.scenes = new Map();
}
init() {
if (this.defaultScene) {
this.switchTo(this.defaultScene);
}
}
addScene(scene) {
this.scenes.set(scene.id, scene);
}
removeScene(scene) {
if (scene === this.currentScene)
this.currentScene.onDeactivate();
this.scenes.delete(scene.id);
}
switchTo(scene) {
if (scene === this.currentScene)
return;
let data;
if (this.currentScene) {
this.currentScene.onDeactivate();
data = this.currentScene.data;
}
this.currentScene = this.scenes.get(scene.id);
this.currentScene.onSwitch(data);
this.currentScene.onActivate(this);
}
setDefaultScene(scene) {
this.defaultScene = scene;
}
}