Asteroids-2026/js/entities/AlienShip.js

55 lines
1.6 KiB
JavaScript

const ALIEN_SPEED = 130; // pixels/sec
const ALIEN_FIRE_RATE = 2500; // ms between shots
export default class AlienShip {
constructor(scene, x, y, group) {
this.scene = scene;
this.alive = true;
this.sprite = scene.physics.add.sprite(x, y, 'alien');
group.add(this.sprite);
this.sprite.gameEntity = this;
this.sprite.body.setAllowGravity(false);
// Circular hitbox: texture is 64x48, use radius 20 centered at (32,24)
this.sprite.body.setCircle(20, 12, 4);
this.lastFired = 0;
}
update(time, player) {
if (!this.alive) return;
if (!player || !player.alive || !player.sprite.active) return;
// Move toward player
const dx = player.sprite.x - this.sprite.x;
const dy = player.sprite.y - this.sprite.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 5) {
this.sprite.setVelocity(
(dx / dist) * ALIEN_SPEED,
(dy / dist) * ALIEN_SPEED
);
}
// Fire at player periodically
if (time - this.lastFired > ALIEN_FIRE_RATE) {
this.lastFired = time;
const angle = Phaser.Math.RadToDeg(Math.atan2(dy, dx));
this.scene.spawnAlienBullet(this.sprite.x, this.sprite.y, angle);
}
// Wrap around screen
this.scene.physics.world.wrap(this.sprite, 40);
}
destroy() {
if (!this.alive) return;
this.alive = false;
if (this.sprite && this.sprite.active) {
this.sprite.destroy();
}
}
}