game.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Ticker.start();
  2. var Game = {
  3. canvas: document.getElementById('game'),
  4. keysDown: {},
  5. start: function() {
  6. Game.sceneManager.push(MainMenu);
  7. Ticker.addListener(function(dt) {
  8. Game.sceneManager.update(dt);
  9. Game.sceneManager.draw();
  10. });
  11. },
  12. pause: function() {
  13. Game.sceneManager.push(PauseScreen);
  14. },
  15. sceneManager: {
  16. sceneStack: [],
  17. currentScene: null,
  18. update: function(delta) {
  19. if(this.currentScene) this.currentScene._update(delta);
  20. },
  21. draw: function() {
  22. if(this.currentScene) this.currentScene._draw();
  23. },
  24. push: function(scene) {
  25. if(this.currentScene) this.currentScene.pause();
  26. this.sceneStack.push(scene);
  27. if(!scene.hasTicker) scene._init();
  28. scene.load();
  29. this.currentScene = scene;
  30. },
  31. pop: function() {
  32. var scene = this.sceneStack.pop();
  33. scene.unload();
  34. this.currentScene = this.sceneStack[this.sceneStack.length-1];
  35. this.currentScene.resume();
  36. }
  37. }
  38. };
  39. (function() {
  40. Game.context = Game.canvas.getContext('2d');
  41. addEventListener('keydown', function(e) {
  42. Game.keysDown[e.keyCode] = true;
  43. }, false);
  44. addEventListener('keyup', function(e) {
  45. delete Game.keysDown[e.keyCode];
  46. }, false);
  47. var soundDeferred = Q.defer();
  48. soundManager.setup({
  49. url: 'swf/',
  50. flashVersion: 9,
  51. onready: function() {
  52. soundDeferred.resolve(this);
  53. }
  54. });
  55. soundDeferred.promise
  56. .then(function() {
  57. return [
  58. createSound('confirm', 'sound/confirm.mp3'),
  59. createSound('back', 'sound/back.mp3'),
  60. createSound('pause', 'sound/pause.mp3')
  61. ]
  62. })
  63. .done(function() {
  64. Game.start();
  65. });
  66. })();
  67. function Scene() {}
  68. Scene.prototype._init = function() {
  69. makeObservable(this);
  70. };
  71. Scene.prototype._update = function(dt) {
  72. this.notify(dt);
  73. this.update(dt);
  74. };
  75. Scene.prototype._draw = function() {
  76. this.draw();
  77. };
  78. Scene.prototype.tween = function(from) {
  79. var tween = new DeferredTween(from);
  80. tween.ticker = this.ticker;
  81. return tween;
  82. };
  83. var MainMenu = new Scene();
  84. (function() {
  85. function onKeyUp() {
  86. var beats = [
  87. {url: 'sound/b1.mp3', duration: 4000},
  88. {url: 'sound/b2.mp3', duration: 4000},
  89. {url: 'sound/b3.mp3', duration: 4000},
  90. {url: 'sound/b4.mp3', duration: 4000},
  91. {url: 'sound/b5.mp3', duration: 4000}
  92. ];
  93. Game.sceneManager.push(new SoundSafari(beats));
  94. soundManager.play('confirm');
  95. }
  96. MainMenu.update = function() {};
  97. MainMenu.draw = function() {
  98. Game.context.fillStyle = 'black';
  99. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  100. Game.context.fillStyle = 'white';
  101. Game.context.textAlign = 'center';
  102. Game.context.fillText('Audventure', Game.canvas.width/2, Game.canvas.height/2 - 20);
  103. Game.context.fillText('Press any key to start', Game.canvas.width/2, Game.canvas.height/2 + 20);
  104. };
  105. MainMenu.load = MainMenu.resume = function() {
  106. addEventListener('keypress', onKeyUp);
  107. };
  108. MainMenu.unload = MainMenu.pause = function() {
  109. removeEventListener('keypress', onKeyUp);
  110. };
  111. })();
  112. var PauseScreen = new Scene();
  113. (function() {
  114. var onKeyUp = function(e) {
  115. switch(e.charCode) {
  116. case 81:
  117. case 113:
  118. case 27:
  119. Game.sceneManager.pop();
  120. case 80:
  121. case 112:
  122. case 32:
  123. Game.sceneManager.pop();
  124. soundManager.play('back');
  125. }
  126. };
  127. PauseScreen.load = PauseScreen.resume = function() {
  128. addEventListener('keypress', onKeyUp);
  129. };
  130. PauseScreen.unload = PauseScreen.pause = function() {
  131. removeEventListener('keypress', onKeyUp);
  132. };
  133. PauseScreen.update = function() {};
  134. PauseScreen.draw = function() {
  135. Game.context.fillStyle = 'black';
  136. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  137. Game.context.fillStyle = 'white';
  138. Game.context.textAlign = 'center';
  139. Game.context.fillText('Paused', Game.canvas.width/2, Game.canvas.height/2 - 30);
  140. Game.context.fillText('P to unpause', Game.canvas.width/2, Game.canvas.height/2);
  141. Game.context.fillText('Q to quit', Game.canvas.width/2, Game.canvas.height/2 + 20);
  142. };
  143. })();