spaceinvaders-models.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. WORLD.farm.bonus = [
  11. {
  12. type: "weapon",
  13. clazz: CarotWeapon,
  14. animation: WORLD.farm.weapons.carot
  15. },
  16. {
  17. type: "weapon",
  18. clazz: CornWeapon,
  19. animation: WORLD.farm.weapons.corn
  20. }
  21. ];
  22. /*** Move ***/
  23. var MOVE = {
  24. translation : {
  25. init : function (x, y) {
  26. return {directionX : 1, directionY : 0};
  27. },
  28. move : function() {
  29. var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
  30. if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
  31. this.directionX *= -1;
  32. this.y = (this.y + this.height / 4);
  33. }
  34. },
  35. },
  36. mirror : {
  37. init : function(x, y) {
  38. if( x < PLAYGROUND_WIDTH / 2 ) {
  39. return {directionX: -1, directionY: 0};
  40. }
  41. return {directionX: 1, directionY: 0};
  42. },
  43. move : function() {
  44. var offset = this.width / 2;
  45. if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
  46. this.directionX *= -1;
  47. this.y = (this.y + this.height / 4);
  48. }
  49. },
  50. },
  51. sinusoid : {
  52. init : function (x, y) {
  53. return {directionX : 1, directionY : 0};
  54. },
  55. move : function () {
  56. var offsetX = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
  57. if (Math.abs((this.getOriginX() - this.getX())) >= offsetX) {
  58. this.directionX *= -1;
  59. }
  60. var offsetY = 5 * ALIENS_HEIGHT;
  61. if (Math.abs((this.getOriginY() - this.getY())) >= offsetY) {
  62. this.directionY *= -1;
  63. }
  64. }
  65. }
  66. };
  67. /*** Move - end ***/
  68. /*** Waves ***/
  69. var WAVES = [
  70. {
  71. wave : [ [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
  72. [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
  73. [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
  74. [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
  75. [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], ],
  76. move : MOVE.sinusoid,
  77. bonus : [40, 20]
  78. },
  79. {
  80. wave : [ [ Alien, Alien, Alien, undefined, Alien, Alien, Alien ],
  81. [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien ],
  82. [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien ],
  83. [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ],
  84. [ Alien, Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ] ],
  85. move : MOVE.mirror,
  86. bonus : [30, 15]
  87. }
  88. ];
  89. /*** Waves - end ***/
  90. /*** Weapons ***/
  91. function Weapon() {
  92. "use strict";
  93. }
  94. Weapon.prototype = {
  95. speed : 5,
  96. strength : 10,
  97. stock: Infinity,
  98. rof : 300,
  99. ror : 1500,
  100. load : 1,
  101. max_load : 1,
  102. width : 5,
  103. height : 5,
  104. shot_timer : false,
  105. reload_timer : false,
  106. directionX : 0,
  107. directionY : 1,
  108. animation : null,
  109. clazz : "default",
  110. callback : undefined,
  111. fire : function() {
  112. if (this.shot_timer || this.load <= 0) {
  113. return false;
  114. }
  115. var _this = this;
  116. this.load = Math.max(0,this.load - 1);
  117. this.shot_timer = setInterval(function() {
  118. if (_this.load > 0) {
  119. clearTimeout(_this.shot_timer);
  120. _this.shot_timer = false;
  121. }
  122. }, this.rof);
  123. if( !this.reload_timer) {
  124. this.reload_timer = setInterval( function() {
  125. _this.load = Math.min(_this.load + 1, _this.max_load);
  126. if( _this.load == _this.max_load ) {
  127. clearInterval(_this.reload_timer);
  128. _this.reload_timer = false;
  129. }
  130. }, this.ror);
  131. }
  132. return true;
  133. }
  134. }
  135. function ShotgunWeapon() {
  136. "use strict";
  137. this.directionY = -1;
  138. this.rof = 200;
  139. this.ror = 1500;
  140. this.load = 2;
  141. this.max_load = 2;
  142. this.width = 3;
  143. this.height = 3;
  144. this.clazz = "Shotgun"
  145. }
  146. ShotgunWeapon.prototype = {
  147. }
  148. heriter(ShotgunWeapon.prototype, Weapon.prototype);
  149. function CarotWeapon() {
  150. "use strict";
  151. this.directionY = -1;
  152. this.stock = 10;
  153. this.clazz = "carot";
  154. this.load = 5;
  155. this.max_load = 5;
  156. this.width = 5;
  157. this.height = 10;
  158. }
  159. CarotWeapon.prototype = {
  160. }
  161. heriter(CarotWeapon.prototype, Weapon.prototype);
  162. function CornWeapon() {
  163. "use strict";
  164. this.directionY = -1;
  165. this.stock = 3;
  166. this.clazz = "corn";
  167. this.load = 1;
  168. this.max_load = 1;
  169. this.callback = function(shot) {
  170. var higherAlien = Math.max.apply( null,
  171. $(".alien").map(function() {
  172. return $(this).y();
  173. }).get() ),
  174. lowerAlien = Math.min.apply( null,
  175. $(".alien").map(function() {
  176. return $(this).y();
  177. }).get() ),
  178. mediumAlien = (higherAlien + lowerAlien) / 2;
  179. if( shot.y() < mediumAlien ) {
  180. shot.remove();
  181. var shipShots = $("#shipShots");
  182. for( var i = 0; i < 8; i++) {
  183. var cos = Math.cos( (Math.PI / 4) * i ),
  184. sin = Math.sin( (Math.PI / 4) * i);
  185. if( Math.abs(cos) < 0.01 ) {
  186. cos = 0;
  187. }
  188. if( Math.abs(sin) < 0.01) {
  189. sin = 0;
  190. }
  191. shipShots.addSprite( "shotCorn" + i, { posx : shot.x() + 5 * cos, posy : shot.y() + 5 * sin, width: 2, height: 2});
  192. var shotCorn = $("#shotCorn" + i);
  193. shotCorn.addClass("shipShot").addClass("shotCorn");
  194. $("#shotCorn" + i)[0].weapon = $.extend({
  195. directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0,
  196. directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0,
  197. speed : 1
  198. }, shot.weapon);
  199. }
  200. }
  201. }
  202. }
  203. CornWeapon.prototype = {
  204. }
  205. heriter(CornWeapon.prototype, Weapon.prototype);
  206. function AlienWeapon() {
  207. "use strict";
  208. this.directionY = 1;
  209. this.width = 5;
  210. this.height = 10;
  211. }
  212. AlienWeapon.prototype = {
  213. }
  214. heriter(AlienWeapon.prototype, Weapon.prototype);
  215. /*** Weapons -END ***/
  216. /*** Actors ***/
  217. function Actor() {
  218. "use strict";
  219. }
  220. Actor.prototype = {
  221. id : null,
  222. node : null,
  223. x : null,
  224. y : null,
  225. originX : 0,
  226. originY : 0,
  227. speed : null,
  228. health : 1,
  229. directionX : 0,
  230. directionY : 0,
  231. fireDirectionX : 0,
  232. fireDirectionY : 0,
  233. weapon : null,
  234. animations : {},
  235. getX : function() {
  236. "use strict";
  237. return this.x;
  238. },
  239. getY : function() {
  240. "use strict";
  241. return this.y;
  242. },
  243. move : function() {
  244. "use strict";
  245. if (!Game.running) {
  246. return;
  247. }
  248. this.x += this.directionX * this.speed;
  249. this.y += this.directionY * this.speed;
  250. this.x = Math.min(this.x, PLAYGROUND_WIDTH - this.node.w());
  251. this.x = Math.max(this.x, 0);
  252. this.node.x(this.x);
  253. this.node.y(this.y);
  254. },
  255. getOriginX : function() {
  256. return this.originX;
  257. },
  258. getOriginY : function() {
  259. return this.originY;
  260. },
  261. up : function(active) {
  262. "use strict";
  263. this.directionY = active ? -1 : 0;
  264. },
  265. down : function(active) {
  266. "use strict";
  267. this.directionY = active ? 1 : 0;
  268. },
  269. left : function(active) {
  270. "use strict";
  271. this.directionX = active ? -1 : 0;
  272. },
  273. right : function(active) {
  274. "use strict";
  275. this.directionX = active ? 1 : 0;
  276. },
  277. fire : function(layout, clazz) {
  278. var name = "shot" + Math.ceil(Math.random() * 1000);
  279. if (this.weapon.fire(layout)) {
  280. layout.addSprite(name, $.extend({ posx : this.x + this.node.w() / 2, posy : this.y + this.fireDirectionY * this.node.h()}, this.weapon));
  281. $("#" + name).addClass(clazz).addClass(this.weapon.clazz);
  282. $("#" + name)[0].weapon = this.weapon;
  283. return true;
  284. }
  285. return false;
  286. },
  287. hit : function() {
  288. this.health = this.health - 1;
  289. if( this.health == 0 ) {
  290. this.destroy();
  291. }
  292. return this.health;
  293. },
  294. destroy : function() {
  295. $("#" + this.id).remove();
  296. }
  297. };
  298. /*** Actors - Aliens ***/
  299. function Alien(id, start, move) {
  300. "use strict";
  301. this.id = id;
  302. this.x = start.x;
  303. this.y = start.y;
  304. this.moveFct = move;
  305. this.weapon = new AlienWeapon();
  306. this.fireDirectionY = 1;
  307. this.originX = this.x;
  308. this.originY = this.y;
  309. this.directionX = -1;
  310. this.speed = 0.5;
  311. }
  312. Alien.prototype = {
  313. speed : 0,
  314. directionX : 0,
  315. directionY : 0,
  316. moveFct : null,
  317. width : ALIENS_WIDTH,
  318. height : ALIENS_HEIGHT,
  319. aggression : 0.0005,
  320. init : function() {
  321. "use strict";
  322. this.speed = 0;
  323. this.node.x(this.x);
  324. this.node.y(this.y);
  325. },
  326. move : function() {
  327. "use strict";
  328. this._super("move", arguments);
  329. if (typeof this.moveFct !== undefined) {
  330. this.moveFct();
  331. }
  332. },
  333. destroy : function() {
  334. this._super("destroy", arguments);
  335. Game.addToScore( 5 );
  336. }
  337. };
  338. heriter(Alien.prototype, Actor.prototype);
  339. /*** Actors - Aliens - END ***/
  340. /*** Actors - Heroes - END ***/
  341. function Ship(id, start, speed, animation) {
  342. "use strict";
  343. this.id = id;
  344. this.x = start.x;
  345. this.y = start.y;
  346. this.weapon = new ShotgunWeapon();
  347. this.fireDirectionY = -1;
  348. this.originX = this.x;
  349. this.originY = this.y;
  350. this.speed = speed;
  351. this.animation = animation;
  352. /*this.bigWheel = $("#bigWheel");
  353. this.smallWheel = $("#smallWheel");
  354. var bigWheelRadius = bigWheel.h() / 2,
  355. smallWheelRadius = smallWheel.h() / 2;
  356. this.bigWheelAngle = this.speed * 360 / ( 2 * Math.PI * bigWheelRadius );
  357. this.smallWheelAngle = this.speed * 360 / ( 2 * Math.PI * smallWheelRadius );*/
  358. }
  359. Ship.prototype = {
  360. speed : 0,
  361. directionX : 0,
  362. directionY : 0,
  363. lives : 3,
  364. animation : null,
  365. /*bigWheel : null,
  366. bigWheelAngle : 0,
  367. smallWheel : null,
  368. smallWheelAngle : 0,*/
  369. init : function() {
  370. "use strict";
  371. this.speed = 0;
  372. this.node.x(this.x);
  373. this.node.y(this.y);
  374. },
  375. /**
  376. * Arc = (2* Pi * R) / (360) * alpha
  377. *
  378. */
  379. move : function() {
  380. "use strict";
  381. this._super("move", arguments);
  382. },
  383. /* right : function(active) {
  384. if( this.x + this.node.w() > PLAYGROUND_WIDTH ){
  385. return false;
  386. }
  387. this._super("right", arguments);
  388. this.bigWheel.rotate(this.bigWheelAngle, true);
  389. this.smallWheel.rotate(this.smallWheelAngle, true);
  390. },
  391. left : function(active) {
  392. if( this.x < 0 ){
  393. return false;
  394. }
  395. this._super("left", arguments);
  396. this.bigWheel.rotate(this.bigWheelAngle, true);
  397. this.smallWheel.rotate(this.smallWheelAngle, true);
  398. },
  399. */
  400. up : function(active) {
  401. if (this.y < (PLAYGROUND_HEIGHT - 2 * HUD_HEIGHT)) {
  402. return false;
  403. }
  404. this._super("up", arguments);
  405. },
  406. destroy : function() {
  407. $("#life" + this.lives).remove();
  408. this.lives = this.lives - 1;
  409. $("#hero").children().hide();
  410. if( this.lives == 0 ) {
  411. Game.game_over();
  412. return true;
  413. }
  414. var _this = this;
  415. setTimeout(function() {
  416. $("#hero").children().show();
  417. _this.health = 1;
  418. }, 2000);
  419. },
  420. fire : function() {
  421. if(this._super("fire", arguments)) {
  422. Game.shots.total = Game.shots.total + 1;
  423. this.weapon.stock--;
  424. if( this.weapon.stock == 0 ) {
  425. this.weapon = new ShotgunWeapon();
  426. $("#current_weapon").setAnimation(this.weapon.animation);
  427. }
  428. }
  429. }
  430. };
  431. heriter(Ship.prototype, Actor.prototype);
  432. /*** Actors - Heroes - END ***/
  433. /*** Actors - END ***/