Files
OpenFrontIO/src/core/execution/ConstructionExecution.ts
T
Ryan f1561df470 Bomb Direction (#2435)
Resolves #2434 

## Description:

Allows bomb direction to be inverted by pressing a hotkey - currently
"U".

**Check the issue for screenshots / videos.**

## 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

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

w.o.n

---------

Co-authored-by: Evan <evanpelle@gmail.com>
Co-authored-by: iamlewis <lewismmmm@gmail.com>
2025-12-29 09:03:46 -08:00

177 lines
5.0 KiB
TypeScript

import { Execution, Game, Player, Tick, Unit, UnitType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { CityExecution } from "./CityExecution";
import { DefensePostExecution } from "./DefensePostExecution";
import { FactoryExecution } from "./FactoryExecution";
import { MirvExecution } from "./MIRVExecution";
import { MissileSiloExecution } from "./MissileSiloExecution";
import { NukeExecution } from "./NukeExecution";
import { PortExecution } from "./PortExecution";
import { SAMLauncherExecution } from "./SAMLauncherExecution";
import { WarshipExecution } from "./WarshipExecution";
export class ConstructionExecution implements Execution {
private structure: Unit | null = null;
private active: boolean = true;
private mg: Game;
private ticksUntilComplete: Tick;
constructor(
private player: Player,
private constructionType: UnitType,
private tile: TileRef,
private rocketDirectionUp?: boolean,
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
if (this.mg.config().isUnitDisabled(this.constructionType)) {
console.warn(
`cannot build construction ${this.constructionType} because it is disabled`,
);
this.active = false;
return;
}
if (!this.mg.isValidRef(this.tile)) {
console.warn(`cannot build construction invalid tile ${this.tile}`);
this.active = false;
return;
}
}
tick(ticks: number): void {
if (this.structure === null) {
const info = this.mg.unitInfo(this.constructionType);
// For non-structure units (nukes/warship), charge once and delegate to specialized executions.
const isStructure = this.isStructure(this.constructionType);
if (!isStructure) {
// Defer validation and gold deduction to the specific execution
this.completeConstruction();
this.active = false;
return;
}
// Structures: build real unit and mark under construction
const spawnTile = this.player.canBuild(this.constructionType, this.tile);
if (spawnTile === false) {
console.warn(`cannot build ${this.constructionType}`);
this.active = false;
return;
}
this.structure = this.player.buildUnit(
this.constructionType,
spawnTile,
{},
);
const duration = info.constructionDuration ?? 0;
if (duration > 0) {
this.structure.setUnderConstruction(true);
this.ticksUntilComplete = duration;
return;
}
// No construction time
this.completeConstruction();
this.active = false;
return;
}
if (!this.structure.isActive()) {
this.active = false;
return;
}
if (this.player !== this.structure.owner()) {
this.player = this.structure.owner();
}
if (this.ticksUntilComplete === 0) {
this.player = this.structure.owner();
this.completeConstruction();
this.active = false;
return;
}
this.ticksUntilComplete--;
}
private completeConstruction() {
if (this.structure) {
this.structure.setUnderConstruction(false);
}
const player = this.player;
switch (this.constructionType) {
case UnitType.AtomBomb:
case UnitType.HydrogenBomb:
this.mg.addExecution(
new NukeExecution(
this.constructionType,
player,
this.tile,
null,
-1,
0,
this.rocketDirectionUp,
),
);
break;
case UnitType.MIRV:
this.mg.addExecution(new MirvExecution(player, this.tile));
break;
case UnitType.Warship:
this.mg.addExecution(
new WarshipExecution({ owner: player, patrolTile: this.tile }),
);
break;
case UnitType.Port:
this.mg.addExecution(new PortExecution(this.structure!));
break;
case UnitType.MissileSilo:
this.mg.addExecution(new MissileSiloExecution(this.structure!));
break;
case UnitType.DefensePost:
this.mg.addExecution(new DefensePostExecution(this.structure!));
break;
case UnitType.SAMLauncher:
this.mg.addExecution(
new SAMLauncherExecution(player, null, this.structure!),
);
break;
case UnitType.City:
this.mg.addExecution(new CityExecution(this.structure!));
break;
case UnitType.Factory:
this.mg.addExecution(new FactoryExecution(this.structure!));
break;
default:
console.warn(
`unit type ${this.constructionType} cannot be constructed`,
);
break;
}
}
private isStructure(type: UnitType): boolean {
switch (type) {
case UnitType.Port:
case UnitType.MissileSilo:
case UnitType.DefensePost:
case UnitType.SAMLauncher:
case UnitType.City:
case UnitType.Factory:
return true;
default:
return false;
}
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}