|
@@ -17,14 +17,15 @@ var Game = {
|
|
|
sceneStack: [],
|
|
|
currentScene: null,
|
|
|
update: function(delta) {
|
|
|
- if(this.currentScene) this.currentScene.update(delta);
|
|
|
+ if(this.currentScene) this.currentScene._update(delta);
|
|
|
},
|
|
|
draw: function() {
|
|
|
- if(this.currentScene) this.currentScene.draw();
|
|
|
+ if(this.currentScene) this.currentScene._draw();
|
|
|
},
|
|
|
push: function(scene) {
|
|
|
if(this.currentScene) this.currentScene.pause();
|
|
|
this.sceneStack.push(scene);
|
|
|
+ if(!scene.hasTicker) scene._init();
|
|
|
scene.load();
|
|
|
this.currentScene = scene;
|
|
|
},
|
|
@@ -61,18 +62,28 @@ var Game = {
|
|
|
});
|
|
|
})();
|
|
|
|
|
|
-var MainMenu = {
|
|
|
- update: function() {},
|
|
|
- draw: function() {
|
|
|
- Game.context.fillStyle = 'black';
|
|
|
- Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
|
|
|
- Game.context.fillStyle = 'white';
|
|
|
- Game.context.textAlign = 'center';
|
|
|
- Game.context.fillText('Audventure', Game.canvas.width/2, Game.canvas.height/2 - 20);
|
|
|
- Game.context.fillText('Press any key to start', Game.canvas.width/2, Game.canvas.height/2 + 20);
|
|
|
- }
|
|
|
+function Scene() {}
|
|
|
+
|
|
|
+Scene.prototype._init = function() {
|
|
|
+ makeObservable(this);
|
|
|
};
|
|
|
|
|
|
+Scene.prototype._update = function(dt) {
|
|
|
+ this.notify(dt);
|
|
|
+ this.update(dt);
|
|
|
+};
|
|
|
+
|
|
|
+Scene.prototype._draw = function() {
|
|
|
+ this.draw();
|
|
|
+};
|
|
|
+
|
|
|
+Scene.prototype.tween = function(from) {
|
|
|
+ var tween = new DeferredTween(from);
|
|
|
+ tween.ticker = this.ticker;
|
|
|
+ return tween;
|
|
|
+};
|
|
|
+
|
|
|
+var MainMenu = new Scene();
|
|
|
(function() {
|
|
|
function onKeyUp() {
|
|
|
var beats = [
|
|
@@ -85,26 +96,45 @@ var MainMenu = {
|
|
|
|
|
|
Game.sceneManager.push(new SoundSafari(beats));
|
|
|
}
|
|
|
+ MainMenu.update = function() {};
|
|
|
+ MainMenu.draw = function() {
|
|
|
+ Game.context.fillStyle = 'black';
|
|
|
+ Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
|
|
|
+ Game.context.fillStyle = 'white';
|
|
|
+ Game.context.textAlign = 'center';
|
|
|
+ Game.context.fillText('Audventure', Game.canvas.width/2, Game.canvas.height/2 - 20);
|
|
|
+ Game.context.fillText('Press any key to start', Game.canvas.width/2, Game.canvas.height/2 + 20);
|
|
|
+ };
|
|
|
MainMenu.load = MainMenu.resume = function() {
|
|
|
- addEventListener('keyup', onKeyUp);
|
|
|
+ addEventListener('keypress', onKeyUp);
|
|
|
};
|
|
|
MainMenu.unload = MainMenu.pause = function() {
|
|
|
- removeEventListener('keyup', onKeyUp);
|
|
|
+ removeEventListener('keypress', onKeyUp);
|
|
|
};
|
|
|
})();
|
|
|
|
|
|
-var PauseScreen = {
|
|
|
- onKeyUp: function(e) {
|
|
|
- if(e.keyCode == 80) Game.sceneManager.pop();
|
|
|
- if(e.keyCode == 81) {
|
|
|
- Game.sceneManager.pop();
|
|
|
- Game.sceneManager.pop();
|
|
|
+var PauseScreen = new Scene();
|
|
|
+(function() {
|
|
|
+ var onKeyUp = function(e) {
|
|
|
+ switch(e.keyCode) {
|
|
|
+ case 81:
|
|
|
+ case 113:
|
|
|
+ case 27:
|
|
|
+ Game.sceneManager.pop();
|
|
|
+ case 80:
|
|
|
+ case 112:
|
|
|
+ case 32:
|
|
|
+ Game.sceneManager.pop();
|
|
|
}
|
|
|
- },
|
|
|
- load: function() {addEventListener('keyup', this.onKeyUp)},
|
|
|
- unload: function() {removeEventListener('keyup', this.onKeyUp)},
|
|
|
- update: function() {},
|
|
|
- draw: function() {
|
|
|
+ };
|
|
|
+ PauseScreen.load = PauseScreen.resume = function() {
|
|
|
+ addEventListener('keypress', onKeyUp);
|
|
|
+ };
|
|
|
+ PauseScreen.unload = PauseScreen.pause = function() {
|
|
|
+ removeEventListener('keypress', onKeyUp);
|
|
|
+ };
|
|
|
+ PauseScreen.update = function() {};
|
|
|
+ PauseScreen.draw = function() {
|
|
|
Game.context.fillStyle = 'black';
|
|
|
Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
|
|
|
Game.context.fillStyle = 'white';
|
|
@@ -112,8 +142,5 @@ var PauseScreen = {
|
|
|
Game.context.fillText('Paused', Game.canvas.width/2, Game.canvas.height/2 - 30);
|
|
|
Game.context.fillText('P to unpause', Game.canvas.width/2, Game.canvas.height/2);
|
|
|
Game.context.fillText('Q to quit', Game.canvas.width/2, Game.canvas.height/2 + 20);
|
|
|
-
|
|
|
- },
|
|
|
- pause: this.unload,
|
|
|
- resume: this.load
|
|
|
-};
|
|
|
+ };
|
|
|
+})();
|