game.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var Game = {
  2. canvas: document.getElementById('game'),
  3. keysDown: {},
  4. start: function() {
  5. Game.sceneManager.push(MainMenu);
  6. this.then = Date.now();
  7. this.time = 0;
  8. setInterval(function() {
  9. Game.loop();
  10. }, 10);
  11. },
  12. loop: function() {
  13. var now = Date.now();
  14. var delta = now - this.then;
  15. this.time += delta;
  16. this.sceneManager.update(delta/1000);
  17. this.sceneManager.draw();
  18. this.then = now;
  19. },
  20. pause: function() {
  21. Game.sceneManager.push(PauseScreen);
  22. },
  23. sceneManager: {
  24. sceneStack: [],
  25. currentScene: null,
  26. update: function(delta) {
  27. if(this.currentScene) this.currentScene.update(delta);
  28. },
  29. draw: function() {
  30. if(this.currentScene) this.currentScene.draw();
  31. },
  32. push: function(scene) {
  33. if(this.currentScene) this.currentScene.pause();
  34. this.sceneStack.push(scene);
  35. scene.load();
  36. this.currentScene = scene;
  37. },
  38. pop: function() {
  39. var scene = this.sceneStack.pop();
  40. scene.unload();
  41. this.currentScene = this.sceneStack[this.sceneStack.length-1];
  42. this.currentScene.resume();
  43. }
  44. }
  45. };
  46. (function() {
  47. Game.context = Game.canvas.getContext('2d');
  48. addEventListener('keydown', function(e) {
  49. Game.keysDown[e.keyCode] = true;
  50. }, false);
  51. addEventListener('keyup', function(e) {
  52. delete Game.keysDown[e.keyCode];
  53. }, false);
  54. var soundDeferred = Q.defer();
  55. soundManager.setup({
  56. url: 'swf/',
  57. flashVersion: 9,
  58. onready: function() {
  59. soundDeferred.resolve(this);
  60. }
  61. });
  62. soundDeferred.promise.done(function() {
  63. Game.start();
  64. });
  65. })();
  66. var MainMenu = {
  67. update: function() {},
  68. draw: function() {
  69. Game.context.fillStyle = 'black';
  70. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  71. Game.context.fillStyle = 'white';
  72. Game.context.textAlign = 'center';
  73. Game.context.fillText('Audventure', Game.canvas.width/2, Game.canvas.height/2 - 20);
  74. Game.context.fillText('Press any key to start', Game.canvas.width/2, Game.canvas.height/2 + 20);
  75. }
  76. };
  77. (function() {
  78. function onKeyUp() {
  79. var beats = [
  80. {url: 'sound/b1.mp3', duration: 4000},
  81. {url: 'sound/b2.mp3', duration: 4000},
  82. {url: 'sound/b3.mp3', duration: 4000},
  83. {url: 'sound/b4.mp3', duration: 4000},
  84. {url: 'sound/b5.mp3', duration: 4000}
  85. ];
  86. Game.sceneManager.push(new SoundSafari(beats));
  87. };
  88. MainMenu.load = MainMenu.resume = function() {
  89. addEventListener('keyup', onKeyUp);
  90. }
  91. MainMenu.unload = MainMenu.pause = function() {
  92. removeEventListener('keyup', onKeyUp);
  93. }
  94. })();
  95. var PauseScreen = {
  96. onKeyUp: function(e) {
  97. if(e.keyCode == 80) Game.sceneManager.pop();
  98. if(e.keyCode == 81) {
  99. Game.sceneManager.pop();
  100. Game.sceneManager.pop();
  101. }
  102. },
  103. load: function() {addEventListener('keyup', this.onKeyUp)},
  104. unload: function() {removeEventListener('keyup', this.onKeyUp)},
  105. update: function() {},
  106. draw: function() {
  107. Game.context.fillStyle = 'black';
  108. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  109. Game.context.fillStyle = 'white';
  110. Game.context.textAlign = 'center';
  111. Game.context.fillText('Paused', Game.canvas.width/2, Game.canvas.height/2 - 30);
  112. Game.context.fillText('P to unpause', Game.canvas.width/2, Game.canvas.height/2);
  113. Game.context.fillText('Q to quit', Game.canvas.width/2, Game.canvas.height/2 + 20);
  114. },
  115. pause: this.unload,
  116. resume: this.load
  117. };