Fix train was destroyed message spam (#2774)

## Description:

Trains are made of a primary unit (`TrainExecution.train`) followed by 6
cars (`TrainExecution.cars`). Currently when any of the units is
destroyed, a message "Your Train was destroyed" is shown. In worst case
scenario, when all cars are blasted by a nuke, 7 messages are displayed
to the user, but the train continues.

Since the actual logic is unaffected as long as the primary unit stays
alive, displaying messages is confusing. This PR fixes it. The message
is only displayed when the primary unit is destroyed. The following cars
are only there for fancy visuals.



## Screencast

##### Current - multiple messages for single train


https://github.com/user-attachments/assets/3df04c71-d899-4f68-af83-36c9d0ffa730

First nuke was a test.
Second nuke destroyed the entire train, prompting 7 messages.
Third nuke destroyed one full train and then some. Prompting many too
many messages.

##### Fixed - one message, only if train was fully destroyed


https://github.com/user-attachments/assets/1f3840a7-6c62-487d-af3a-82de39dad9e8

First train was only partially destroyed, no message was shown, delivery
mission completed A+.
Following 2 trains had the front engine destroyed and therefore were
promptly decommissioned.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [ ] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [ ] 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:

moleole
This commit is contained in:
Arkadiusz Sygulski
2026-01-03 03:53:24 +01:00
committed by GitHub
parent ae6293f6da
commit ab5b044362
4 changed files with 37 additions and 14 deletions
+12 -5
View File
@@ -80,11 +80,18 @@ export const loadAllSprites = async (): Promise<void> => {
* The train sprites rely on the train attributes and not only on its type
*/
function trainTypeToSpriteType(unit: UnitView): TrainTypeSprite {
return unit.trainType() === TrainType.Engine
? TrainTypeSprite.Engine
: unit.isLoaded()
? TrainTypeSprite.LoadedCarriage
: TrainTypeSprite.Carriage;
const trainType = unit.trainType();
switch (trainType) {
case TrainType.Engine:
case TrainType.TailEngine:
return TrainTypeSprite.Engine;
case TrainType.Carriage:
default:
return unit.isLoaded()
? TrainTypeSprite.LoadedCarriage
: TrainTypeSprite.Carriage;
}
}
const getSpriteForUnit = (unit: UnitView): ImageBitmap | null => {
+4 -3
View File
@@ -14,8 +14,8 @@ import { TrainStation } from "../game/TrainStation";
export class TrainExecution implements Execution {
private active = true;
private mg: Game | null = null;
private train: Unit | null = null;
private cars: Unit[] = [];
private train: Unit | null = null; // primary unit
private cars: Unit[] = []; // stored back to front
private hasCargo: boolean = false;
private currentTile: number = 0;
private spacing = 2;
@@ -69,6 +69,7 @@ export class TrainExecution implements Execution {
if (this.train === null) {
throw new Error("Not initialized");
}
if (!this.train.isActive() || !this.activeSourceOrDestination()) {
this.deleteTrain();
return;
@@ -113,7 +114,7 @@ export class TrainExecution implements Execution {
this.cars.push(
this.player.buildUnit(UnitType.Train, tile, {
targetUnit: this.destination.unit,
trainType: TrainType.Engine,
trainType: TrainType.TailEngine,
}),
);
for (let i = 0; i < this.numCars; i++) {
+1
View File
@@ -217,6 +217,7 @@ export enum UnitType {
export enum TrainType {
Engine = "Engine",
TailEngine = "TailEngine",
Carriage = "Carriage",
}
+20 -6
View File
@@ -265,13 +265,11 @@ export class UnitImpl implements Unit {
this._active = false;
this.mg.addUpdate(this.toUpdate());
this.mg.removeUnit(this);
if (displayMessage !== false && this._type !== UnitType.MIRVWarhead) {
this.mg.displayMessage(
`Your ${this._type} was destroyed`,
MessageType.UNIT_DESTROYED,
this.owner().id(),
);
if (displayMessage !== false) {
this.displayMessageOnDeleted();
}
if (destroyer !== undefined) {
switch (this._type) {
case UnitType.TransportShip:
@@ -296,6 +294,22 @@ export class UnitImpl implements Unit {
}
}
private displayMessageOnDeleted(): void {
if (this._type === UnitType.MIRVWarhead) {
return;
}
if (this._type === UnitType.Train && this._trainType !== TrainType.Engine) {
return;
}
this.mg.displayMessage(
`Your ${this._type} was destroyed`,
MessageType.UNIT_DESTROYED,
this.owner().id(),
);
}
isActive(): boolean {
return this._active;
}