Files
MI2Mario/src/entities/Player.js
T

115 lines
3.3 KiB
JavaScript

/*
* Copyright (c) 2020. Antonio Martinez Casadesus and Pascal Syma.
* All rights reserved.
*/
import Entity from "./Entity";
import AttackA from "./Attacks/AttackA";
import AttackB from "./Attacks/AttackB";
export default class Player extends Entity {
constructor(scene, spawnPoint, texture) {
super(scene, spawnPoint.x, spawnPoint.y, texture);
this.setData("speed", 100);
this.setData('hp', 20);
this.setOrigin(0.5, 1).setScale(32 / 48);
this.body.setSize(48, 48, false);
this.body.setOffset(4, 16);
this.body.setCollideWorldBounds(true);
this.play('idle');
this.attackA = new AttackA(this);
this.attackB = new AttackB(this);
this.attackHitbox = this.scene.add.group([
this.attackA,
this.attackB
]);
this.items = [];
}
pickup(item) {
if (this.items.length >= 1) {
this.items.shift().setPosition(this.x, this.y).drop();
}
console.log(item);
item.pickup();
this.items[0] = item;
}
update(...args) {
// if (this.scene.dieActive()) {
// this.pause = true;
// this.play('die', true)
// .once('animationcomplete', () => {
// this.setFrame(51);
// this.scene.scene.pause();
// })
// }
if (this.pause)
return;
this.body.velocity.x = 0;
// Jumping, Dashing
if (this.scene.upActive() && this.body.blocked.down) {
this.body.velocity.y = -this.getData('speed') * 1.5;
}
// Walking L R
if (this.scene.leftActive()) {
this.body.velocity.x = -this.getData('speed');
if (!this.flipX)
this.x -= 16;
this.setFlipX(true);
this.body.setOffset(32 - 4, 16);
} else if (this.scene.rightActive()) {
this.body.velocity.x = this.getData('speed');
if (this.flipX)
this.x += 16;
this.setFlipX(false);
this.body.setOffset(4, 16);
}
// this.setData('speed', 100);
// animation
if (this.anims.getCurrentKey().startsWith('attack_')) {
// this.setData('speed', 50);
} else if (this.scene.keySpace.isDown) {
// const nextAttack = [this.attackA].sort(() => 0.5 - Math.random())[0];
// nextAttack.start();
if (this.scene.upActive())
this.attackA.start();
else
this.attackB.start();
} else if (this.body.blocked.down) {
if (this.body.velocity.x !== 0) {
this.play('run', true);
} else {
this.play('idle', true);
}
} else if (this.anims.getCurrentKey() !== 'jump_start' && this.anims.getCurrentKey() !== 'jump') {
// this.play('jump', true)
this.play('jump_start', true).on('animationcomplete', () => this.play('jump', true));
}
this.attackHitbox.getChildren().forEach(c => {
c.update()
});
this.items.forEach(i => i.setPosition(this.body.x, this.body.y - 16));
if (this.scene.keyAction1.isDown && this.items.length >= 1) {
this.items.shift().die();
}
}
}