1
0

spaceinvaders-core.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var WAVES = [
  2. {
  3. wave : [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  4. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  5. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  6. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  7. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ],
  8. move : function() {
  9. var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
  10. if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
  11. this.directionX *= -1;
  12. this.y = (this.y + this.height / 4);
  13. }
  14. }
  15. },
  16. {
  17. wave : [ [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0 ],
  18. [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  19. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  20. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ],
  21. move : function() {
  22. var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
  23. if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
  24. this.directionX *= -1;
  25. this.y = (this.y + this.height / 4);
  26. }
  27. }
  28. } ];
  29. Game = {
  30. running : false,
  31. wave : -1,
  32. aliens : [],
  33. ship : null,
  34. score : 0,
  35. init : function() {
  36. "use strict";
  37. Game.wave = Game.wave + 1;
  38. var row, col;
  39. var wave = WAVES[Game.wave].wave;
  40. for (row = 0; row < wave.length; row = row + 1) {
  41. var aliensRow = wave[row], type = aliensRow[0], offset = (PLAYGROUND_WIDTH - ((aliensRow.length - 1) * 0.5 + aliensRow.length)
  42. * ALIENS_WIDTH) / 2;
  43. for (col = 0; col < aliensRow.length; col = col + 1) {
  44. Game.setAlien(col, row, offset, type, WAVES[Game.wave].move);
  45. }
  46. }
  47. SCOREBOARD.init();
  48. SCOREBOARD.set_score( Game.score );
  49. Game.setShip();
  50. Game.running = true;
  51. },
  52. levelComplete : function() {
  53. "use strict";
  54. Game.running = false;
  55. setTimeout(function() {
  56. Game.init();
  57. }, 3000);
  58. },
  59. hit : function() {
  60. "use strict";
  61. if( !Game.running ) {
  62. return false;
  63. }
  64. console.log( "Game.hit" );
  65. var health = Game.ship.hit();
  66. $(".alienShot").remove();
  67. $(".shipShot").remove();
  68. $("#life" + Game.ship.lives).remove();
  69. Game.running = false;
  70. Game.ship.lives = Game.ship.lives - 1;
  71. $("#hero").children().hide();
  72. if( Game.ship.lives > 0 ) {
  73. var _this = Game.ship;
  74. setTimeout(function() {
  75. Game.running = true;
  76. $("#hero").children().show();
  77. _this.health = 1;
  78. }, 2000);
  79. }
  80. else {
  81. GUI.drawText( $("#message"), game_over, true );
  82. Game.show_game_over();
  83. }
  84. },
  85. setAlien : function(x, y, offset, type, move) {
  86. "use strict";
  87. var id = x * ROWS + y;
  88. var alien = new Alien("alien" + id, {
  89. x : offset + x * ALIENS_WIDTH * 1.5,
  90. y : START_Y + (y * 1.25 * ALIENS_HEIGHT)
  91. }, move);
  92. $("#actors").addSprite("alien" + id, $.extend({posx : alien.x, posy : alien.y}, animations.alien));
  93. alien.node = $("#alien" + id);
  94. alien.node.addClass("alien");
  95. $("#alien" + id)[0].alien = alien;
  96. Game.aliens.push(alien);
  97. },
  98. setShip : function() {
  99. var type = SHIPS.scout;
  100. Game.ship = new Ship("ship", {
  101. x : $("#hero").x(),
  102. y : $("#hero").y()
  103. }, 3, animations.hero.ship.animation);
  104. var hero = $("#hero");
  105. $.each(animations.hero,
  106. function(id, obj){
  107. hero.addSprite(id, obj);
  108. });
  109. Game.ship.node = $("#hero");
  110. },
  111. addToScore : function( toAdd ) {
  112. Game.score = Game.score + toAdd;
  113. SCOREBOARD.add( toAdd );
  114. },
  115. control : function() {
  116. if( !Game.running ) {
  117. return false;
  118. }
  119. $(document).keyup(function(e){
  120. switch(e.keyCode) {
  121. case 37:
  122. e.preventDefault();
  123. Game.ship.left(false);
  124. break;
  125. case 39:
  126. e.preventDefault();
  127. Game.ship.right(false);
  128. break;
  129. }
  130. });
  131. $(document).keydown(function(e){
  132. switch(e.keyCode) {
  133. case 37:
  134. e.preventDefault();
  135. Game.ship.left(true);
  136. break;
  137. case 39:
  138. e.preventDefault();
  139. Game.ship.right(true);
  140. break;
  141. case 32:
  142. e.preventDefault();
  143. Game.ship.fire($("#shipShots"), "shipShot");
  144. return false;
  145. }
  146. });
  147. },
  148. alienControl : function() {
  149. if( !Game.running ) {
  150. return false;
  151. }
  152. $.each(Game.aliens, function(index, alien ) {
  153. alien.move();
  154. if( alien.health > 0 && Math.random() < alien.aggression ) {
  155. alien.fire($("#aliensShots"), "alienShot");
  156. }
  157. });
  158. },
  159. heroShotCollision : function() {
  160. if( !Game.running ) {
  161. return false;
  162. }
  163. $(".shipShot").each(function(i,e) {
  164. var posy = $(this).y();
  165. if( posy < -$(this).height() ) {
  166. this.remove();
  167. return;
  168. }
  169. var weapon = $(this)[0].weapon;
  170. $(this).y(weapon.directionY * weapon.speed, true);
  171. $(this).x(weapon.directionX * weapon.speed, true);
  172. var collisions = $(this).collision(".alien,."+$.gQ.groupCssClass);
  173. collisions.each( function() {
  174. var alien = $(this)[0];
  175. var aliensNotInArray = $.grep( Game.aliens, function( elementOfArray, index) {
  176. return elementOfArray.id == alien.id;
  177. }, true);
  178. Game.aliens = aliensNotInArray;
  179. $(this)[0].alien.hit();
  180. })
  181. if( collisions.length > 0 ) {
  182. this.remove()
  183. }
  184. if( Game.aliens.length == 0 ) {
  185. Game.running = false;
  186. Game.levelComplete();
  187. }
  188. });
  189. },
  190. alienShotCollision : function() {
  191. if( !Game.running ) {
  192. return false;
  193. }
  194. $(".alienShot").each(function(i,e) {
  195. var posy = $(this).y();
  196. if( posy > PLAYGROUND_HEIGHT ) {
  197. this.remove();
  198. return;
  199. }
  200. var weapon = $(this)[0].weapon;
  201. $(this).y(weapon.directionY * weapon.speed, true);
  202. $(this).x(weapon.directionX * weapon.speed, true);
  203. var collisions = $(this).collision("#ship,."+$.gQ.groupCssClass);
  204. collisions.each( function() {
  205. Game.hit();
  206. })
  207. if( collisions.length > 0 ) {
  208. this.remove()
  209. }
  210. });
  211. }
  212. };