154 lines
4.5 KiB
JavaScript
154 lines
4.5 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";
|
|
import Item from "./Item";
|
|
import Chest from "./Chest";
|
|
|
|
export default class Player extends Entity {
|
|
|
|
constructor(scene, spawnPoint, texture) {
|
|
super(scene, spawnPoint.x, spawnPoint.y, texture);
|
|
this.setData("speed", 100);
|
|
this.setData('jump', 1.5)
|
|
this.setData('hp', 10);
|
|
this.setOrigin(0.5, 1).setScale(32 / 48);
|
|
|
|
this.body.setSize(48, 48, false);
|
|
this.body.setOffset(4, 16);
|
|
|
|
this.body.setCollideWorldBounds(true);
|
|
if (this.scene.anims.exists('idle'))
|
|
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 = [];
|
|
|
|
this.dieAnimation = 'die';
|
|
this.hitAnimation = 'hit';
|
|
}
|
|
|
|
interact(item) {
|
|
|
|
if (item instanceof Item) {
|
|
if (this.items.length >= 2) {
|
|
this.items.shift().setPosition(this.x, this.y).drop();
|
|
}
|
|
|
|
|
|
item.interact();
|
|
|
|
this.items[this.items.length] = item;
|
|
} else if (item instanceof Chest) {
|
|
item.interact();
|
|
}
|
|
}
|
|
|
|
|
|
hit(dmg) {
|
|
super.hit(dmg);
|
|
if (this.getData('hp') > this.scene.hearts.length)
|
|
this.setData('hp', this.scene.hearts.length);
|
|
|
|
for (let i = this.getData('hp'); i < this.scene.hearts.length; i++) {
|
|
this.scene.hearts[i].alpha = 0;
|
|
}
|
|
for (let i = 0; i < this.getData('hp'); i++) {
|
|
this.scene.hearts[i].alpha = 1;
|
|
}
|
|
}
|
|
|
|
update(...args) {
|
|
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') * this.getData('jump');
|
|
}
|
|
|
|
// 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()
|
|
});
|
|
|
|
|
|
if (this.items.length >= 1)
|
|
this.items[0].setPosition(this.body.x, this.body.y - 16);
|
|
|
|
|
|
if (this.items.length >= 2)
|
|
this.items[1].setPosition(this.body.x + 32, this.body.y - 16);
|
|
|
|
if (this.scene.keyAction1.isDown && this.items.length >= 1) {
|
|
this.items.shift().use(this);
|
|
this.scene.keyAction1.reset();
|
|
}
|
|
|
|
if (this.scene.keyAction2.isDown && this.items.length >= 2) {
|
|
this.items.pop().use(this);
|
|
this.scene.keyAction2.reset();
|
|
}
|
|
}
|
|
|
|
die() {
|
|
this.anims.stop();
|
|
this.pause = true;
|
|
this.play(this.dieAnimation)
|
|
.once('animationcomplete', () => {
|
|
this.setFrame(51);
|
|
this.scene.scene.pause();
|
|
this.scene.music.pause();
|
|
this.scene.scene.launch('GUI', {key: 'Dead'}).bringToTop('GUI');
|
|
})
|
|
}
|
|
} |