class Fighter {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius; // Detection radius
this.speed = 2;
}
// Method to move the fighter towards the target
moveTowards(target) {
const dx = target.x - this.x;
const dy = target.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > this.speed) {
this.x += (dx / distance) * this.speed;
this.y += (dy / distance) * this.speed;
} else {
this.x = target.x;
this.y = target.y;
}
}
// Method to detect and eliminate nukes
eliminateNukes(nukes) {
nukes.forEach((nuke, index) => {
const distance = Math.sqrt(
Math.pow(this.x - nuke.x, 2) + Math.pow(this.y - nuke.y, 2)
);
if (distance <= this.radius) {
console.log(`Nuke at (${nuke.x}, ${nuke.y}) eliminated by fighter at (${this.x}, ${this.y})`);
nukes.splice(index, 1); // Remove the nuke from the list
} else {
this.moveTowards(nuke); // Move towards the nuke if it's not in range
}
});
}
}
class Nuke {
constructor(x, y) {
this.x = x; // Nuke's X-coordinate
this.y = y; // Nuke's Y-coordinate
}
}
// Example
const nukes = [
new Nuke(50, 50),
new Nuke(70, 70),
new Nuke(30, 30)
];
const fighter = new Fighter(10, 10, 10); // Fighter at (10, 10) with a radius of 10
console.log("Before elimination:", nukes);
const interval = setInterval(() => {
fighter.eliminateNukes(nukes);
console.log(Fighter position: (${fighter.x.toFixed(2)}, ${fighter.y.toFixed(2)}));
if (nukes.length === 0) {
console.log("All nukes eliminated!");
clearInterval(interval);
}
}, 100);