var Game = { canvas: document.getElementById('game'), keysDown: {}, start: function() { Game.sceneManager.push(MainMenu); this.then = Date.now(); this.time = 0; setInterval(function() { Game.loop(); }, 10); }, loop: function() { var now = Date.now(); var delta = now - this.then; this.time += delta; this.sceneManager.update(delta/1000); this.sceneManager.draw(); this.then = now; }, pause: function() { Game.sceneManager.push(PauseScreen); }, sceneManager: { sceneStack: [], currentScene: null, update: function(delta) { if(this.currentScene) this.currentScene.update(delta); }, draw: function() { if(this.currentScene) this.currentScene.draw(); }, push: function(scene) { if(this.currentScene) this.currentScene.pause(); this.sceneStack.push(scene); scene.load(); this.currentScene = scene; }, pop: function() { var scene = this.sceneStack.pop(); scene.unload(); this.currentScene = this.sceneStack[this.sceneStack.length-1]; this.currentScene.resume(); } } }; (function() { Game.context = Game.canvas.getContext('2d'); addEventListener('keydown', function(e) { Game.keysDown[e.keyCode] = true; }, false); addEventListener('keyup', function(e) { delete Game.keysDown[e.keyCode]; }, false); var soundDeferred = Q.defer(); soundManager.setup({ url: 'swf/', flashVersion: 9, onready: function() { soundDeferred.resolve(this); } }); soundDeferred.promise.done(function() { Game.start(); }); })(); 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() { function onKeyUp() { var beats = [ {url: 'sound/b1.mp3', duration: 4000}, {url: 'sound/b2.mp3', duration: 4000}, {url: 'sound/b3.mp3', duration: 4000}, {url: 'sound/b4.mp3', duration: 4000}, {url: 'sound/b5.mp3', duration: 4000} ]; Game.sceneManager.push(new SoundSafari(beats)); }; MainMenu.load = MainMenu.resume = function() { addEventListener('keyup', onKeyUp); } MainMenu.unload = MainMenu.pause = function() { removeEventListener('keyup', onKeyUp); } })(); var PauseScreen = { onKeyUp: function(e) { if(e.keyCode == 80) Game.sceneManager.pop(); if(e.keyCode == 81) { Game.sceneManager.pop(); Game.sceneManager.pop(); } }, load: function() {addEventListener('keyup', this.onKeyUp)}, unload: function() {removeEventListener('keyup', this.onKeyUp)}, 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('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 };