Files
OpenFrontIO/src/core/execution/DeleteUnitExecution.ts
T
Kipstz 4b129a2f7f Add button for remove building (#1609)
## Description:

Added a red delete button with trash can icon to the right-click radial
menu that allows players to voluntarily delete their own units.

## 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
- [x] I have read and accepted the CLA agreement (only required once).

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

Kipstz

<img width="286" height="209" alt="image"
src="https://github.com/user-attachments/assets/85142be3-2aa5-4c84-ab30-0c68289c8f85"
/>

---------

Co-authored-by: Drills Kibo <59177241+drillskibo@users.noreply.github.com>
2025-09-03 16:06:07 -07:00

82 lines
1.8 KiB
TypeScript

import { Execution, Game, MessageType, Player } from "../game/Game";
export class DeleteUnitExecution implements Execution {
private active: boolean = true;
private mg: Game;
constructor(
private player: Player,
private unitId: number,
) {}
activeDuringSpawnPhase(): boolean {
return false;
}
init(mg: Game, ticks: number) {
if (!this.active) {
return;
}
this.mg = mg;
const unit = this.player.units().find((u) => u.id() === this.unitId);
if (!unit) {
console.warn(
`SECURITY: unit ${this.unitId} not found or not owned by player ${this.player.displayName()}`,
);
this.active = false;
return;
}
if (!unit.isActive()) {
console.warn(`SECURITY: unit ${this.unitId} is not active`);
this.active = false;
return;
}
const tileOwner = mg.owner(unit.tile());
if (!tileOwner.isPlayer() || tileOwner.id() !== this.player.id()) {
console.warn(
`SECURITY: unit ${this.unitId} is not on player's territory`,
);
this.active = false;
return;
}
if (!mg.isLand(unit.tile())) {
console.warn(`SECURITY: unit ${this.unitId} is not on land`);
this.active = false;
return;
}
if (mg.inSpawnPhase()) {
console.warn(`SECURITY: cannot delete units during spawn phase`);
this.active = false;
return;
}
if (!this.player.canDeleteUnit()) {
console.warn(`SECURITY: delete unit cooldown not expired`);
this.active = false;
return;
}
unit.delete(false);
this.player.recordDeleteUnit();
this.mg.displayMessage(
`events_display.unit_voluntarily_deleted`,
MessageType.UNIT_DESTROYED,
this.player.id(),
);
this.active = false;
}
tick(ticks: number) {}
isActive(): boolean {
return this.active;
}
}