game.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. var fontDeferred = Q.defer();
  2. WebFontConfig = {
  3. google: { families: [ 'Quicksand::latin' ] },
  4. active: function() {
  5. fontDeferred.resolve();
  6. }
  7. };
  8. (function() {
  9. var wf = document.createElement('script');
  10. wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
  11. '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
  12. wf.type = 'text/javascript';
  13. wf.async = 'true';
  14. var s = document.getElementsByTagName('script')[0];
  15. s.parentNode.insertBefore(wf, s);
  16. })();
  17. var soundDeferred = Q.defer();
  18. soundManager.setup({
  19. url: 'swf/',
  20. flashVersion: 9,
  21. onready: function() {
  22. soundDeferred.resolve(this);
  23. }
  24. });
  25. var Game = {
  26. canvas: document.getElementById('game'),
  27. context: document.getElementById('game').getContext('2d'),
  28. keysDown: {},
  29. start: function() {
  30. addEventListener('keydown', function(e) {
  31. Game.keysDown[e.keyCode] = true;
  32. }, false);
  33. addEventListener('keyup', function(e) {
  34. delete Game.keysDown[e.keyCode];
  35. }, false);
  36. var fontPromise = fontDeferred.promise.timeout(5000);
  37. var soundPromise = soundDeferred.promise.then(function() {
  38. return [
  39. createSound('confirm', 'sound/confirm.mp3'),
  40. createSound('back', 'sound/back.mp3'),
  41. createSound('pause', 'sound/pause.mp3'),
  42. createSound('cursor', 'sound/cursor.mp3')
  43. ]
  44. });
  45. Game.sceneManager.push(SplashScreen);
  46. Q.allResolved([fontPromise, soundPromise]).done(function() {
  47. Game.sceneManager.pop();
  48. Game.sceneManager.push(MainMenu);
  49. });
  50. Ticker.start();
  51. Ticker.addListener(function(dt) {
  52. Game.sceneManager.update(dt);
  53. Game.sceneManager.draw();
  54. });
  55. },
  56. pause: function() {
  57. Game.sceneManager.push(PauseScreen, 0);
  58. },
  59. sceneManager: {
  60. sceneStack: [],
  61. currentScene: null,
  62. promise: Q(),
  63. update: function(delta) {
  64. if(this.currentScene) this.currentScene._update(delta);
  65. },
  66. draw: function() {
  67. if(this.currentScene) this.currentScene._draw();
  68. this.drawFade();
  69. },
  70. push: function(scene, outDuration, inDuration) {
  71. outDuration = (outDuration == undefined) ? 0.5 : outDuration;
  72. inDuration = (inDuration == undefined) ? outDuration : inDuration;
  73. var self = this;
  74. var promise = this.promise;
  75. if(this.currentScene) {
  76. this.currentScene.pause();
  77. promise = this.fadeOut(outDuration);
  78. }
  79. promise.then(function() {
  80. self.sceneStack.push(scene);
  81. if(!scene.hasTicker) scene._init();
  82. scene.load();
  83. self.currentScene = scene;
  84. return self.fadeIn(inDuration);
  85. });
  86. },
  87. pop: function(count, outDuration, inDuration) {
  88. outDuration = (outDuration == undefined) ? 0.5 : outDuration;
  89. inDuration = (inDuration == undefined) ? outDuration : inDuration;
  90. count = count || 1;
  91. var scenes = [];
  92. for(var i = 0; i < count; ++i) {
  93. scenes.push(this.sceneStack.pop());
  94. }
  95. var promise = scenes.reduce(function(acc, scene) {
  96. return Q.when(acc, scene.unload());
  97. }, Q());
  98. var self = this;
  99. Q.all([this.fadeOut(outDuration), promise]).then(function() {
  100. if(self.sceneStack.length > 0) {
  101. self.currentScene = self.sceneStack[self.sceneStack.length-1];
  102. self.currentScene.resume();
  103. self.fadeIn(inDuration);
  104. }
  105. else {
  106. self.currentScene = null;
  107. }
  108. });
  109. },
  110. fadeIn: function(duration) {
  111. var self = this
  112. this.promise = this.promise.then(function() {
  113. self.tween = tweenFrom(1).to(0, duration).animate();
  114. return self.tween.promise;
  115. });
  116. return this.promise;
  117. },
  118. fadeOut: function(duration) {
  119. var self = this;
  120. this.promise = this.promise.then(function() {
  121. self.tween = tweenFrom(0).to(1, duration).animate();
  122. return self.tween.promise;
  123. });
  124. return this.promise;
  125. },
  126. drawFade: function() {
  127. if(this.tween && this.promise.isPending()) {
  128. Game.context.globalAlpha = this.tween.value;
  129. Game.context.fillStyle = 'black';
  130. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  131. Game.context.globalAlpha = 1;
  132. }
  133. }
  134. }
  135. };
  136. function Scene() {}
  137. Scene.prototype._init = function() {
  138. makeObservable(this);
  139. };
  140. Scene.prototype._update = function(dt) {
  141. this.notify(dt);
  142. this.update(dt);
  143. };
  144. Scene.prototype._draw = function() {
  145. this.draw();
  146. };
  147. Scene.prototype.tween = function(from) {
  148. var tween = new DeferredTween(from);
  149. tween.ticker = this.ticker;
  150. return tween;
  151. };
  152. Scene.prototype.update = function() {};
  153. Scene.prototype.draw = function() {};
  154. Scene.prototype.load = function() {};
  155. Scene.prototype.unload = function() {};
  156. Scene.prototype.resume = function() {};
  157. Scene.prototype.pause = function() {};
  158. var SplashScreen = new Scene();
  159. (function() {
  160. var ctx = Game.context;
  161. var x = Game.canvas.width / 2;
  162. var y = Game.canvas.height / 2;
  163. var beater = new Beater();
  164. beater.init(x, y, 'white');
  165. SplashScreen.update = function(dt) {
  166. beater.update(dt);
  167. };
  168. SplashScreen.draw = function() {
  169. ctx.fillStyle = 'black';
  170. ctx.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  171. beater.draw();
  172. };
  173. })();
  174. var MainMenu = new Scene();
  175. (function() {
  176. var ctx = Game.context;
  177. var canvasW = Game.canvas.width;
  178. var canvasH = Game.canvas.height;
  179. var selected = 0;
  180. var alternate = false;
  181. var notYetDone = {
  182. name: '???',
  183. alt: '???',
  184. start: function() {}
  185. };
  186. var games = [
  187. {
  188. name: 'sound safari',
  189. alt: 'sound catcher',
  190. start: function() {
  191. var beats = [
  192. {url: 'sound/b1.mp3', duration: 4000},
  193. {url: 'sound/b2.mp3', duration: 4000},
  194. {url: 'sound/b3.mp3', duration: 4000},
  195. {url: 'sound/b4.mp3', duration: 4000},
  196. {url: 'sound/b5.mp3', duration: 4000}
  197. ];
  198. Game.sceneManager.push(new SoundSafari(beats, true));
  199. }
  200. },
  201. notYetDone,
  202. notYetDone,
  203. notYetDone,
  204. notYetDone,
  205. notYetDone,
  206. notYetDone
  207. ];
  208. function onKeyUp(e) {
  209. switch(e.keyCode) {
  210. case 66:
  211. alternate = !alternate;
  212. document.title = alternate ? 'SoundVoyager' : 'audventure';
  213. break;
  214. case 38:
  215. e.preventDefault();
  216. soundManager.play('cursor');
  217. selected = (selected + games.length -1) % games.length;
  218. break;
  219. case 40:
  220. e.preventDefault();
  221. soundManager.play('cursor');
  222. selected = (selected + 1) % games.length;
  223. break;
  224. case 32:
  225. e.preventDefault();
  226. case 13:
  227. soundManager.play('confirm');
  228. games[selected].start();
  229. }
  230. }
  231. MainMenu.update = function() {};
  232. MainMenu.draw = function() {
  233. ctx.fillStyle = 'black';
  234. ctx.fillRect(0, 0, canvasW, canvasH);
  235. ctx.fillStyle = 'white';
  236. ctx.textAlign = 'center';
  237. var canvasWM = canvasW/2;
  238. var canvasHM = canvasH/2;
  239. ctx.font = font(48);
  240. ctx.fillText(alternate ? 'SoundVoyager' : 'audventure', canvasWM, canvasHM - 50);
  241. ctx.font = font(16);
  242. games.forEach(function(game, index) {
  243. var y = canvasHM + 10 + index * 25;
  244. var name = alternate ? game.alt : game.name;
  245. ctx.fillText(name, canvasWM, y);
  246. if(index == selected) {
  247. var offset = ctx.measureText(name).width / 2 + 10;
  248. drawTriangle(canvasWM - offset, y - 5, 10, 7);
  249. drawTriangle(canvasWM + offset, y - 5, 10, -7);
  250. }
  251. });
  252. };
  253. MainMenu.load = MainMenu.resume = function() {
  254. addEventListener('keydown', onKeyUp);
  255. };
  256. MainMenu.unload = MainMenu.pause = function() {
  257. removeEventListener('keydown', onKeyUp);
  258. };
  259. })();
  260. var PauseScreen = new Scene();
  261. (function() {
  262. var onKeyUp = function(e) {
  263. switch(e.charCode) {
  264. case 81:
  265. case 113:
  266. case 27:
  267. Game.sceneManager.pop(2);
  268. soundManager.play('back');
  269. break;
  270. case 80:
  271. case 112:
  272. case 32:
  273. Game.sceneManager.pop(1, 0);
  274. soundManager.play('back');
  275. }
  276. };
  277. PauseScreen.load = PauseScreen.resume = function() {
  278. addEventListener('keypress', onKeyUp);
  279. };
  280. PauseScreen.unload = PauseScreen.pause = function() {
  281. removeEventListener('keypress', onKeyUp);
  282. };
  283. PauseScreen.update = function() {};
  284. PauseScreen.draw = function() {
  285. Game.context.fillStyle = 'black';
  286. Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  287. Game.context.fillStyle = 'white';
  288. Game.context.textAlign = 'center';
  289. Game.context.fillText('Paused', Game.canvas.width/2, Game.canvas.height/2 - 30);
  290. Game.context.fillText('P to unpause', Game.canvas.width/2, Game.canvas.height/2);
  291. Game.context.fillText('Q to quit', Game.canvas.width/2, Game.canvas.height/2 + 20);
  292. Game.context.textAlign = 'left';
  293. };
  294. })();
  295. Game.start();