spaceinvaders-ui.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2013 Fabrice ECAILLE aka Febbweiss
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5. *
  6. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9. */
  10. /*global jQuery */
  11. var PLAYGROUND_WIDTH = 448,
  12. PLAYGROUND_HEIGHT = 544,
  13. MODAL_WIDTH = 300,
  14. REFRESH_RATE = 15,
  15. HUD_HEIGHT = 70,
  16. ROWS = 5,
  17. ALIENS = 11,
  18. ALIENS_WIDTH = 24,
  19. ALIENS_HEIGHT = 17,
  20. START_Y = 40;
  21. var animations = WORLD.farm;
  22. function explose( div, explosion, step, callback ) {
  23. if( step < explosion.length - 2 ) {
  24. div.setAnimation( explosion[step].animation, function() {
  25. explose( div, explosion, step + 1, callback );
  26. });
  27. } else {
  28. div.setAnimation( explosion[step].animation, callback);
  29. }
  30. };
  31. function explosionBig( div, callback ) {
  32. explose(div, EXPLOSIONS.BIG, 0, callback );
  33. };
  34. function explosionSmall( div, callback ) {
  35. explose(div, EXPLOSIONS.SMALL, 0, callback );
  36. };
  37. function displayModal(data) {
  38. var score = data.score.toString();
  39. $.playground()
  40. .addGroup( "modal_pane", {width: MODAL_WIDTH, height: data.end ? 190 : 140, posx: (PLAYGROUND_WIDTH - MODAL_WIDTH ) /2, posy: (PLAYGROUND_HEIGHT - (140 ))/2})
  41. .addSprite("scoreLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 50 : 10})
  42. .addSprite("scoreNbr", {width: score.length * 32, height: 32, posx: (MODAL_WIDTH - 10 - score.length * 32), posy: data.end ? 50 : 10})
  43. .end();
  44. if( data.end ) {
  45. var width = (data.end.length - 1 ) * 16 + 32;
  46. $("#modal_pane").addSprite("gameSt", {width: width, height: 32, posx: (MODAL_WIDTH - width) / 2, posy: 10});
  47. $("#gameSt").append(data.end).lettering();
  48. }
  49. if( data.bonus ) {
  50. var bonus = data.bonus.toString(),
  51. total = data.wave_score.toString();
  52. $("#modal_pane")
  53. .addSprite("totalLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 130 : 90})
  54. .addSprite("totalNbr", {width: total.length * 32, height: 32, posx: (MODAL_WIDTH - 10 - total.length * 32), posy: data.end ? 130 : 90})
  55. .addSprite("bonusLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 90 : 50})
  56. .addSprite("bonusNbr", {width: bonus.length * 32, height: 32, posx: (MODAL_WIDTH - 10 - bonus.length * 32), posy: data.end ? 90 : 50})
  57. ;
  58. $("#totalLbl").append("Wave").lettering();
  59. $("#totalNbr").append(total).lettering();
  60. $("#bonusLbl").append("Bonus").lettering();
  61. $("#bonusNbr").append(bonus).lettering();
  62. }
  63. $("#modal_pane").addClass( "modal" );
  64. $("#scoreLbl").append("Score").lettering();
  65. $("#scoreNbr").append(score).lettering();
  66. }
  67. function hideModal() {
  68. $("#modal_pane").remove();
  69. }
  70. function updateWeaponLoad( load ) {
  71. "use strict";
  72. var HTMLDiv = $("#weapon_load");
  73. HTMLDiv.removeClass().addClass("weapon_level");
  74. if( load > 2/3) {
  75. HTMLDiv.addClass("good");
  76. } else {
  77. if( load > 1/3) {
  78. HTMLDiv.addClass("middle");
  79. } else {
  80. HTMLDiv.addClass("bad");
  81. }
  82. }
  83. HTMLDiv.width( (load * 100) + "%" );
  84. }
  85. function updateLevel(level) {
  86. var strLevel = level.toString();
  87. var div = $("#level");
  88. div.w(strLevel.length * 32);
  89. div.x( PLAYGROUND_WIDTH / 2 - 10 - strLevel.length * 32);
  90. div.empty().append(strLevel).lettering();
  91. }
  92. $(function(){
  93. "use strict";
  94. //Playground Sprites
  95. $("#playground").playground({height: PLAYGROUND_HEIGHT, width: PLAYGROUND_WIDTH, keyTracker: true});
  96. $.playground({refreshRate: 60})
  97. .addGroup("background", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  98. .addSprite( "background1", {animation: animations.backgrounds.farm.background1.animation, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  99. .addSprite( "background2", {animation: animations.backgrounds.farm.background2.animation, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  100. .addSprite("ground", {posx: 0, posy : PLAYGROUND_HEIGHT - HUD_HEIGHT - 20, width: PLAYGROUND_WIDTH, height: 20})
  101. .end()
  102. .addGroup("actors", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  103. .addGroup("hero", {
  104. width: 48,
  105. height: 60,
  106. posx: (PLAYGROUND_WIDTH - 48) / 2,
  107. posy: PLAYGROUND_HEIGHT - HUD_HEIGHT - 50
  108. })
  109. .end()
  110. /* .addSprite("alienA", $.extend({posx : 50, posy : 300}, ALIENS[0]))
  111. .addSprite("alienB", $.extend({posx : 50, posy : 400}, ALIENS[1]))
  112. .addSprite("alienC", $.extend({posx : 50, posy : 500}, ALIENS[2]))
  113. */ .addSprite("explosion", {
  114. width: 64,
  115. height: 64,
  116. posx: 100,
  117. posy: 100
  118. })
  119. .end()
  120. .addGroup( "shipShots", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  121. .end()
  122. .addGroup( "aliensShots", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  123. .end()
  124. .addGroup( "hud", {width: PLAYGROUND_WIDTH, height: HUD_HEIGHT, posx: 0, posy: 0})
  125. .addSprite( "current_weapon", $.extend({posx: 10, posy: 40}, animations.weapons.gun))
  126. .addGroup("weapon_bar", {
  127. posx : 50,
  128. posy : 40,
  129. width : 100,
  130. height : 10
  131. })
  132. .addSprite("weapon_load", {
  133. width : 100,
  134. height : 10
  135. })
  136. .end()
  137. .addGroup("scoreboard", {
  138. posx: 10,
  139. posy: 2,
  140. width: 6 * 32,
  141. height: 32
  142. })
  143. .addSprite( "subScoreboard", {
  144. width: 6 * 32,
  145. height: 32
  146. })
  147. .end()
  148. .addGroup("lives", {
  149. posx: PLAYGROUND_WIDTH - 100,
  150. posy: 32,
  151. width: 100,
  152. height: 32
  153. })
  154. .addSprite("life2", animations.life)
  155. .addSprite("life1", $.extend({posx: 32}, animations.life))
  156. .addSprite("life0", $.extend({posx: 64}, animations.life))
  157. .end()
  158. .addGroup("levelGrp", {
  159. posx: PLAYGROUND_WIDTH / 2,
  160. posy: 2,
  161. width: PLAYGROUND_WIDTH / 2,
  162. height: 16
  163. })
  164. .addSprite("levelLbl", {
  165. width: 32 + 5 * 16,
  166. posx: 10
  167. })
  168. .addSprite("level", {
  169. width : 64,
  170. posx: 32 + 7 * 16,
  171. height: 32
  172. })
  173. .end()
  174. .end()
  175. ;
  176. $("#alienA").addClass("alien");
  177. $("#alienB").addClass("alien");
  178. $("#alienC").addClass("alien");
  179. $("#ground").css("background-color", "brown");
  180. $("#levelLbl").append("Level").lettering();
  181. $("#level").append("0").lettering();
  182. $("#scoreboard").addClass("scoreboard");
  183. $("#subScoreboard").addClass("subScoreboard");
  184. var hud = $("#hud");
  185. // Controls
  186. $.playground().registerCallback(Game.control, REFRESH_RATE);
  187. $.playground().registerCallback(Game.alienControl, REFRESH_RATE);
  188. // Collisions management
  189. $.playground().registerCallback(Game.bonusCollision, REFRESH_RATE);
  190. $.playground().registerCallback(Game.heroShotCollision, REFRESH_RATE);
  191. $.playground().registerCallback(Game.alienShotCollision, REFRESH_RATE);
  192. // Refresh playground
  193. $.playground().registerCallback(function(){
  194. updateWeaponLoad(Game.ship.weapon.load / Game.ship.weapon.max_load);
  195. if( Game.running ) {
  196. Game.ship.move();
  197. $(".bonus").each( function(index, bonus) {
  198. var posy = $(this).y();
  199. if( posy < Game.ship.y + 10 ) {
  200. $(this).y(3, true);
  201. return;
  202. }
  203. });
  204. }
  205. }, REFRESH_RATE);
  206. });
  207. $(document).ready(function () {
  208. Game.init();
  209. $.loadCallback(function(percent){
  210. $("#loadingBar").width(400*percent);
  211. });
  212. $.playground().startGame(
  213. function() {
  214. $("#welcomeScreen").remove();
  215. }
  216. );
  217. });