SAMLauncherExecution

This commit is contained in:
Scott Anderson
2025-04-14 20:40:40 -04:00
parent d45efb0bbd
commit 82ab591bba
+14 -10
View File
@@ -13,9 +13,9 @@ import { PseudoRandom } from "../PseudoRandom";
import { SAMMissileExecution } from "./SAMMissileExecution";
export class SAMLauncherExecution implements Execution {
private player: Player;
private mg: Game;
private sam: Unit;
private player: Player | null = null;
private mg: Game | null = null;
private sam: Unit | null = null;
private active: boolean = true;
private target: Unit | null = null;
@@ -40,9 +40,12 @@ export class SAMLauncherExecution implements Execution {
}
tick(ticks: number): void {
if (this.sam == null) {
if (this.mg === null || this.player === null) {
throw new Error("Not initialized");
}
if (this.sam === null) {
const spawnTile = this.player.canBuild(UnitType.SAMLauncher, this.tile);
if (spawnTile == false) {
if (spawnTile === false) {
consolex.warn("cannot build SAM Launcher");
this.active = false;
return;
@@ -56,11 +59,11 @@ export class SAMLauncherExecution implements Execution {
return;
}
if (this.player != this.sam.owner()) {
if (this.player !== this.sam.owner()) {
this.player = this.sam.owner();
}
if (!this.pseudoRandom) {
if (this.pseudoRandom === null) {
this.pseudoRandom = new PseudoRandom(this.sam.id());
}
@@ -71,7 +74,8 @@ export class SAMLauncherExecution implements Execution {
])
.filter(
({ unit }) =>
unit.owner() !== this.player && !this.player.isFriendly(unit.owner()),
unit.owner() !== this.player &&
!this.player?.isFriendly(unit.owner()),
);
this.target =
@@ -97,7 +101,7 @@ export class SAMLauncherExecution implements Execution {
if (
this.sam.isCooldown() &&
this.sam.ticksLeftInCooldown(this.mg.config().SAMCooldown()) == 0
this.sam.ticksLeftInCooldown(this.mg.config().SAMCooldown()) === 0
) {
this.sam.setCooldown(false);
}
@@ -106,7 +110,7 @@ export class SAMLauncherExecution implements Execution {
this.sam.setCooldown(true);
const random = this.pseudoRandom.next();
let hit = true;
if (this.target.type() != UnitType.AtomBomb) {
if (this.target.type() !== UnitType.AtomBomb) {
hit = random < this.mg.config().samHittingChance();
}
if (!hit) {