Files
OpenFrontIO/src/core/game/Railroad.ts
T
Scott Anderson b56e380107 Enable the sort-keys eslint rule (#1746)
## Description:

Enable the `sort-keys` eslint rule.

Fixes #1629

## 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
- [ ] I have read and accepted the CLA agreement (only required once).
2025-08-07 19:13:42 -04:00

68 lines
1.6 KiB
TypeScript

import { Game } from "./Game";
import { TileRef } from "./GameMap";
import { GameUpdateType, RailTile, RailType } from "./GameUpdates";
import { TrainStation } from "./TrainStation";
export class Railroad {
constructor(
public from: TrainStation,
public to: TrainStation,
public tiles: TileRef[],
) {}
delete(game: Game) {
const railTiles: RailTile[] = this.tiles.map((tile) => ({
railType: RailType.VERTICAL,
tile,
}));
game.addUpdate({
isActive: false,
railTiles,
type: GameUpdateType.RailroadEvent,
});
this.from.getRailroads().delete(this);
this.to.getRailroads().delete(this);
}
}
export function getOrientedRailroad(
from: TrainStation,
to: TrainStation,
): OrientedRailroad | null {
for (const railroad of from.getRailroads()) {
if (railroad.from === to) {
return new OrientedRailroad(railroad, false);
} else if (railroad.to === to) {
return new OrientedRailroad(railroad, true);
}
}
return null;
}
/**
* Wrap a railroad with a direction so it always starts at tiles[0]
*/
export class OrientedRailroad {
private tiles: TileRef[] = [];
constructor(
private railroad: Railroad,
private forward: boolean,
) {
this.tiles = this.forward
? this.railroad.tiles
: [...this.railroad.tiles].reverse();
}
getTiles(): TileRef[] {
return this.tiles;
}
getStart(): TrainStation {
return this.forward ? this.railroad.from : this.railroad.to;
}
getEnd(): TrainStation {
return this.forward ? this.railroad.to : this.railroad.from;
}
}