game.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. scene.load();
  28. this.currentScene = scene;
  29. },
  30. pop: function() {
  31. var scene = this.sceneStack.pop();
  32. scene.unload();
  33. this.currentScene = this.sceneStack[this.sceneStack.length-1];
  34. this.currentScene.resume();
  35. }
  36. }
  37. };
  38. (function() {
  39. Game.context = Game.canvas.getContext('2d');
  40. addEventListener('keydown', function(e) {
  41. Game.keysDown[e.keyCode] = true;
  42. }, false);
  43. addEventListener('keyup', function(e) {
  44. delete Game.keysDown[e.keyCode];
  45. }, false);
  46. var soundDeferred = Q.defer();
  47. soundManager.setup({
  48. url: 'swf/',
  49. flashVersion: 9,
  50. onready: function() {
  51. soundDeferred.resolve(this);
  52. }
  53. });
  54. soundDeferred.promise.done(function() {
  55. Game.start();
  56. });
  57. })();
  58. var MainMenu = {
  59. update: function() {},
  60. draw: function() {
  61. Game.context.fillStyle = 'black';
  62. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  63. Game.context.fillStyle = 'white';
  64. Game.context.textAlign = 'center';
  65. Game.context.fillText('Audventure', Game.canvas.width/2, Game.canvas.height/2 - 20);
  66. Game.context.fillText('Press any key to start', Game.canvas.width/2, Game.canvas.height/2 + 20);
  67. }
  68. };
  69. (function() {
  70. function onKeyUp() {
  71. var beats = [
  72. {url: 'sound/b1.mp3', duration: 4000},
  73. {url: 'sound/b2.mp3', duration: 4000},
  74. {url: 'sound/b3.mp3', duration: 4000},
  75. {url: 'sound/b4.mp3', duration: 4000},
  76. {url: 'sound/b5.mp3', duration: 4000}
  77. ];
  78. Game.sceneManager.push(new SoundSafari(beats));
  79. }
  80. MainMenu.load = MainMenu.resume = function() {
  81. addEventListener('keyup', onKeyUp);
  82. };
  83. MainMenu.unload = MainMenu.pause = function() {
  84. removeEventListener('keyup', onKeyUp);
  85. };
  86. })();
  87. var PauseScreen = {
  88. onKeyUp: function(e) {
  89. if(e.keyCode == 80) Game.sceneManager.pop();
  90. if(e.keyCode == 81) {
  91. Game.sceneManager.pop();
  92. Game.sceneManager.pop();
  93. }
  94. },
  95. load: function() {addEventListener('keyup', this.onKeyUp)},
  96. unload: function() {removeEventListener('keyup', this.onKeyUp)},
  97. update: function() {},
  98. draw: function() {
  99. Game.context.fillStyle = 'black';
  100. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  101. Game.context.fillStyle = 'white';
  102. Game.context.textAlign = 'center';
  103. Game.context.fillText('Paused', Game.canvas.width/2, Game.canvas.height/2 - 30);
  104. Game.context.fillText('P to unpause', Game.canvas.width/2, Game.canvas.height/2);
  105. Game.context.fillText('Q to quit', Game.canvas.width/2, Game.canvas.height/2 + 20);
  106. },
  107. pause: this.unload,
  108. resume: this.load
  109. };