Files
OpenFrontIO/src/core/execution/ShellExecution.ts
T
Scott Anderson 51519b0b9d Enable strictPropertyInitialization (#1909)
## Description:

Enable the tsconfig option `strictPropertyInitialization`.

Fixes #1907

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
2025-08-23 19:21:40 -04:00

89 lines
2.6 KiB
TypeScript

import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { AirPathFinder } from "../pathfinding/PathFinding";
import { PseudoRandom } from "../PseudoRandom";
import { TileRef } from "../game/GameMap";
export class ShellExecution implements Execution {
private active = true;
private pathFinder: AirPathFinder | undefined;
private shell: Unit | undefined;
private mg: Game | undefined;
private destroyAtTick = -1;
private random: PseudoRandom | undefined;
constructor(
private readonly spawn: TileRef,
private readonly _owner: Player,
private readonly ownerUnit: Unit,
private readonly target: Unit,
) {}
init(mg: Game, ticks: number): void {
this.pathFinder = new AirPathFinder(mg, new PseudoRandom(mg.ticks()));
this.mg = mg;
this.random = new PseudoRandom(mg.ticks());
}
tick(ticks: number): void {
if (this.mg === undefined) throw new Error("Not initialized");
if (this.pathFinder === undefined) throw new Error("Not initialized");
this.shell ??= this._owner.buildUnit(UnitType.Shell, this.spawn, {});
if (!this.shell.isActive()) {
this.active = false;
return;
}
if (
!this.target.isActive() ||
this.target.owner() === this.shell.owner() ||
(this.destroyAtTick !== -1 && this.mg.ticks() >= this.destroyAtTick)
) {
this.shell.delete(false);
this.active = false;
return;
}
if (this.destroyAtTick === -1 && !this.ownerUnit.isActive()) {
this.destroyAtTick = this.mg.ticks() + this.mg.config().shellLifetime();
}
for (let i = 0; i < 3; i++) {
const result = this.pathFinder.nextTile(
this.shell.tile(),
this.target.tile(),
);
if (result === true) {
this.active = false;
this.target.modifyHealth(-this.effectOnTarget(), this._owner);
this.shell.setReachedTarget();
this.shell.delete(false);
return;
} else {
this.shell.move(result);
}
}
}
private effectOnTarget(): number {
if (this.mg === undefined) throw new Error("Not initialized");
if (this.random === undefined) throw new Error("Not initialized");
const { damage } = this.mg.config().unitInfo(UnitType.Shell);
const baseDamage = damage ?? 250;
const roll = this.random.nextInt(1, 6);
const damageMultiplier = (roll - 1) * 25 + 200;
return Math.round((baseDamage / 250) * damageMultiplier);
}
public getEffectOnTargetForTesting(): number {
return this.effectOnTarget();
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}