overrun/js/entities/enemies/ChaseEnemy.js

67 lines
1.6 KiB
JavaScript

import { BaseEnemy } from './BaseEnemy.js';
const GRID = 80;
export class ChaseEnemy extends BaseEnemy {
constructor(scene, x, y, player) {
super(scene, x, y, player, {
frameOffset: 0,
radius: 14,
hp: 40,
speed: 80,
xp: 15,
contactDamage: 12,
});
}
update(delta) {
super.update(delta);
const angle = Phaser.Math.Angle.Between(this.x, this.y, this.player.x, this.player.y);
this.sprite.body.setVelocity(
Math.cos(angle) * this.speed,
Math.sin(angle) * this.speed
);
}
_die() {
this._gridDeathPulse(this.x, this.y);
super._die();
}
_gridDeathPulse(px, py) {
const scene = this.scene;
const W = scene.scale.width;
const H = scene.scale.height;
const col = Math.floor(px / GRID);
const row = Math.floor(py / GRID);
const makeTile = (c, r, alpha) => {
if (c < 0 || r < 0 || c * GRID >= W || r * GRID >= H) return null;
return scene.add.rectangle(
c * GRID + GRID / 2,
r * GRID + GRID / 2,
GRID, GRID, 0x00ff44, alpha,
).setDepth(2);
};
const center = makeTile(col, row, 0.55);
// Pre-create adjacent tiles but hide them until ADJ_MS elapses
const adjacents = [];
[[-1, 0], [1, 0], [0, -1], [0, 1]].forEach(([dc, dr]) => {
const t = makeTile(col + dc, row + dr, 0.38);
if (t) { t.setVisible(false); adjacents.push(t); }
});
scene._pulseEffects.push({
center,
adjacents,
elapsed: 0,
adjSpawned: false,
ADJ_MS: 80,
FADE_MS: 120,
END_MS: 400,
});
}
}