Vehicular Combat Game example
class Vehicle {
constructor(x, y) {
this.x = x;
this.y = y;
this.health = 100;
}
move(x, y) {
this.x = x;
this.y = y;
}
attack(target) {
target.health -= 10;
console.log(`${this.constructor.name} attacks ${target.constructor.name} for 10 damage.`);
}
}
class Tank extends Vehicle {
constructor(x, y, turretX, turretY) {
super(x, y);
this.turretX = turretX;
this.turretY = turretY;
}
attack(target) {
const turretDistance = Math.sqrt(Math.pow(target.x - this.turretX, 2) + Math.pow(target.y - this.turretY, 2));
if (turretDistance <= 5) {
super.attack(target);
}
}
}
class Helicopter extends Vehicle {
constructor(x, y) {
super(x, y);
this.minAltitude = 10;
this.maxAltitude = 100;
}
move(x, y) {
super.move(x, y);
console.log(`${this.constructor.name} flies to (${x}, ${y}).`);
}
changeAltitude(altitude) {
if (altitude >= this.minAltitude && altitude <= this.maxAltitude) {
this.y = altitude;
console.log(`${this.constructor.name} changes altitude to ${altitude}.`);
}
}
}
const tank = new Tank(0, 0, 5, 5);
const helicopter = new Helicopter(10, 10);
helicopter.changeAltitude(50);
// Output: "Helicopter changes altitude to 50."
helicopter.move(5, 5);
// Output: "Helicopter flies to (5, 5)."
tank.attack(helicopter);
// Output: "Tank attacks Helicopter for 10 damage."
helicopter.changeAltitude(200);
// Output: "Altitude out of range."Last updated