123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- Ticker.start();
- var Game = {
- canvas: document.getElementById('game'),
- keysDown: {},
- start: function() {
- Game.sceneManager.push(MainMenu);
- Ticker.addListener(function(dt) {
- Game.sceneManager.update(dt);
- Game.sceneManager.draw();
- });
- },
- 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);
- if(!scene.hasTicker) scene._init();
- 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
- .then(function() {
- return [
- createSound('confirm', 'sound/confirm.mp3'),
- createSound('back', 'sound/back.mp3'),
- createSound('pause', 'sound/pause.mp3')
- ]
- })
- .done(function() {
- Game.start();
- });
- })();
- 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 = [
- {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));
- soundManager.play('confirm');
- }
- 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('keypress', onKeyUp);
- };
- MainMenu.unload = MainMenu.pause = function() {
- removeEventListener('keypress', onKeyUp);
- };
- })();
- var PauseScreen = new Scene();
- (function() {
- var onKeyUp = function(e) {
- switch(e.charCode) {
- case 81:
- case 113:
- case 27:
- Game.sceneManager.pop();
- case 80:
- case 112:
- case 32:
- Game.sceneManager.pop();
- soundManager.play('back');
- }
- };
- 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';
- 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);
- };
- })();
|