models.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. var ALIENS = {
  143. alien1 : {
  144. health : 1,
  145. weapon : AlienWeapon,
  146. score : 5,
  147. aggression : 0.0005,
  148. animation : ALIENS_TYPE[0]
  149. },
  150. alien2 : {
  151. health : 1,
  152. weapon : AlienWeapon,
  153. score : 10,
  154. aggression : 0.001,
  155. animation : ALIENS_TYPE[1]
  156. },
  157. alien3 : {
  158. health : 1,
  159. weapon : AlienWeapon,
  160. score : 20,
  161. aggression : 0.0015,
  162. animation : ALIENS_TYPE[2]
  163. }
  164. }
  165. /*** Waves ***/
  166. var WAVES = [
  167. {
  168. wave : [
  169. [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ],
  170. [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ],
  171. [ ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3 ],
  172. [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ],
  173. [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ]
  174. ],
  175. move : MOVE.translation,
  176. bonus : [40, 20]
  177. },
  178. {
  179. wave : [ [ Alien, Alien, Alien, undefined, Alien, Alien, Alien ],
  180. [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien ],
  181. [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien ],
  182. [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ],
  183. [ Alien, Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ]
  184. ],
  185. move : MOVE.mirror,
  186. bonus : [30, 15]
  187. },
  188. {
  189. wave : [ [ undefined, undefined, Alien, undefined, undefined, undefined, undefined, undefined, Alien, undefined, undefined ],
  190. [ undefined, Alien, Alien, Alien, undefined, undefined, undefined, Alien, Alien, Alien, undefined ],
  191. [ Alien, Alien, undefined, Alien, Alien, undefined, Alien, Alien, undefined, Alien, Alien ],
  192. [ undefined, Alien, Alien, Alien, Alien, undefined, undefined, Alien, Alien, Alien, undefined ],
  193. [ undefined, undefined, Alien, undefined, undefined, undefined, undefined, undefined, Alien, undefined, undefined ]
  194. ],
  195. move : MOVE.half_part_rotation,
  196. bonus : [20, 10]
  197. },
  198. {
  199. wave : [
  200. [ undefined, undefined, undefined, undefined, Alien, undefined, undefined, undefined, undefined ],
  201. [ undefined, undefined, undefined, Alien, Alien, Alien, undefined, undefined, undefined ],
  202. [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
  203. [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
  204. [ Alien, Alien, Alien, undefined, undefined, undefined, Alien, Alien, Alien ],
  205. [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
  206. [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
  207. [ undefined, undefined, undefined, Alien, Alien, Alien, undefined, undefined, undefined ],
  208. [ undefined, undefined, undefined, undefined, Alien, undefined, undefined, undefined, undefined ]
  209. ],
  210. move : MOVE.rotation,
  211. bonus : [25, 12]
  212. }
  213. ];
  214. /*** Waves - end ***/
  215. /*** Weapons ***/
  216. function Weapon() {
  217. "use strict";
  218. }
  219. Weapon.prototype = {
  220. speed : 5,
  221. strength : 10,
  222. stock: Infinity,
  223. rof : 300,
  224. ror : 1500,
  225. load : 1,
  226. max_load : 1,
  227. width : 5,
  228. height : 5,
  229. shot_timer : false,
  230. reload_timer : false,
  231. directionX : 0,
  232. directionY : 1,
  233. animation : null,
  234. clazz : "default",
  235. callback : undefined,
  236. fire : function() {
  237. if (this.shot_timer || this.load <= 0) {
  238. return false;
  239. }
  240. var _this = this;
  241. this.load = Math.max(0,this.load - 1);
  242. this.shot_timer = setInterval(function() {
  243. if (_this.load > 0) {
  244. clearTimeout(_this.shot_timer);
  245. _this.shot_timer = false;
  246. }
  247. }, this.rof);
  248. if( !this.reload_timer) {
  249. this.reload_timer = setInterval( function() {
  250. _this.load = Math.min(_this.load + 1, Math.min(_this.max_load, _this.stock));
  251. if( _this.load == _this.max_load ) {
  252. clearInterval(_this.reload_timer);
  253. _this.reload_timer = false;
  254. }
  255. }, this.ror);
  256. }
  257. return true;
  258. }
  259. }
  260. function ShotgunWeapon() {
  261. "use strict";
  262. this.directionY = -1;
  263. this.rof = 200;
  264. this.ror = 1500;
  265. this.load = 2;
  266. this.max_load = 2;
  267. this.width = 3;
  268. this.height = 3;
  269. this.clazz = "Shotgun"
  270. }
  271. ShotgunWeapon.prototype = {
  272. }
  273. heriter(ShotgunWeapon.prototype, Weapon.prototype);
  274. function CarotWeapon() {
  275. "use strict";
  276. this.directionY = -1;
  277. this.stock = 10;
  278. this.clazz = "carot";
  279. this.load = 5;
  280. this.max_load = 5;
  281. this.width = 5;
  282. this.height = 10;
  283. }
  284. CarotWeapon.prototype = {
  285. }
  286. heriter(CarotWeapon.prototype, Weapon.prototype);
  287. function CornWeapon() {
  288. "use strict";
  289. this.directionY = -1;
  290. this.stock = 3;
  291. this.clazz = "corn";
  292. this.load = 1;
  293. this.max_load = 1;
  294. this.callback = function(shot) {
  295. var mediumAlien = getAliensMidHeight();
  296. if( shot.y() < mediumAlien ) {
  297. var x = shot.x(),
  298. y = shot.y(),
  299. explosion = EXPLOSIONS.SMALL[0];
  300. $("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2});
  301. explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()});
  302. shot.remove();
  303. var shipShots = $("#shipShots");
  304. for( var i = 0; i < 8; i++) {
  305. var cos = Math.cos( (Math.PI / 4) * i ),
  306. sin = Math.sin( (Math.PI / 4) * i);
  307. if( Math.abs(cos) < 0.01 ) {
  308. cos = 0;
  309. }
  310. if( Math.abs(sin) < 0.01) {
  311. sin = 0;
  312. }
  313. shipShots.addSprite( "shotCorn" + i, { posx : x + 5 * cos, posy : y + 5 * sin, width: 2, height: 2});
  314. var shotCorn = $("#shotCorn" + i);
  315. shotCorn.addClass("shipShot").addClass("shotCorn");
  316. $("#shotCorn" + i)[0].weapon = $.extend({
  317. directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0,
  318. directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0,
  319. speed : 1
  320. }, shot.weapon);
  321. }
  322. }
  323. }
  324. }
  325. CornWeapon.prototype = {
  326. }
  327. heriter(CornWeapon.prototype, Weapon.prototype);
  328. function AlienWeapon() {
  329. "use strict";
  330. this.directionY = 1;
  331. this.width = 5;
  332. this.height = 10;
  333. }
  334. AlienWeapon.prototype = {
  335. }
  336. heriter(AlienWeapon.prototype, Weapon.prototype);
  337. /*** Weapons -END ***/
  338. /*** Actors ***/
  339. function Actor() {
  340. "use strict";
  341. }
  342. Actor.prototype = {
  343. id : null,
  344. node : null,
  345. x : null,
  346. y : null,
  347. originX : 0,
  348. originY : 0,
  349. speed : 0,
  350. health : 1,
  351. directionX : 0,
  352. directionY : 0,
  353. fireDirectionX : 0,
  354. fireDirectionY : 0,
  355. weapon : null,
  356. animations : {},
  357. getX : function() {
  358. "use strict";
  359. return this.x;
  360. },
  361. getY : function() {
  362. "use strict";
  363. return this.y;
  364. },
  365. move : function() {
  366. "use strict";
  367. if (!Game.running) {
  368. return;
  369. }
  370. this.x += this.directionX * this.speed;
  371. this.y += this.directionY * this.speed;
  372. this.x = Math.min(this.x, PLAYGROUND_WIDTH - this.node.w());
  373. this.x = Math.max(this.x, 0);
  374. this.node.x(this.x);
  375. this.node.y(this.y);
  376. },
  377. getOriginX : function() {
  378. return this.originX;
  379. },
  380. getOriginY : function() {
  381. return this.originY;
  382. },
  383. up : function(active) {
  384. "use strict";
  385. this.directionY = active ? -1 : 0;
  386. },
  387. down : function(active) {
  388. "use strict";
  389. this.directionY = active ? 1 : 0;
  390. },
  391. left : function(active) {
  392. "use strict";
  393. this.directionX = active ? -1 : 0;
  394. },
  395. right : function(active) {
  396. "use strict";
  397. this.directionX = active ? 1 : 0;
  398. },
  399. fire : function(layout, clazz) {
  400. var name = "shot" + Math.ceil(Math.random() * 1000);
  401. if (this.weapon.fire(layout)) {
  402. layout.addSprite(name, $.extend({ posx : this.x + this.node.w() / 2, posy : this.y + this.fireDirectionY * this.node.h()}, this.weapon));
  403. $("#" + name).addClass(clazz).addClass(this.weapon.clazz);
  404. $("#" + name)[0].weapon = this.weapon;
  405. return true;
  406. }
  407. return false;
  408. },
  409. hit : function() {
  410. this.health = this.health - 1;
  411. if( this.health == 0 ) {
  412. this.destroy();
  413. }
  414. return this.health;
  415. },
  416. destroy : function() {
  417. $("#" + this.id).remove();
  418. }
  419. };
  420. /*** Actors - Aliens ***/
  421. function Alien(id, start, move, type) {
  422. "use strict";
  423. this.id = id;
  424. this.x = start.x;
  425. this.y = start.y;
  426. this.moveFct = move;
  427. this.weapon = new type.weapon();
  428. this.fireDirectionY = 1;
  429. this.originX = this.x;
  430. this.originY = this.y;
  431. this.directionX = -1;
  432. this.speed = 0.5;
  433. this.animation = type.animation;
  434. this.width = type.animation.width;
  435. this.height = type.animation.height;
  436. this.health = type.health;
  437. this.aggression = type.aggression;
  438. this.score = type.score;
  439. }
  440. Alien.prototype = {
  441. moveFct : null,
  442. width : 0,
  443. height : 0,
  444. aggression : 0,
  445. animation : null,
  446. score : 0,
  447. init : function() {
  448. "use strict";
  449. this.speed = 0;
  450. this.node.x(this.x);
  451. this.node.y(this.y);
  452. },
  453. move : function() {
  454. "use strict";
  455. this._super("move", arguments);
  456. if (typeof this.moveFct !== undefined) {
  457. this.moveFct();
  458. }
  459. },
  460. destroy : function() {
  461. this._super("destroy", arguments);
  462. Game.addToScore( this.score );
  463. }
  464. };
  465. heriter(Alien.prototype, Actor.prototype);
  466. /*** Actors - Aliens - END ***/
  467. /*** Actors - Heroes - END ***/
  468. function Ship(id, start, speed, animation) {
  469. "use strict";
  470. this.id = id;
  471. this.x = start.x;
  472. this.y = start.y;
  473. this.weapon = new ShotgunWeapon();
  474. this.fireDirectionY = -1;
  475. this.originX = this.x;
  476. this.originY = this.y;
  477. this.speed = speed;
  478. this.animation = animation;
  479. /*this.bigWheel = $("#bigWheel");
  480. this.smallWheel = $("#smallWheel");
  481. var bigWheelRadius = bigWheel.h() / 2,
  482. smallWheelRadius = smallWheel.h() / 2;
  483. this.bigWheelAngle = this.speed * 360 / ( 2 * Math.PI * bigWheelRadius );
  484. this.smallWheelAngle = this.speed * 360 / ( 2 * Math.PI * smallWheelRadius );*/
  485. }
  486. Ship.prototype = {
  487. speed : 0,
  488. directionX : 0,
  489. directionY : 0,
  490. lives : 3,
  491. animation : null,
  492. /*bigWheel : null,
  493. bigWheelAngle : 0,
  494. smallWheel : null,
  495. smallWheelAngle : 0,*/
  496. init : function() {
  497. "use strict";
  498. this.speed = 0;
  499. this.node.x(this.x);
  500. this.node.y(this.y);
  501. },
  502. /**
  503. * Arc = (2* Pi * R) / (360) * alpha
  504. *
  505. */
  506. move : function() {
  507. "use strict";
  508. this._super("move", arguments);
  509. },
  510. up : function(active) {
  511. if (this.y < (PLAYGROUND_HEIGHT - 2 * HUD_HEIGHT)) {
  512. return false;
  513. }
  514. this._super("up", arguments);
  515. },
  516. destroy : function() {
  517. var _this = this,
  518. _hero = $("#hero"),
  519. explosion = EXPLOSIONS.BIG;
  520. $("#life" + this.lives).remove();
  521. this.lives = this.lives - 1;
  522. $("#actors").addSprite("heroExplosion",
  523. {
  524. width: explosion[0].width,
  525. height: explosion[0].height,
  526. posx: _hero.x() - explosion[0].width / 2 + _hero.w(),
  527. posy: _hero.y() - explosion[0].height / 2 - _hero.h() /4
  528. }
  529. );
  530. explosionBig($("#heroExplosion"), function() {$("#heroExplosion").remove()});
  531. _hero.children().hide();
  532. },
  533. respawn : function() {
  534. $("#heroExplosion").remove();
  535. $("#hero").children().show();
  536. },
  537. fire : function() {
  538. if(this._super("fire", arguments)) {
  539. Game.shots.total = Game.shots.total + 1;
  540. this.weapon.stock--;
  541. if( this.weapon.stock == 0 ) {
  542. this.weapon = new ShotgunWeapon();
  543. $("#current_weapon").setAnimation(this.weapon.animation);
  544. }
  545. }
  546. }
  547. };
  548. heriter(Ship.prototype, Actor.prototype);
  549. /*** Actors - Heroes - END ***/
  550. /*** Actors - END ***/