game.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.done(function() {
  56. Game.start();
  57. });
  58. })();
  59. function Scene() {}
  60. Scene.prototype._init = function() {
  61. makeObservable(this);
  62. };
  63. Scene.prototype._update = function(dt) {
  64. this.notify(dt);
  65. this.update(dt);
  66. };
  67. Scene.prototype._draw = function() {
  68. this.draw();
  69. };
  70. Scene.prototype.tween = function(from) {
  71. var tween = new DeferredTween(from);
  72. tween.ticker = this.ticker;
  73. return tween;
  74. };
  75. var MainMenu = new Scene();
  76. (function() {
  77. function onKeyUp() {
  78. var beats = [
  79. {url: 'sound/b1.mp3', duration: 4000},
  80. {url: 'sound/b2.mp3', duration: 4000},
  81. {url: 'sound/b3.mp3', duration: 4000},
  82. {url: 'sound/b4.mp3', duration: 4000},
  83. {url: 'sound/b5.mp3', duration: 4000}
  84. ];
  85. Game.sceneManager.push(new SoundSafari(beats));
  86. }
  87. MainMenu.update = function() {};
  88. MainMenu.draw = function() {
  89. Game.context.fillStyle = 'black';
  90. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  91. Game.context.fillStyle = 'white';
  92. Game.context.textAlign = 'center';
  93. Game.context.fillText('Audventure', Game.canvas.width/2, Game.canvas.height/2 - 20);
  94. Game.context.fillText('Press any key to start', Game.canvas.width/2, Game.canvas.height/2 + 20);
  95. };
  96. MainMenu.load = MainMenu.resume = function() {
  97. addEventListener('keypress', onKeyUp);
  98. };
  99. MainMenu.unload = MainMenu.pause = function() {
  100. removeEventListener('keypress', onKeyUp);
  101. };
  102. })();
  103. var PauseScreen = new Scene();
  104. (function() {
  105. var onKeyUp = function(e) {
  106. switch(e.keyCode) {
  107. case 81:
  108. case 113:
  109. case 27:
  110. Game.sceneManager.pop();
  111. case 80:
  112. case 112:
  113. case 32:
  114. Game.sceneManager.pop();
  115. }
  116. };
  117. PauseScreen.load = PauseScreen.resume = function() {
  118. addEventListener('keypress', onKeyUp);
  119. };
  120. PauseScreen.unload = PauseScreen.pause = function() {
  121. removeEventListener('keypress', onKeyUp);
  122. };
  123. PauseScreen.update = function() {};
  124. PauseScreen.draw = function() {
  125. Game.context.fillStyle = 'black';
  126. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  127. Game.context.fillStyle = 'white';
  128. Game.context.textAlign = 'center';
  129. Game.context.fillText('Paused', Game.canvas.width/2, Game.canvas.height/2 - 30);
  130. Game.context.fillText('P to unpause', Game.canvas.width/2, Game.canvas.height/2);
  131. Game.context.fillText('Q to quit', Game.canvas.width/2, Game.canvas.height/2 + 20);
  132. };
  133. })();