Guest User

Untitled

a guest
Apr 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.84 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import GAME_CONST from "../const/GAME_CONST";
  4. import PlayButton from "../objects/widgets/buttons/PlayButton";
  5. import gameInfo from "../objects/Store/GameInfo";
  6.  
  7. var Play = function () {
  8. }
  9.  
  10.  
  11. Play.prototype = {
  12.  
  13. preload() {
  14. this.boss_spawned = false;
  15. this.counter = 0;
  16. this.is_enemy_spawned = [];
  17. this.enemy = [];
  18. this.ramp = [];
  19. },
  20.  
  21. create() {
  22.  
  23. this.game.physics.startSystem(Phaser.Physics.P2JS);
  24.  
  25.  
  26. this.game.world.setBounds(0, 0, 7680, 1080);
  27.  
  28. this.p = this.game.add.sprite(50, 0, 'character');
  29. this.p.rpg = this._getRPGStats();
  30. this.p.health = 2;
  31. this.p.maxHeath = 2;
  32. this.enemy = [];
  33.  
  34. this.game.physics.p2.enable(this.p);
  35. this.yAxis = p2.vec2.fromValues(0, 1);
  36. this.p2vec2 = p2.vec2;
  37. this.p.body.clearShapes();
  38. this.p.body.loadPolygon("mapPhysics", "phaser-dude");
  39. this.game.physics.p2.gravity.y = 4900;
  40. this.p.body.fixedRotation = true;
  41. this.game.camera.follow(this.p);
  42. this.p.animations.add('walk_left', [8, 9, 10, 11, 12, 13, 15]);
  43. this.p.animations.add('walk_right', [16, 17, 18, 19, 20, 21, 22, 23]);
  44. this.p.animations.add('attack_left', [0, 1, 2, 3]);
  45. this.p.animations.add('attack_right', [4, 5, 6, 7]);
  46. this.p.animations.play('attack_right', 10, true);
  47. this.p.attack = {};
  48. this.p.attack.isAttacking = false;
  49. this.p.canAttack = true;
  50. this.p.attack.since = new Date().getTime();
  51.  
  52. this.cursors = this.game.input.keyboard.createCursorKeys();
  53. this.game.input.gamepad.start();
  54. this.ground = this.add.sprite(3840, 747, 'platform', 0);
  55. this.ground.anchor.setTo(0, 0);
  56. this.game.physics.p2.enable(this.ground);
  57. this.healthMeterBar = this.game.add.plugin(Phaser.Plugin.HealthMeter);
  58. this.healthMeterBar.bar(this.p, {
  59. y: 20, x: 50,
  60. width: 100, height: 20,
  61. foreground: '#323232',
  62. background: '#aaaaaa',
  63. alpha: 0.6
  64. });
  65. //
  66. this.ground.body.clearShapes();
  67. this.ground.body.loadPolygon("mapPhysics", "ground");
  68. this.ground.body.dynamic = false;
  69. this.ground.body.gravityScale = 0;
  70.  
  71.  
  72. this.characterMaterial = this.game.physics.p2.createMaterial('characterMaterial', this.p.body);
  73.  
  74. this.groundMaterial = this.game.physics.p2.createMaterial('groundMaterial', this.ground.body);
  75.  
  76.  
  77. this.contactMaterial = this.game.physics.p2.createContactMaterial(this.characterMaterial, this.groundMaterial);
  78.  
  79. this.contactMaterial.restitution = 0;
  80.  
  81. //init dumb enemies
  82. this.game.dumb_enemies = this.game.add.physicsGroup(
  83. Phaser.Physics.P2JS,
  84. this.game.map,
  85. 'aliens'
  86. );
  87. this.game.dumb_enemies.lastSpawned = new Date().getTime();
  88.  
  89. this._add_static_ramps();
  90. this._add_enemy_home();
  91.  
  92. // setTimeout(function(){
  93. // this.game.state.start(GAME_CONST.STATES.SHOP);
  94. // }.bind(this), 10000);
  95. },
  96.  
  97. _add_enemy_home() {
  98. this.enemy_home = this.add.sprite(1300, 773, 'house', 0);
  99. this.enemy_home.anchor.setTo(0, 0);
  100. this.game.physics.p2.enable(this.enemy_home);
  101. //
  102. this.enemy_home.body.clearShapes();
  103. this.enemy_home.body.loadPolygon("mapPhysics", 'house');
  104. this.enemy_home.body.dynamic = false;
  105. this.enemy_home.body.gravityScale = 0;
  106. },
  107.  
  108. _check_n_spawn_enemy() {
  109. var length = this.enemy.length;
  110. var currentSpawn = new Date().getTime();
  111. if (currentSpawn > this.game.dumb_enemies.lastSpawned + 5000) {
  112. this.enemy[length] = this.game.dumb_enemies.create(1200, 773, 'alien');
  113. this.game.physics.p2.enable(this.enemy[length]);
  114. this.enemy[length].body.velocity.x = -200;
  115. this.game.dumb_enemies.lastSpawned = currentSpawn;
  116. }
  117. },
  118.  
  119. _move_enemies(){
  120. let i = 0
  121. for (; i < this.enemy.length; i++) {
  122. if (this.enemy[i].body != null) {
  123. this.enemy[i].body.velocity.x = -200;
  124. }
  125. if (this.enemy[i].body != null && this.enemy[i].body.x < 95) {
  126. this.enemy[i].destroy();
  127. }
  128. }
  129. },
  130.  
  131. _check_who_dies() {
  132. for (let i = 0; i < this.enemy.length; i++) {
  133. if (this.enemy[i].body != null) {
  134. if (this._check_if_collides(this.enemy[i])) {
  135. if (this.p.attack.isAttacking) {
  136. this.enemy[i].destroy();
  137. return false;
  138. } else {
  139. this.enemy[i].destroy();
  140. this.p.health--;
  141. return true;
  142. }
  143. }
  144. }
  145. }
  146. return false;
  147. },
  148.  
  149. _check_if_collides(enemy){
  150. // console.log("/////////////////");
  151. // console.log("x diff: " + (enemy.x - this.p.x));
  152. // console.log("x widths: " + (enemy.width / 2 + this.p.width / 2));
  153. // console.log("y diff: " + (enemy.y - this.p.y));
  154. // console.log("x height: " + (enemy.height / 2 + this.p.height / 2));
  155. // cons ole.log("/////////////////");
  156. if (enemy.x - this.p.x < enemy.width / 2 + this.p.width / 2) {
  157. if (enemy.y - this.p.y < enemy.height / 2 + this.p.height / 2) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. },
  163.  
  164. _add_static_ramps() {
  165. this._add_ramp_at_pos(1042, 547, 'floating-ramp-1');
  166. this._add_ramp_at_pos(2890, 397, 'floating-ramp-2');
  167. this._add_ramp_at_pos(3345, 297, 'floating-ramp-3');
  168. this.ramp[2].min_y = 347;
  169. this.ramp[2].max_y = 647;
  170. this.ramp[2].state = 'stationary_top';
  171. this.ramp[2].stateSince = new Date().getTime();
  172. this._add_ramp_at_pos(3866, 397, 'floating-ramp-4');
  173. },
  174.  
  175. _move_dynamic_ramp() {
  176. if (this.ramp[2] != undefined && this.ramp[2].stateSince != undefined) {
  177. var currentTime = new Date().getTime();
  178. if (this.ramp[2].state == 'stationary_top' &&
  179. currentTime > this.ramp[2].stateSince + 5000) {
  180. this.ramp[2].state = 'moving_bottom';
  181. this.ramp[2].stateSince = currentTime;
  182. this.ramp[2].body.velocity.y = 200;
  183. }
  184. if (this.ramp[2].state == 'moving_bottom' &&
  185. this.ramp[2].y > this.ramp[2].max_y) {
  186. this.ramp[2].state = 'stationary_bottom';
  187. this.ramp[2].stateSince = currentTime;
  188. this.ramp[2].body.velocity.y = 0;
  189. }
  190. if (this.ramp[2].state == 'stationary_bottom' &&
  191. currentTime > this.ramp[2].stateSince + 1500) {
  192. this.ramp[2].state = 'moving_up';
  193. this.ramp[2].stateSince = currentTime;
  194. this.ramp[2].body.velocity.y = -200;
  195. }
  196. if (this.ramp[2].state == 'moving_up' &&
  197. this.ramp[2].y < this.ramp[2].min_y) {
  198. this.ramp[2].state = 'stationary_top';
  199. this.ramp[2].stateSince = currentTime;
  200. this.ramp[2].body.velocity.y = 0;
  201. }
  202. }
  203. },
  204.  
  205. _add_ramp_at_pos(x, y, image) {
  206. var rampCount = this.ramp.length;
  207. this.ramp[rampCount] = this.add.sprite(x, y, image, 0);
  208. this.ramp[rampCount].anchor.setTo(0, 0);
  209. this.game.physics.p2.enable(this.ramp[rampCount]);
  210. //
  211. this.ramp[rampCount].body.clearShapes();
  212. this.ramp[rampCount].body.loadPolygon("mapPhysics", image);
  213. this.ramp[rampCount].body.dynamic = false;
  214. this.ramp[rampCount].body.gravityScale = 0;
  215. },
  216.  
  217. u_controller_clicked(button) {
  218. if (button.name == "leftButton") {
  219. this.p.body.velocity.x = -1 * GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
  220. }
  221. else if (this.name == "rightButton") {
  222. console.log("WE are right" + button.name);
  223. this.p.body.velocity.x = GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
  224. }
  225. },
  226.  
  227. update() {
  228. // console.log("x: " + this.p.x + " y: " + this.p.y);
  229. this._adjustCharacterPhysicsBound();
  230. this._check_n_spawn_enemy();
  231. this._move_enemies();
  232. this._update_attack_sequence();
  233. this._check_who_dies()
  234. if (this.p.health <= 0) {
  235. // console.log("game over");
  236. // this.game.state.start(GAME_CONST.STATES.SHOP);
  237. }
  238. if (this.p.y > 1080) {
  239. // console.log("game over");
  240. }
  241. // this.p.rotation = 0;
  242. this.p.body.velocity.x = 0;
  243. // this.p.body.velocity.y = 0;
  244. let isCurserDown = false;
  245. if (this.cursors.up.isDown) {
  246. // if (this.p.body.onFloor()) {
  247. // this.p.body.velocity.y = -1 * GAME_CONST.VELOCITY.y[this.p.rpg.y_index];
  248. // }
  249. if (this._checkIfCanJump()) {
  250. this.p.body.velocity.y = -9 * GAME_CONST.VELOCITY.y[this.p.rpg.y_index];
  251. }
  252. isCurserDown = true;
  253. }
  254. //should have been stationary before attacking
  255. else if (this.cursors.down.isDown && this.p.canAttack) {
  256. this.p.attack.isAttacking = true;
  257. this.p.attack.since = new Date().getTime();
  258. }
  259.  
  260. if (this.p.attack.isAttacking) {
  261. isCurserDown = true;
  262. if ((this.p.frame >= 8 && this.p.frame < 16) || (this.p.frame >= 0 && this.p.frame < 3) || this.p.frame === 24) {
  263. this.p.animations.play('attack_left', 10, true);
  264. }
  265. else if ((this.p.frame >= 16 && this.p.frame < 24) || (this.p.frame >= 4 && this.p.frame < 7) || this.p.frame === 25) {
  266. this.p.animations.play('attack_right', 10, true);
  267. }
  268. }
  269.  
  270. if (this.cursors.left.isDown) {
  271. this.p.body.velocity.x = -2 * GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
  272. this.p.animations.play('walk_left', 10, true);
  273. this.p.canAttack = false;
  274. isCurserDown = true;
  275. this.p.attack.isAttacking = false;
  276. }
  277. else if (this.cursors.right.isDown) {
  278. this.p.body.velocity.x = 3 * GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
  279. this.p.animations.play('walk_right', 10, true);
  280. this.p.canAttack = false;
  281. isCurserDown = true;
  282. this.p.attack.isAttacking = false;
  283. }
  284. if (!isCurserDown) {
  285. this.p.animations.currentAnim.stop();
  286. if ((this.p.frame >= 8 && this.p.frame < 16) || (this.p.frame >= 0 && this.p.frame < 4)) {
  287. this.p.frameName = "standing-left.png";
  288. this.p.canAttack = "true";
  289. }
  290. else if ((this.p.frame >= 16 && this.p.frame < 24) || (this.p.frame >= 4 && this.p.frame < 8)) {
  291. this.p.frameName = "standing-right.png";
  292. this.p.canAttack = true;
  293. }
  294. }
  295.  
  296. this._move_dynamic_ramp();
  297. if (this.p.health <= 0) {
  298. this.game.state.start(GAME_CONST.STATES.SHOP);
  299. }
  300. },
  301.  
  302. _update_attack_sequence(){
  303. let current = new Date().getTime();
  304. if (this.p.attack.isAttacking && current > this.p.attack.since + 500) {
  305. this.p.attack.isAttacking = false;
  306. }
  307. },
  308.  
  309. _adjustCharacterPhysicsBound() {
  310. // this.p.body.clearShapes();
  311. //
  312. // if (this.p.frame == "walk") {
  313. // objectName.body.loadPolygon(loadedJSONFileName, walkHitboxName);
  314. // }
  315. //
  316. // else if (currentAnimation == "fight") {
  317. // objectName.body.loadPolygon(loadedJSONFileName, fightHitboxName);
  318. // }
  319. this.p.body.clearShapes();
  320. switch (this.p.frame) {
  321. case 0 : {
  322. this.p.body.loadPolygon("mapPhysics", "attack-left-1");
  323. break;
  324. }
  325. case 1 : {
  326. this.p.body.loadPolygon("mapPhysics", "attack-left-2");
  327. break;
  328. }
  329. case 2 : {
  330. this.p.body.loadPolygon("mapPhysics", "attack-left-3");
  331. break;
  332. }
  333. case 3 : {
  334. this.p.body.loadPolygon("mapPhysics", "attack-left-4");
  335. break;
  336. }
  337. case 4 : {
  338. this.p.body.loadPolygon("mapPhysics", "attack-right-1");
  339. break;
  340. }
  341. case 5 : {
  342. this.p.body.loadPolygon("mapPhysics", "attack-right-2");
  343. break;
  344. }
  345. case 6 : {
  346. this.p.body.loadPolygon("mapPhysics", "attack-right-3");
  347. break;
  348. }
  349. case 7 : {
  350. this.p.body.loadPolygon("mapPhysics", "attack-right-4");
  351. break;
  352. }
  353. case 8 : {
  354. this.p.body.loadPolygon("mapPhysics", "character-run-left-1");
  355. break;
  356. }
  357. case 9 : {
  358. this.p.body.loadPolygon("mapPhysics", "character-run-left-2");
  359. break;
  360. }
  361. case 10 : {
  362. this.p.body.loadPolygon("mapPhysics", "character-run-left-3");
  363. break;
  364. }
  365. case 11 : {
  366. this.p.body.loadPolygon("mapPhysics", "character-run-left-4");
  367. break;
  368. }
  369. case 12 : {
  370. this.p.body.loadPolygon("mapPhysics", "character-run-left-5");
  371. break;
  372. }
  373. case 13 : {
  374. this.p.body.loadPolygon("mapPhysics", "character-run-left-6");
  375. break;
  376. }
  377. case 14 : {
  378. this.p.body.loadPolygon("mapPhysics", "character-run-left-7");
  379. break;
  380. }
  381. case 15 : {
  382. this.p.body.loadPolygon("mapPhysics", "character-run-left-8");
  383. break;
  384. }
  385. case 16 : {
  386. this.p.body.loadPolygon("mapPhysics", "character-run-right-1");
  387. break;
  388. }
  389. case 17 : {
  390. this.p.body.loadPolygon("mapPhysics", "character-run-right-2");
  391. break;
  392. }
  393. case 18 : {
  394. this.p.body.loadPolygon("mapPhysics", "character-run-right-3");
  395. break;
  396. }
  397. case 19 : {
  398. this.p.body.loadPolygon("mapPhysics", "character-run-right-4");
  399. break;
  400. }
  401. case 20 : {
  402. this.p.body.loadPolygon("mapPhysics", "character-run-right-5");
  403. break;
  404. }
  405. case 21 : {
  406. this.p.body.loadPolygon("mapPhysics", "character-run-right-6");
  407. break;
  408. }
  409. case 22 : {
  410. this.p.body.loadPolygon("mapPhysics", "character-run-right-7");
  411. break;
  412. }
  413. case 23 : {
  414. this.p.body.loadPolygon("mapPhysics", "character-run-right-8");
  415. break;
  416. }
  417. case 24: {
  418. this.p.body.loadPolygon("mapPhysics", "standing-left");
  419. break;
  420. }
  421. case 25: {
  422. this.p.body.loadPolygon("mapPhysics", "standing-right");
  423. break;
  424. }
  425. default: {
  426. console.log("Can't resolve Frame Index");
  427. break;
  428. }
  429. }
  430. },
  431.  
  432. shutdown() {
  433. // for (let i = this.game.stage.children.length - 1; i >= 0; i--) {
  434. // this.game.stage.removeChild(this.game.stage.children[i]);
  435. // }
  436. },
  437.  
  438.  
  439. _createLoader() {
  440. this.progressBar = this.add.sprite(this.world.centerX - 360, this.world.centerY, "progressBar");
  441. this.progressBar.anchor.setTo(0, 0.5);
  442. // this.game.stage.addChild(this.progressBar);
  443. this.load.setPreloadSprite(this.progressBar);
  444.  
  445. this.progressBackground = this.add.sprite(this.world.centerX, this.world.centerY, "progressBackground");
  446. this.progressBackground.anchor.setTo(0.5, 0.5);
  447. // this.game.stage.addChild(this.progressBackground);
  448. },
  449.  
  450. _onLoadComplete() {
  451. this._start();
  452. },
  453.  
  454. _start() {
  455. this.loadingComplete = true;
  456. },
  457.  
  458. _checkIfCanJump() {
  459. var result = false;
  460. for (var i = 0; i < this.game.physics.p2.world.narrowphase.contactEquations.length; i++) {
  461. var c = this.game.physics.p2.world.narrowphase.contactEquations[i];
  462.  
  463. if (c.bodyA === this.p.body.data || c.bodyB === this.p.body.data) {
  464. var d = this.p2vec2.dot(c.normalA, this.yAxis);
  465.  
  466. if (c.bodyA === this.p.body.data) {
  467. d *= -1;
  468. }
  469.  
  470. if (d > 0.5) {
  471. result = true;
  472. }
  473. }
  474. }
  475.  
  476. return result;
  477.  
  478. },
  479.  
  480. _createPlayButton() {
  481. this.playButtton = new PlayButton({
  482. game: this.game,
  483. posX: this.world.centerX,
  484. posY: this.world.centerY,
  485. label: 'playGame',
  486. anchorX: 0.5,
  487. anchorY: 0.5
  488. });
  489. // this.game.stage.addChild(this.playButtton);
  490. },
  491.  
  492. check_n_spawn_boss() {
  493. if (this.p.x > GAME_CONST.COORDINATES.arena_x
  494. && this.boss_spawned == false) {
  495. this.boss_spawned = true;
  496. this._spawn_boss(GAME_CONST.COORDINATES.arena_x + 200, 50);
  497. }
  498. },
  499.  
  500. _spawn_boss(x, y) {
  501. this.boss = this.game.bosses.create(x, y, 'enemy');
  502. this.boss.init_x = x;
  503. this.game.physics.enable(this.boss);
  504. this.boss.body.bounce.y = 0.5;
  505. this.boss.body.bounce.x = 1;
  506. this.boss.body.linearDamping = 1;
  507. this.boss.body.collideWorldBounds = true;
  508. this.boss.body.velocity.x = -30;
  509. this.boss.state = {};
  510. this.boss.state.val = 'moving_left';
  511. },
  512.  
  513. _getRPGStats() {
  514. return gameInfo.rpgElements;
  515. },
  516.  
  517. _setRPGStats(value) {
  518. gameInfo.rpgElements = value;
  519. }
  520. };
  521. export default Play;
Add Comment
Please, Sign In to add comment