50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
import { BASE_FIRE_RATE, BASE_DAMAGE, BASE_HEALTH } from '../config.js';
|
|
import Bullet from './Bullet.js';
|
|
|
|
export default class Base extends Phaser.GameObjects.Sprite {
|
|
constructor(scene, x, y, texture) {
|
|
super(scene, x, y, texture, 0);
|
|
this.scene = scene;
|
|
this.health = BASE_HEALTH;
|
|
this.lastShot = 0;
|
|
this.setOrigin(0.5, 0.5);
|
|
this.setDepth(1);
|
|
}
|
|
|
|
update(time, delta) {
|
|
if (time > this.lastShot + BASE_FIRE_RATE) {
|
|
const target = this.findNearestEnemy();
|
|
if (target) {
|
|
this.shoot(target);
|
|
this.lastShot = time;
|
|
}
|
|
}
|
|
}
|
|
|
|
findNearestEnemy() {
|
|
const enemies = this.scene.enemies.getChildren();
|
|
if (!enemies.length) return null;
|
|
let nearest = null;
|
|
let minDist = Infinity;
|
|
enemies.forEach(e => {
|
|
const d = Phaser.Math.Distance.Between(this.x, this.y, e.x, e.y);
|
|
if (d < minDist) { minDist = d; nearest = e; }
|
|
});
|
|
return nearest;
|
|
}
|
|
|
|
shoot(target) {
|
|
const bullet = new Bullet(this.scene, this.x, this.y, 'bullet', target);
|
|
bullet.damage = BASE_DAMAGE;
|
|
this.scene.bullets.add(bullet);
|
|
this.scene.sound.play('shoot');
|
|
}
|
|
|
|
takeDamage(amount) {
|
|
this.health -= amount;
|
|
this.scene.uiManager.updateBaseHealth(this.health);
|
|
}
|
|
|
|
isDestroyed() { return this.health <= 0; }
|
|
}
|