TrainsAndRoads/interface/types/Cell.ts

39 lines
1.2 KiB
TypeScript

import { CellType } from "../constants/CellType";
import { Direction } from "../constants/Direction";
import { PieceId, pieceMap } from "../constants/Pieces";
import { ExternalNode } from "./ExternalNode";
import { PlacedPiece } from "./PlacedPiece";
export class Cell {
public readonly externalNodes: Map<Direction, ExternalNode>;
public readonly cellType: CellType;
public placedPiece?: {
piece: PlacedPiece;
id: PieceId;
};
constructor(cellType: CellType) {
this.externalNodes = new Map([
[Direction.NORTH, new ExternalNode(this, Direction.NORTH)],
[Direction.SOUTH, new ExternalNode(this, Direction.SOUTH)],
[Direction.EAST, new ExternalNode(this, Direction.EAST)],
[Direction.WEST, new ExternalNode(this, Direction.WEST)],
]);
this.cellType = cellType;
}
public getNodeAt(direction: Direction): ExternalNode {
const node = this.externalNodes.get(direction);
if (!node) throw Error(`Could not find node at ${direction}`);
return node!;
}
public placePiece(pieceId: PieceId) {
if (this.placedPiece !== undefined) return;
this.placedPiece = {
piece: pieceMap[pieceId].toPlacedPiece(this),
id: pieceId,
};
}
}