改寫原本的 Day 06 的 ScenesManger
switch scence with ScenesManager by ts
class ScenesManager {
private static scenes: any = {}; // should be hashmap but a JS object is fine too :)
public static currentScene: Scene;
public create(width: number, height: number, scale:bool=false) {
// requestAnimFrame(ScenesManager.loop);
var canvas = document.getElementById('canvas')
canvas.width = width
canvas.height = height
return this;
}
public createScene(id: string, TScene: void) {
console.log('id: ', id)
console.log('TScene: ', TScene)
if (ScenesManager.scenes[id]) return undefined;
// if (ScenesManager.scenes[id]) return undefined;
// var scene = new TScene();
ScenesManager.scenes[id] = TScene;
console.log('ScenesManager.scenes: ', ScenesManager.scenes)
// return scene;
}
public clearScene() {
var canvas = document.getElementById('canvas')
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
public goToScene(id: string): bool {
this.clearScene()
if (ScenesManager.scenes[id]) {
// if (ScenesManager.currentScene) ScenesManager.currentScene.pause();
ScenesManager.currentScene = ScenesManager.scenes[id];
// ScenesManager.currentScene.resume();
ScenesManager.scenes[id]()
return true;
}
return false;
}
}