spaceinvaders-models.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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, _this.max_load);
  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. shot.remove();
  274. var shipShots = $("#shipShots");
  275. for( var i = 0; i < 8; i++) {
  276. var cos = Math.cos( (Math.PI / 4) * i ),
  277. sin = Math.sin( (Math.PI / 4) * i);
  278. if( Math.abs(cos) < 0.01 ) {
  279. cos = 0;
  280. }
  281. if( Math.abs(sin) < 0.01) {
  282. sin = 0;
  283. }
  284. shipShots.addSprite( "shotCorn" + i, { posx : shot.x() + 5 * cos, posy : shot.y() + 5 * sin, width: 2, height: 2});
  285. var shotCorn = $("#shotCorn" + i);
  286. shotCorn.addClass("shipShot").addClass("shotCorn");
  287. $("#shotCorn" + i)[0].weapon = $.extend({
  288. directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0,
  289. directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0,
  290. speed : 1
  291. }, shot.weapon);
  292. }
  293. }
  294. }
  295. }
  296. CornWeapon.prototype = {
  297. }
  298. heriter(CornWeapon.prototype, Weapon.prototype);
  299. function AlienWeapon() {
  300. "use strict";
  301. this.directionY = 1;
  302. this.width = 5;
  303. this.height = 10;
  304. }
  305. AlienWeapon.prototype = {
  306. }
  307. heriter(AlienWeapon.prototype, Weapon.prototype);
  308. /*** Weapons -END ***/
  309. /*** Actors ***/
  310. function Actor() {
  311. "use strict";
  312. }
  313. Actor.prototype = {
  314. id : null,
  315. node : null,
  316. x : null,
  317. y : null,
  318. originX : 0,
  319. originY : 0,
  320. speed : 0,
  321. health : 1,
  322. directionX : 0,
  323. directionY : 0,
  324. fireDirectionX : 0,
  325. fireDirectionY : 0,
  326. weapon : null,
  327. animations : {},
  328. getX : function() {
  329. "use strict";
  330. return this.x;
  331. },
  332. getY : function() {
  333. "use strict";
  334. return this.y;
  335. },
  336. move : function() {
  337. "use strict";
  338. if (!Game.running) {
  339. return;
  340. }
  341. this.x += this.directionX * this.speed;
  342. this.y += this.directionY * this.speed;
  343. this.x = Math.min(this.x, PLAYGROUND_WIDTH - this.node.w());
  344. this.x = Math.max(this.x, 0);
  345. this.node.x(this.x);
  346. this.node.y(this.y);
  347. },
  348. getOriginX : function() {
  349. return this.originX;
  350. },
  351. getOriginY : function() {
  352. return this.originY;
  353. },
  354. up : function(active) {
  355. "use strict";
  356. this.directionY = active ? -1 : 0;
  357. },
  358. down : function(active) {
  359. "use strict";
  360. this.directionY = active ? 1 : 0;
  361. },
  362. left : function(active) {
  363. "use strict";
  364. this.directionX = active ? -1 : 0;
  365. },
  366. right : function(active) {
  367. "use strict";
  368. this.directionX = active ? 1 : 0;
  369. },
  370. fire : function(layout, clazz) {
  371. var name = "shot" + Math.ceil(Math.random() * 1000);
  372. if (this.weapon.fire(layout)) {
  373. layout.addSprite(name, $.extend({ posx : this.x + this.node.w() / 2, posy : this.y + this.fireDirectionY * this.node.h()}, this.weapon));
  374. $("#" + name).addClass(clazz).addClass(this.weapon.clazz);
  375. $("#" + name)[0].weapon = this.weapon;
  376. return true;
  377. }
  378. return false;
  379. },
  380. hit : function() {
  381. this.health = this.health - 1;
  382. if( this.health == 0 ) {
  383. this.destroy();
  384. }
  385. return this.health;
  386. },
  387. destroy : function() {
  388. $("#" + this.id).remove();
  389. }
  390. };
  391. /*** Actors - Aliens ***/
  392. function Alien(id, start, move) {
  393. "use strict";
  394. this.id = id;
  395. this.x = start.x;
  396. this.y = start.y;
  397. this.moveFct = move;
  398. this.weapon = new AlienWeapon();
  399. this.fireDirectionY = 1;
  400. this.originX = this.x;
  401. this.originY = this.y;
  402. this.directionX = -1;
  403. this.speed = 0.5;
  404. }
  405. Alien.prototype = {
  406. moveFct : null,
  407. width : ALIENS_WIDTH,
  408. height : ALIENS_HEIGHT,
  409. aggression : 0.0005,
  410. init : function() {
  411. "use strict";
  412. this.speed = 0;
  413. this.node.x(this.x);
  414. this.node.y(this.y);
  415. },
  416. move : function() {
  417. "use strict";
  418. this._super("move", arguments);
  419. if (typeof this.moveFct !== undefined) {
  420. this.moveFct();
  421. }
  422. },
  423. destroy : function() {
  424. this._super("destroy", arguments);
  425. Game.addToScore( 5 );
  426. }
  427. };
  428. heriter(Alien.prototype, Actor.prototype);
  429. /*** Actors - Aliens - END ***/
  430. /*** Actors - Heroes - END ***/
  431. function Ship(id, start, speed, animation) {
  432. "use strict";
  433. this.id = id;
  434. this.x = start.x;
  435. this.y = start.y;
  436. this.weapon = new ShotgunWeapon();
  437. this.fireDirectionY = -1;
  438. this.originX = this.x;
  439. this.originY = this.y;
  440. this.speed = speed;
  441. this.animation = animation;
  442. /*this.bigWheel = $("#bigWheel");
  443. this.smallWheel = $("#smallWheel");
  444. var bigWheelRadius = bigWheel.h() / 2,
  445. smallWheelRadius = smallWheel.h() / 2;
  446. this.bigWheelAngle = this.speed * 360 / ( 2 * Math.PI * bigWheelRadius );
  447. this.smallWheelAngle = this.speed * 360 / ( 2 * Math.PI * smallWheelRadius );*/
  448. }
  449. Ship.prototype = {
  450. speed : 0,
  451. directionX : 0,
  452. directionY : 0,
  453. lives : 3,
  454. animation : null,
  455. /*bigWheel : null,
  456. bigWheelAngle : 0,
  457. smallWheel : null,
  458. smallWheelAngle : 0,*/
  459. init : function() {
  460. "use strict";
  461. this.speed = 0;
  462. this.node.x(this.x);
  463. this.node.y(this.y);
  464. },
  465. /**
  466. * Arc = (2* Pi * R) / (360) * alpha
  467. *
  468. */
  469. move : function() {
  470. "use strict";
  471. this._super("move", arguments);
  472. },
  473. /* right : function(active) {
  474. if( this.x + this.node.w() > PLAYGROUND_WIDTH ){
  475. return false;
  476. }
  477. this._super("right", arguments);
  478. this.bigWheel.rotate(this.bigWheelAngle, true);
  479. this.smallWheel.rotate(this.smallWheelAngle, true);
  480. },
  481. left : function(active) {
  482. if( this.x < 0 ){
  483. return false;
  484. }
  485. this._super("left", arguments);
  486. this.bigWheel.rotate(this.bigWheelAngle, true);
  487. this.smallWheel.rotate(this.smallWheelAngle, true);
  488. },
  489. */
  490. up : function(active) {
  491. if (this.y < (PLAYGROUND_HEIGHT - 2 * HUD_HEIGHT)) {
  492. return false;
  493. }
  494. this._super("up", arguments);
  495. },
  496. destroy : function() {
  497. $("#life" + this.lives).remove();
  498. this.lives = this.lives - 1;
  499. $("#hero").children().hide();
  500. if( this.lives == 0 ) {
  501. Game.game_over();
  502. return true;
  503. }
  504. var _this = this;
  505. setTimeout(function() {
  506. $("#hero").children().show();
  507. _this.health = 1;
  508. }, 2000);
  509. },
  510. fire : function() {
  511. if(this._super("fire", arguments)) {
  512. Game.shots.total = Game.shots.total + 1;
  513. this.weapon.stock--;
  514. if( this.weapon.stock == 0 ) {
  515. this.weapon = new ShotgunWeapon();
  516. $("#current_weapon").setAnimation(this.weapon.animation);
  517. }
  518. }
  519. }
  520. };
  521. heriter(Ship.prototype, Actor.prototype);
  522. /*** Actors - Heroes - END ***/
  523. /*** Actors - END ***/