spaceinvaders-models.js 15 KB

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