Files
OpenFrontIO/src/core/execution/SAMMissileExecution.ts
T

84 lines
2.0 KiB
TypeScript

import {
Execution,
Game,
MessageType,
Player,
Unit,
UnitType,
} from "../game/Game";
import { TileRef } from "../game/GameMap";
import { AirPathFinder } from "../pathfinding/PathFinding";
import { PseudoRandom } from "../PseudoRandom";
export class SAMMissileExecution implements Execution {
private active = true;
private pathFinder: AirPathFinder;
private SAMMissile: Unit;
private mg: Game;
constructor(
private spawn: TileRef,
private _owner: Player,
private ownerUnit: Unit,
private target: Unit,
private speed: number = 12,
) {}
init(mg: Game, ticks: number): void {
this.pathFinder = new AirPathFinder(mg, new PseudoRandom(mg.ticks()));
this.mg = mg;
}
tick(ticks: number): void {
if (this.SAMMissile == null) {
this.SAMMissile = this._owner.buildUnit(
UnitType.SAMMissile,
0,
this.spawn,
);
}
if (!this.SAMMissile.isActive()) {
this.active = false;
return;
}
// Mirv warheads are too fast, and mirv shouldn't be stopped ever
const nukesWhitelist = [UnitType.AtomBomb, UnitType.HydrogenBomb];
if (
!this.target.isActive() ||
!this.ownerUnit.isActive() ||
this.target.owner() == this.SAMMissile.owner() ||
!nukesWhitelist.includes(this.target.type())
) {
this.SAMMissile.delete(false);
this.active = false;
return;
}
for (let i = 0; i < this.speed; i++) {
const result = this.pathFinder.nextTile(
this.SAMMissile.tile(),
this.target.tile(),
);
if (result === true) {
this.mg.displayMessage(
`Missile intercepted ${this.target.type()}`,
MessageType.SUCCESS,
this._owner.id(),
);
this.active = false;
this.target.delete();
this.SAMMissile.delete(false);
return;
} else {
this.SAMMissile.move(result);
}
}
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}