spaceinvaders-ui.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*global jQuery, console */
  2. var PLAYGROUND_WIDTH = 448,
  3. PLAYGROUND_HEIGHT = 544,
  4. REFRESH_RATE = 15,
  5. HUD_HEIGHT = 70,
  6. ROWS = 5,
  7. ALIENS = 11,
  8. ALIENS_WIDTH = 24,
  9. ALIENS_HEIGHT = 17,
  10. START_Y = 40,
  11. IMAGES_PREFIX = "images/";
  12. var SHIPS = {
  13. scout: {width: 30, height: 25},
  14. bomber: {width: 30, height: 25},
  15. cruser: {width: 30, height: 25}
  16. };
  17. var animations = {
  18. hero : {
  19. ship : {
  20. animation : new $.gameQuery.Animation({
  21. imageURL : IMAGES_PREFIX + "farm.png"
  22. }),
  23. width : 48,
  24. height : 24,
  25. posx : 0,
  26. posy : 17
  27. },
  28. /*cockpit : {
  29. animation : new $.gameQuery.Animation({
  30. imageURL : IMAGES_PREFIX + "farm.png",
  31. offsety : 24,
  32. offsetx : 20
  33. }),
  34. width : 20,
  35. height : 33,
  36. posx : 28,
  37. posy : 3
  38. },*/
  39. smallWheel : {
  40. animation : new $.gameQuery.Animation({
  41. imageURL : IMAGES_PREFIX + "farm.png",
  42. offsetx : 0,
  43. offsety : 24
  44. }),
  45. posx : 4,
  46. posy : 30,
  47. width: 14,
  48. height: 14
  49. },
  50. bigWheel : {
  51. animation : new $.gameQuery.Animation({
  52. imageURL : IMAGES_PREFIX + "farm.png",
  53. offsetx : 0,
  54. offsety : 38
  55. }),
  56. width : 19,
  57. height : 19,
  58. posx : 25,
  59. posy : 27
  60. }
  61. },
  62. alien : {
  63. animation : new $.gQ.Animation({
  64. imageURL : IMAGES_PREFIX + "invader.png",
  65. numberOfFrame : 2,
  66. delta : ALIENS_WIDTH,
  67. rate : 400,
  68. type : $.gQ.ANIMATION_HORIZONTAL
  69. }),
  70. width : ALIENS_WIDTH,
  71. height : ALIENS_HEIGHT
  72. },
  73. ufo : {
  74. animation: new $.gQ.Animation({imageURL: IMAGES_PREFIX + "ufo.png"}),
  75. width: 48,
  76. height: 32,
  77. posy: 10
  78. },
  79. life : {
  80. animation : new $.gameQuery.Animation({
  81. imageURL : IMAGES_PREFIX + "farm.png",
  82. offsetx : 14,
  83. offsety : 24
  84. }),
  85. width: 32,
  86. height: 16
  87. },
  88. weapons : {
  89. carot : {
  90. animation : new $.gameQuery.Animation({
  91. imageURL : IMAGES_PREFIX + "farm.png",
  92. offsetx : 19,
  93. offsety : 37
  94. }),
  95. width: 19,
  96. height: 19
  97. },
  98. corn : {
  99. animation : new $.gameQuery.Animation({
  100. imageURL : IMAGES_PREFIX + "farm.png",
  101. offsetx : 38,
  102. offsety : 37
  103. }),
  104. width: 19,
  105. height: 19
  106. },
  107. gun : {
  108. animation : new $.gameQuery.Animation({
  109. imageURL : IMAGES_PREFIX + "farm.png",
  110. offsetx : 57,
  111. offsety : 37
  112. }),
  113. width: 19,
  114. height: 19
  115. }
  116. },
  117. backgrounds : {
  118. farm: {
  119. background1 : {
  120. animation : new $.gQ.Animation({
  121. imageURL : IMAGES_PREFIX + "background.png"
  122. })
  123. },
  124. background2 : {
  125. animation : new $.gQ.Animation({
  126. imageURL : IMAGES_PREFIX + "background2.png"
  127. })
  128. }
  129. }
  130. }
  131. };
  132. function updateWeaponLoad( load ) {
  133. "use strict";
  134. var HTMLDiv = $("#weapon_load");
  135. HTMLDiv.removeClass().addClass("weapon_level");
  136. if( load > 2/3) {
  137. HTMLDiv.addClass("good");
  138. } else {
  139. if( load > 1/3) {
  140. HTMLDiv.addClass("middle");
  141. } else {
  142. HTMLDiv.addClass("bad");
  143. }
  144. }
  145. HTMLDiv.width( (load * 100) + "%" );
  146. }
  147. $(function(){
  148. "use strict";
  149. //Playground Sprites
  150. $("#playground").playground({height: PLAYGROUND_HEIGHT, width: PLAYGROUND_WIDTH, keyTracker: true});
  151. $.playground({refreshRate: 60})
  152. .addGroup("background", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  153. .addSprite( "background1", {animation: animations.backgrounds.farm.background1.animation, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  154. .addSprite( "background2", {animation: animations.backgrounds.farm.background2.animation, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  155. .addSprite("ground", {posx: 0, posy : PLAYGROUND_HEIGHT - HUD_HEIGHT - 20, width: PLAYGROUND_WIDTH, height: 20})
  156. .end()
  157. .addGroup("actors", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  158. .addGroup("hero", {
  159. width: 48,
  160. height: 60,
  161. posx: (PLAYGROUND_WIDTH - 48) / 2,
  162. posy: PLAYGROUND_HEIGHT - HUD_HEIGHT - 50
  163. })
  164. .end()
  165. .end()
  166. .addGroup( "shipShots", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  167. .end()
  168. .addGroup( "aliensShots", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
  169. .end()
  170. .addGroup( "hud", {width: PLAYGROUND_WIDTH, height: HUD_HEIGHT, posx: 0, posy: 0})
  171. .addSprite( "current_weapon", $.extend({posx: 10, posy: 40}, animations.weapons.gun))
  172. .addGroup("weapon_bar", {
  173. posx : 50,
  174. posy : 40,
  175. width : 100,
  176. height : 10
  177. })
  178. .addSprite("weapon_load", {
  179. width : 100,
  180. height : 10
  181. })
  182. .end()
  183. .addGroup("scoreboard", {
  184. posx: 10,
  185. posy: 2,
  186. width: 6 * 32,
  187. height: 32
  188. })
  189. .addSprite( "subScoreboard", {
  190. width: 6 * 32,
  191. height: 32
  192. })
  193. .end()
  194. .addGroup("lives", {
  195. posx: PLAYGROUND_WIDTH - 100,
  196. posy: 32,
  197. width: 100,
  198. height: 32
  199. })
  200. .addSprite("life3", animations.life)
  201. .addSprite("life2", $.extend({posx: 32}, animations.life))
  202. .addSprite("life1", $.extend({posx: 64}, animations.life))
  203. .end()
  204. .addGroup("levelGrp", {
  205. posx: PLAYGROUND_WIDTH - 6 * 32 + 16,
  206. posy: 2,
  207. width: 32 + 5 * 16,
  208. height: 16
  209. })
  210. .addSprite("levelLbl", {
  211. width: 32 + 5 * 16
  212. })
  213. .addSprite("level", {
  214. width : 64,
  215. posx: 32 + 7 * 16,
  216. posy: 16,
  217. height: 16
  218. })
  219. .end()
  220. .addSprite( "carot", $.extend({posx: 210, posy: 40}, animations.weapons.carot))
  221. .addSprite( "corn", $.extend({posx: 230, posy: 40}, animations.weapons.corn))
  222. .end()
  223. ;
  224. $("#ground").css("background-color", "brown");
  225. $("#levelLbl").append("Level").lettering();
  226. $("#level").append("0").lettering();
  227. $("#scoreboard").addClass("scoreboard");
  228. $("#subScoreboard").addClass("subScoreboard");
  229. var hud = $("#hud");
  230. /*
  231. hud.append("<div id='scoreMessage'></div>");
  232. hud.append("<div id='message'></div>'");
  233. hud.append("<div id='level'></div>'");
  234. hud.append("<div id='levelNumber'></div>'");*/
  235. // Controls
  236. $.playground().registerCallback(Game.control, REFRESH_RATE);
  237. $.playground().registerCallback(Game.alienControl, REFRESH_RATE);
  238. // Collisions management
  239. $.playground().registerCallback(Game.heroShotCollision, REFRESH_RATE);
  240. $.playground().registerCallback(Game.alienShotCollision, REFRESH_RATE);
  241. // Refresh playground
  242. $.playground().registerCallback(function(){
  243. updateWeaponLoad(Game.ship.weapon.load / Game.ship.weapon.max_load);
  244. if( Game.running ) {
  245. Game.ship.move();
  246. }
  247. }, REFRESH_RATE);
  248. });
  249. $(document).ready(function () {
  250. Game.init();
  251. $.loadCallback(function(percent){
  252. $("#loadingBar").width(400*percent);
  253. });
  254. $.playground().startGame(
  255. function() {
  256. $("#welcomeScreen").remove();
  257. }
  258. );
  259. });