Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import GAME_CONST from "../const/GAME_CONST";
- import PlayButton from "../objects/widgets/buttons/PlayButton";
- import gameInfo from "../objects/Store/GameInfo";
- var Play = function () {
- }
- Play.prototype = {
- preload() {
- this.boss_spawned = false;
- this.counter = 0;
- this.is_enemy_spawned = [];
- this.enemy = [];
- this.ramp = [];
- },
- create() {
- this.game.physics.startSystem(Phaser.Physics.P2JS);
- this.game.world.setBounds(0, 0, 7680, 1080);
- this.p = this.game.add.sprite(50, 0, 'character');
- this.p.rpg = this._getRPGStats();
- this.p.health = 2;
- this.p.maxHeath = 2;
- this.enemy = [];
- this.game.physics.p2.enable(this.p);
- this.yAxis = p2.vec2.fromValues(0, 1);
- this.p2vec2 = p2.vec2;
- this.p.body.clearShapes();
- this.p.body.loadPolygon("mapPhysics", "phaser-dude");
- this.game.physics.p2.gravity.y = 4900;
- this.p.body.fixedRotation = true;
- this.game.camera.follow(this.p);
- this.p.animations.add('walk_left', [8, 9, 10, 11, 12, 13, 15]);
- this.p.animations.add('walk_right', [16, 17, 18, 19, 20, 21, 22, 23]);
- this.p.animations.add('attack_left', [0, 1, 2, 3]);
- this.p.animations.add('attack_right', [4, 5, 6, 7]);
- this.p.animations.play('attack_right', 10, true);
- this.p.attack = {};
- this.p.attack.isAttacking = false;
- this.p.canAttack = true;
- this.p.attack.since = new Date().getTime();
- this.cursors = this.game.input.keyboard.createCursorKeys();
- this.game.input.gamepad.start();
- this.ground = this.add.sprite(3840, 747, 'platform', 0);
- this.ground.anchor.setTo(0, 0);
- this.game.physics.p2.enable(this.ground);
- this.healthMeterBar = this.game.add.plugin(Phaser.Plugin.HealthMeter);
- this.healthMeterBar.bar(this.p, {
- y: 20, x: 50,
- width: 100, height: 20,
- foreground: '#323232',
- background: '#aaaaaa',
- alpha: 0.6
- });
- //
- this.ground.body.clearShapes();
- this.ground.body.loadPolygon("mapPhysics", "ground");
- this.ground.body.dynamic = false;
- this.ground.body.gravityScale = 0;
- this.characterMaterial = this.game.physics.p2.createMaterial('characterMaterial', this.p.body);
- this.groundMaterial = this.game.physics.p2.createMaterial('groundMaterial', this.ground.body);
- this.contactMaterial = this.game.physics.p2.createContactMaterial(this.characterMaterial, this.groundMaterial);
- this.contactMaterial.restitution = 0;
- //init dumb enemies
- this.game.dumb_enemies = this.game.add.physicsGroup(
- Phaser.Physics.P2JS,
- this.game.map,
- 'aliens'
- );
- this.game.dumb_enemies.lastSpawned = new Date().getTime();
- this._add_static_ramps();
- this._add_enemy_home();
- // setTimeout(function(){
- // this.game.state.start(GAME_CONST.STATES.SHOP);
- // }.bind(this), 10000);
- },
- _add_enemy_home() {
- this.enemy_home = this.add.sprite(1300, 773, 'house', 0);
- this.enemy_home.anchor.setTo(0, 0);
- this.game.physics.p2.enable(this.enemy_home);
- //
- this.enemy_home.body.clearShapes();
- this.enemy_home.body.loadPolygon("mapPhysics", 'house');
- this.enemy_home.body.dynamic = false;
- this.enemy_home.body.gravityScale = 0;
- },
- _check_n_spawn_enemy() {
- var length = this.enemy.length;
- var currentSpawn = new Date().getTime();
- if (currentSpawn > this.game.dumb_enemies.lastSpawned + 5000) {
- this.enemy[length] = this.game.dumb_enemies.create(1200, 773, 'alien');
- this.game.physics.p2.enable(this.enemy[length]);
- this.enemy[length].body.velocity.x = -200;
- this.game.dumb_enemies.lastSpawned = currentSpawn;
- }
- },
- _move_enemies(){
- let i = 0
- for (; i < this.enemy.length; i++) {
- if (this.enemy[i].body != null) {
- this.enemy[i].body.velocity.x = -200;
- }
- if (this.enemy[i].body != null && this.enemy[i].body.x < 95) {
- this.enemy[i].destroy();
- }
- }
- },
- _check_who_dies() {
- for (let i = 0; i < this.enemy.length; i++) {
- if (this.enemy[i].body != null) {
- if (this._check_if_collides(this.enemy[i])) {
- if (this.p.attack.isAttacking) {
- this.enemy[i].destroy();
- return false;
- } else {
- this.enemy[i].destroy();
- this.p.health--;
- return true;
- }
- }
- }
- }
- return false;
- },
- _check_if_collides(enemy){
- // console.log("/////////////////");
- // console.log("x diff: " + (enemy.x - this.p.x));
- // console.log("x widths: " + (enemy.width / 2 + this.p.width / 2));
- // console.log("y diff: " + (enemy.y - this.p.y));
- // console.log("x height: " + (enemy.height / 2 + this.p.height / 2));
- // cons ole.log("/////////////////");
- if (enemy.x - this.p.x < enemy.width / 2 + this.p.width / 2) {
- if (enemy.y - this.p.y < enemy.height / 2 + this.p.height / 2) {
- return true;
- }
- }
- return false;
- },
- _add_static_ramps() {
- this._add_ramp_at_pos(1042, 547, 'floating-ramp-1');
- this._add_ramp_at_pos(2890, 397, 'floating-ramp-2');
- this._add_ramp_at_pos(3345, 297, 'floating-ramp-3');
- this.ramp[2].min_y = 347;
- this.ramp[2].max_y = 647;
- this.ramp[2].state = 'stationary_top';
- this.ramp[2].stateSince = new Date().getTime();
- this._add_ramp_at_pos(3866, 397, 'floating-ramp-4');
- },
- _move_dynamic_ramp() {
- if (this.ramp[2] != undefined && this.ramp[2].stateSince != undefined) {
- var currentTime = new Date().getTime();
- if (this.ramp[2].state == 'stationary_top' &&
- currentTime > this.ramp[2].stateSince + 5000) {
- this.ramp[2].state = 'moving_bottom';
- this.ramp[2].stateSince = currentTime;
- this.ramp[2].body.velocity.y = 200;
- }
- if (this.ramp[2].state == 'moving_bottom' &&
- this.ramp[2].y > this.ramp[2].max_y) {
- this.ramp[2].state = 'stationary_bottom';
- this.ramp[2].stateSince = currentTime;
- this.ramp[2].body.velocity.y = 0;
- }
- if (this.ramp[2].state == 'stationary_bottom' &&
- currentTime > this.ramp[2].stateSince + 1500) {
- this.ramp[2].state = 'moving_up';
- this.ramp[2].stateSince = currentTime;
- this.ramp[2].body.velocity.y = -200;
- }
- if (this.ramp[2].state == 'moving_up' &&
- this.ramp[2].y < this.ramp[2].min_y) {
- this.ramp[2].state = 'stationary_top';
- this.ramp[2].stateSince = currentTime;
- this.ramp[2].body.velocity.y = 0;
- }
- }
- },
- _add_ramp_at_pos(x, y, image) {
- var rampCount = this.ramp.length;
- this.ramp[rampCount] = this.add.sprite(x, y, image, 0);
- this.ramp[rampCount].anchor.setTo(0, 0);
- this.game.physics.p2.enable(this.ramp[rampCount]);
- //
- this.ramp[rampCount].body.clearShapes();
- this.ramp[rampCount].body.loadPolygon("mapPhysics", image);
- this.ramp[rampCount].body.dynamic = false;
- this.ramp[rampCount].body.gravityScale = 0;
- },
- u_controller_clicked(button) {
- if (button.name == "leftButton") {
- this.p.body.velocity.x = -1 * GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
- }
- else if (this.name == "rightButton") {
- console.log("WE are right" + button.name);
- this.p.body.velocity.x = GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
- }
- },
- update() {
- // console.log("x: " + this.p.x + " y: " + this.p.y);
- this._adjustCharacterPhysicsBound();
- this._check_n_spawn_enemy();
- this._move_enemies();
- this._update_attack_sequence();
- this._check_who_dies()
- if (this.p.health <= 0) {
- // console.log("game over");
- // this.game.state.start(GAME_CONST.STATES.SHOP);
- }
- if (this.p.y > 1080) {
- // console.log("game over");
- }
- // this.p.rotation = 0;
- this.p.body.velocity.x = 0;
- // this.p.body.velocity.y = 0;
- let isCurserDown = false;
- if (this.cursors.up.isDown) {
- // if (this.p.body.onFloor()) {
- // this.p.body.velocity.y = -1 * GAME_CONST.VELOCITY.y[this.p.rpg.y_index];
- // }
- if (this._checkIfCanJump()) {
- this.p.body.velocity.y = -9 * GAME_CONST.VELOCITY.y[this.p.rpg.y_index];
- }
- isCurserDown = true;
- }
- //should have been stationary before attacking
- else if (this.cursors.down.isDown && this.p.canAttack) {
- this.p.attack.isAttacking = true;
- this.p.attack.since = new Date().getTime();
- }
- if (this.p.attack.isAttacking) {
- isCurserDown = true;
- if ((this.p.frame >= 8 && this.p.frame < 16) || (this.p.frame >= 0 && this.p.frame < 3) || this.p.frame === 24) {
- this.p.animations.play('attack_left', 10, true);
- }
- else if ((this.p.frame >= 16 && this.p.frame < 24) || (this.p.frame >= 4 && this.p.frame < 7) || this.p.frame === 25) {
- this.p.animations.play('attack_right', 10, true);
- }
- }
- if (this.cursors.left.isDown) {
- this.p.body.velocity.x = -2 * GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
- this.p.animations.play('walk_left', 10, true);
- this.p.canAttack = false;
- isCurserDown = true;
- this.p.attack.isAttacking = false;
- }
- else if (this.cursors.right.isDown) {
- this.p.body.velocity.x = 3 * GAME_CONST.VELOCITY.x[this.p.rpg.x_index];
- this.p.animations.play('walk_right', 10, true);
- this.p.canAttack = false;
- isCurserDown = true;
- this.p.attack.isAttacking = false;
- }
- if (!isCurserDown) {
- this.p.animations.currentAnim.stop();
- if ((this.p.frame >= 8 && this.p.frame < 16) || (this.p.frame >= 0 && this.p.frame < 4)) {
- this.p.frameName = "standing-left.png";
- this.p.canAttack = "true";
- }
- else if ((this.p.frame >= 16 && this.p.frame < 24) || (this.p.frame >= 4 && this.p.frame < 8)) {
- this.p.frameName = "standing-right.png";
- this.p.canAttack = true;
- }
- }
- this._move_dynamic_ramp();
- if (this.p.health <= 0) {
- this.game.state.start(GAME_CONST.STATES.SHOP);
- }
- },
- _update_attack_sequence(){
- let current = new Date().getTime();
- if (this.p.attack.isAttacking && current > this.p.attack.since + 500) {
- this.p.attack.isAttacking = false;
- }
- },
- _adjustCharacterPhysicsBound() {
- // this.p.body.clearShapes();
- //
- // if (this.p.frame == "walk") {
- // objectName.body.loadPolygon(loadedJSONFileName, walkHitboxName);
- // }
- //
- // else if (currentAnimation == "fight") {
- // objectName.body.loadPolygon(loadedJSONFileName, fightHitboxName);
- // }
- this.p.body.clearShapes();
- switch (this.p.frame) {
- case 0 : {
- this.p.body.loadPolygon("mapPhysics", "attack-left-1");
- break;
- }
- case 1 : {
- this.p.body.loadPolygon("mapPhysics", "attack-left-2");
- break;
- }
- case 2 : {
- this.p.body.loadPolygon("mapPhysics", "attack-left-3");
- break;
- }
- case 3 : {
- this.p.body.loadPolygon("mapPhysics", "attack-left-4");
- break;
- }
- case 4 : {
- this.p.body.loadPolygon("mapPhysics", "attack-right-1");
- break;
- }
- case 5 : {
- this.p.body.loadPolygon("mapPhysics", "attack-right-2");
- break;
- }
- case 6 : {
- this.p.body.loadPolygon("mapPhysics", "attack-right-3");
- break;
- }
- case 7 : {
- this.p.body.loadPolygon("mapPhysics", "attack-right-4");
- break;
- }
- case 8 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-1");
- break;
- }
- case 9 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-2");
- break;
- }
- case 10 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-3");
- break;
- }
- case 11 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-4");
- break;
- }
- case 12 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-5");
- break;
- }
- case 13 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-6");
- break;
- }
- case 14 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-7");
- break;
- }
- case 15 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-left-8");
- break;
- }
- case 16 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-1");
- break;
- }
- case 17 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-2");
- break;
- }
- case 18 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-3");
- break;
- }
- case 19 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-4");
- break;
- }
- case 20 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-5");
- break;
- }
- case 21 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-6");
- break;
- }
- case 22 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-7");
- break;
- }
- case 23 : {
- this.p.body.loadPolygon("mapPhysics", "character-run-right-8");
- break;
- }
- case 24: {
- this.p.body.loadPolygon("mapPhysics", "standing-left");
- break;
- }
- case 25: {
- this.p.body.loadPolygon("mapPhysics", "standing-right");
- break;
- }
- default: {
- console.log("Can't resolve Frame Index");
- break;
- }
- }
- },
- shutdown() {
- // for (let i = this.game.stage.children.length - 1; i >= 0; i--) {
- // this.game.stage.removeChild(this.game.stage.children[i]);
- // }
- },
- _createLoader() {
- this.progressBar = this.add.sprite(this.world.centerX - 360, this.world.centerY, "progressBar");
- this.progressBar.anchor.setTo(0, 0.5);
- // this.game.stage.addChild(this.progressBar);
- this.load.setPreloadSprite(this.progressBar);
- this.progressBackground = this.add.sprite(this.world.centerX, this.world.centerY, "progressBackground");
- this.progressBackground.anchor.setTo(0.5, 0.5);
- // this.game.stage.addChild(this.progressBackground);
- },
- _onLoadComplete() {
- this._start();
- },
- _start() {
- this.loadingComplete = true;
- },
- _checkIfCanJump() {
- var result = false;
- for (var i = 0; i < this.game.physics.p2.world.narrowphase.contactEquations.length; i++) {
- var c = this.game.physics.p2.world.narrowphase.contactEquations[i];
- if (c.bodyA === this.p.body.data || c.bodyB === this.p.body.data) {
- var d = this.p2vec2.dot(c.normalA, this.yAxis);
- if (c.bodyA === this.p.body.data) {
- d *= -1;
- }
- if (d > 0.5) {
- result = true;
- }
- }
- }
- return result;
- },
- _createPlayButton() {
- this.playButtton = new PlayButton({
- game: this.game,
- posX: this.world.centerX,
- posY: this.world.centerY,
- label: 'playGame',
- anchorX: 0.5,
- anchorY: 0.5
- });
- // this.game.stage.addChild(this.playButtton);
- },
- check_n_spawn_boss() {
- if (this.p.x > GAME_CONST.COORDINATES.arena_x
- && this.boss_spawned == false) {
- this.boss_spawned = true;
- this._spawn_boss(GAME_CONST.COORDINATES.arena_x + 200, 50);
- }
- },
- _spawn_boss(x, y) {
- this.boss = this.game.bosses.create(x, y, 'enemy');
- this.boss.init_x = x;
- this.game.physics.enable(this.boss);
- this.boss.body.bounce.y = 0.5;
- this.boss.body.bounce.x = 1;
- this.boss.body.linearDamping = 1;
- this.boss.body.collideWorldBounds = true;
- this.boss.body.velocity.x = -30;
- this.boss.state = {};
- this.boss.state.val = 'moving_left';
- },
- _getRPGStats() {
- return gameInfo.rpgElements;
- },
- _setRPGStats(value) {
- gameInfo.rpgElements = value;
- }
- };
- export default Play;
Add Comment
Please, Sign In to add comment