Files
OpenFrontIO/src/core/execution/SAMMissileExecution.ts
T
evanpelle 1d0732d3d9 Refactor UnitSpecific info => AllUnitParams type union (#701)
## Description:

By using a type union we get better type safety, enforcing each unit
type have the appropriate params when initializing

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:
evan

---------

Co-authored-by: evan <openfrontio@gmail.com>
2025-05-10 11:40:47 -07:00

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,
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;
}
}