Aethelgard/js/objects/Enemy.js

24 lines
762 B
JavaScript

export default class Enemy extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y, texture, cfg) {
super(scene, x, y, texture, 0);
this.scene = scene;
this.health = cfg.health;
this.speed = cfg.speed;
this.xpValue = cfg.xp;
this.collisionDamage = cfg.collisionDamage || 20;
scene.physics.world.enable(this);
scene.add.existing(this);
this.setOrigin(0.5, 0.5);
}
preUpdate(time, delta) {
super.preUpdate(time, delta);
const base = this.scene.base;
const angle = Phaser.Math.Angle.Between(this.x, this.y, base.x, base.y);
this.scene.physics.velocityFromRotation(angle, this.speed, this.body.velocity);
}
takeDamage(amount) { this.health -= amount; }
isDead() { return this.health <= 0; }
}