spaceinvaders-models.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*** Weapons ***/
  2. function Weapon() {
  3. "use strict";
  4. }
  5. Weapon.prototype = {
  6. speed : 5,
  7. strength : 10,
  8. rof : 300,
  9. ror : 1500,
  10. load : 3,
  11. max_load : 3,
  12. width : 5,
  13. height : 20,
  14. shot_timer : false,
  15. reload_timer : false,
  16. directionX : 0,
  17. directionY : 1,
  18. animation : null,
  19. fire : function() {
  20. if (this.shot_timer || this.load <= 0) {
  21. return false;
  22. }
  23. var _this = this;
  24. this.load = Math.max(0,this.load - 1);
  25. this.shot_timer = setInterval(function() {
  26. if (_this.load > 0) {
  27. clearTimeout(_this.shot_timer);
  28. _this.shot_timer = false;
  29. }
  30. }, this.rof);
  31. if( !this.reload_timer) {
  32. this.reload_timer = setInterval( function() {
  33. _this.load = Math.min(_this.load + 1, _this.max_load);
  34. if( _this.load == _this.max_load ) {
  35. clearInterval(_this.reload_timer);
  36. _this.reload_timer = false;
  37. }
  38. }, this.ror);
  39. }
  40. return true;
  41. }
  42. }
  43. function HeroWeapon() {
  44. "use strict";
  45. this.directionY = -1;
  46. }
  47. HeroWeapon.prototype = {
  48. }
  49. heriter(HeroWeapon.prototype, Weapon.prototype);
  50. function AlienWeapon() {
  51. "use strict";
  52. this.directionY = 1;
  53. this.width = 5;
  54. this.height = 10;
  55. }
  56. AlienWeapon.prototype = {
  57. }
  58. heriter(AlienWeapon.prototype, Weapon.prototype);
  59. /*** Weapons -END ***/
  60. /*** Actors ***/
  61. function Actor() {
  62. "use strict";
  63. }
  64. Actor.prototype = {
  65. id : null,
  66. node : null,
  67. x : null,
  68. y : null,
  69. originX : 0,
  70. originY : 0,
  71. speed : null,
  72. health : 1,
  73. directionX : 0,
  74. directionY : 0,
  75. fireDirectionX : 0,
  76. fireDirectionY : 0,
  77. weapon : null,
  78. animations : {},
  79. getX : function() {
  80. "use strict";
  81. return this.x;
  82. },
  83. getY : function() {
  84. "use strict";
  85. return this.y;
  86. },
  87. move : function() {
  88. "use strict";
  89. if (!Game.running) {
  90. return;
  91. }
  92. this.x += this.directionX * this.speed;
  93. this.y += this.directionY * this.speed;
  94. this.x = Math.min(this.x, PLAYGROUND_WIDTH - this.node.w());
  95. this.x = Math.max(this.x, 0);
  96. this.node.x(this.x);
  97. this.node.y(this.y);
  98. },
  99. getOriginX : function() {
  100. return this.originX;
  101. },
  102. getOriginY : function() {
  103. return this.originY;
  104. },
  105. up : function(active) {
  106. "use strict";
  107. this.directionY = active ? -1 : 0;
  108. },
  109. down : function(active) {
  110. "use strict";
  111. this.directionY = active ? 1 : 0;
  112. },
  113. left : function(active) {
  114. "use strict";
  115. this.directionX = active ? -1 : 0;
  116. },
  117. right : function(active) {
  118. "use strict";
  119. this.directionX = active ? 1 : 0;
  120. },
  121. fire : function(layout, clazz) {
  122. var name = "shot" + Math.ceil(Math.random() * 1000);
  123. if (this.weapon.fire(layout)) {
  124. layout.addSprite(name, $.extend({ posx : this.x + this.node.w() / 2, posy : this.y + this.fireDirectionY * this.node.h()}, this.weapon));
  125. $("#" + name).addClass(clazz)
  126. $("#" + name)[0].weapon = this.weapon;
  127. }
  128. },
  129. hit : function() {
  130. this.health = this.health - 1;
  131. if( this.health == 0 ) {
  132. this.destroy();
  133. }
  134. return this.health;
  135. },
  136. destroy : function() {
  137. $("#" + this.id).remove();
  138. }
  139. };
  140. /*** Actors - Aliens ***/
  141. function Alien(id, start, move) {
  142. "use strict";
  143. this.id = id;
  144. this.x = start.x;
  145. this.y = start.y;
  146. this.moveFct = move;
  147. this.weapon = new AlienWeapon();
  148. this.fireDirectionY = 1;
  149. this.originX = this.x;
  150. this.originY = this.y;
  151. this.directionX = -1;
  152. this.speed = 0.5;
  153. }
  154. Alien.prototype = {
  155. speed : 0,
  156. directionX : 0,
  157. directionY : 0,
  158. moveFct : null,
  159. width : ALIENS_WIDTH,
  160. height : ALIENS_HEIGHT,
  161. aggression : 0.0005,
  162. init : function() {
  163. "use strict";
  164. this.speed = 0;
  165. this.node.x(this.x);
  166. this.node.y(this.y);
  167. },
  168. move : function() {
  169. "use strict";
  170. this._super("move", arguments);
  171. if (typeof this.moveFct !== undefined) {
  172. this.moveFct();
  173. }
  174. },
  175. destroy : function() {
  176. this._super("destroy", arguments);
  177. Game.addToScore( 5 );
  178. }
  179. };
  180. heriter(Alien.prototype, Actor.prototype);
  181. /*** Actors - Aliens - END ***/
  182. /*** Actors - Heroes - END ***/
  183. function Ship(id, start, speed, animation) {
  184. "use strict";
  185. this.id = id;
  186. this.x = start.x;
  187. this.y = start.y;
  188. this.weapon = new HeroWeapon();
  189. this.fireDirectionY = -1;
  190. this.originX = this.x;
  191. this.originY = this.y;
  192. this.speed = speed;
  193. this.animation = animation;
  194. /*this.bigWheel = $("#bigWheel");
  195. this.smallWheel = $("#smallWheel");
  196. var bigWheelRadius = bigWheel.h() / 2,
  197. smallWheelRadius = smallWheel.h() / 2;
  198. this.bigWheelAngle = this.speed * 360 / ( 2 * Math.PI * bigWheelRadius );
  199. this.smallWheelAngle = this.speed * 360 / ( 2 * Math.PI * smallWheelRadius );*/
  200. }
  201. Ship.prototype = {
  202. speed : 0,
  203. directionX : 0,
  204. directionY : 0,
  205. lives : 3,
  206. animation : null,
  207. /*bigWheel : null,
  208. bigWheelAngle : 0,
  209. smallWheel : null,
  210. smallWheelAngle : 0,*/
  211. init : function() {
  212. "use strict";
  213. this.speed = 0;
  214. this.node.x(this.x);
  215. this.node.y(this.y);
  216. },
  217. /**
  218. * Arc = (2* Pi * R) / (360) * alpha
  219. *
  220. */
  221. move : function() {
  222. "use strict";
  223. this._super("move", arguments);
  224. },
  225. /* right : function(active) {
  226. if( this.x + this.node.w() > PLAYGROUND_WIDTH ){
  227. return false;
  228. }
  229. this._super("right", arguments);
  230. this.bigWheel.rotate(this.bigWheelAngle, true);
  231. this.smallWheel.rotate(this.smallWheelAngle, true);
  232. },
  233. left : function(active) {
  234. if( this.x < 0 ){
  235. return false;
  236. }
  237. this._super("left", arguments);
  238. this.bigWheel.rotate(this.bigWheelAngle, true);
  239. this.smallWheel.rotate(this.smallWheelAngle, true);
  240. },
  241. */
  242. up : function(active) {
  243. if (this.y < (PLAYGROUND_HEIGHT - 2 * HUD_HEIGHT)) {
  244. return false;
  245. }
  246. this._super("up", arguments);
  247. },
  248. destroy : function() {
  249. $("#life" + this.lives).remove();
  250. this.lives = this.lives - 1;
  251. $("#hero").children().hide();
  252. var _this = this;
  253. setTimeout(function() {
  254. $("#hero").children().show();
  255. _this.health = 1;
  256. }, 2000);
  257. }
  258. };
  259. heriter(Ship.prototype, Actor.prototype);
  260. /*** Actors - Heroes - END ***/
  261. /*** Actors - END ***/