Created piece and placed piece abstractions
parent
163ca86734
commit
7354586ef5
|
|
@ -2,14 +2,14 @@ import { Direction } from "../constants/Direction";
|
||||||
import { Border } from "./Border";
|
import { Border } from "./Border";
|
||||||
import { Cell } from "./Cell";
|
import { Cell } from "./Cell";
|
||||||
import { Exit } from "./Exit";
|
import { Exit } from "./Exit";
|
||||||
import { Node } from "./Node";
|
|
||||||
|
|
||||||
export class ExternalNode extends Node {
|
export class ExternalNode {
|
||||||
public readonly direction: Direction;
|
public readonly direction: Direction;
|
||||||
private border?: Border;
|
private border?: Border;
|
||||||
|
public readonly cell: Cell;
|
||||||
|
|
||||||
constructor(cell: Cell, direction: Direction) {
|
constructor(cell: Cell, direction: Direction) {
|
||||||
super(cell);
|
this.cell = cell;
|
||||||
this.direction = direction;
|
this.direction = direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { randomUUID } from "crypto";
|
||||||
|
|
||||||
|
export class InternalNode {
|
||||||
|
id: string;
|
||||||
|
type: string;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.id = randomUUID();
|
||||||
|
this.type = "STATION";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
import { Cell } from "./Cell";
|
import { ExternalNode } from "./ExternalNode";
|
||||||
|
import { InternalNode } from "./InternalNode";
|
||||||
|
|
||||||
export abstract class Node {
|
export type Node = ExternalNode | InternalNode;
|
||||||
public readonly cell: Cell;
|
|
||||||
|
|
||||||
constructor(cell: Cell) {
|
|
||||||
this.cell = cell;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
import { Direction } from "../constants/Direction";
|
||||||
|
import { TrackType } from "../constants/TrackType";
|
||||||
|
import { Cell } from "./Cell";
|
||||||
|
import { InternalNode } from "./InternalNode";
|
||||||
|
import { PlacedPiece } from "./PlacedPiece";
|
||||||
|
import { Node } from "./Node";
|
||||||
|
|
||||||
|
export interface TrackProps {
|
||||||
|
startPoint: Direction;
|
||||||
|
endPoint?: Direction;
|
||||||
|
isInternal: boolean;
|
||||||
|
type: TrackType;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Piece {
|
||||||
|
tracks: Set<{
|
||||||
|
joinedPoints: {
|
||||||
|
firstPoint: Direction;
|
||||||
|
secondPoint: Direction | InternalNode;
|
||||||
|
};
|
||||||
|
type: TrackType;
|
||||||
|
}>;
|
||||||
|
internalNodes: Set<InternalNode>;
|
||||||
|
|
||||||
|
constructor(hasInternalNode: boolean, trackDefinitions: TrackProps[]) {
|
||||||
|
const internalNode = new InternalNode();
|
||||||
|
this.internalNodes = new Set();
|
||||||
|
if (hasInternalNode) {
|
||||||
|
this.internalNodes.add(internalNode);
|
||||||
|
}
|
||||||
|
this.tracks = new Set(
|
||||||
|
trackDefinitions.map((track) => {
|
||||||
|
if (!track.isInternal && !track.endPoint) {
|
||||||
|
throw Error("Missing direction for non-internal track");
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
joinedPoints: {
|
||||||
|
firstPoint: track.startPoint,
|
||||||
|
secondPoint: track.isInternal
|
||||||
|
? internalNode
|
||||||
|
: (track.endPoint as Direction),
|
||||||
|
},
|
||||||
|
type: track.type,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toPlacedPiece(cell: Cell) {
|
||||||
|
return new PlacedPiece(
|
||||||
|
new Set(
|
||||||
|
Array.from(this.tracks).map((track) => {
|
||||||
|
return {
|
||||||
|
nodes: {
|
||||||
|
firstNode: cell.getNodeAt(track.joinedPoints.firstPoint),
|
||||||
|
secondNode:
|
||||||
|
track.joinedPoints.secondPoint instanceof Node
|
||||||
|
? track.joinedPoints.secondPoint as Node
|
||||||
|
: cell.getNodeAt(track.joinedPoints.secondPoint as Direction),
|
||||||
|
},
|
||||||
|
type: track.type,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
this.internalNodes,
|
||||||
|
cell,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { TrackType } from "../constants/TrackType";
|
||||||
|
import { Cell } from "./Cell";
|
||||||
|
import { InternalNode } from "./InternalNode";
|
||||||
|
import { Node } from "./Node";
|
||||||
|
|
||||||
|
export class PlacedPiece {
|
||||||
|
tracks: Set<{
|
||||||
|
nodes: {
|
||||||
|
firstNode: Node;
|
||||||
|
secondNode: Node;
|
||||||
|
};
|
||||||
|
type: TrackType;
|
||||||
|
}>;
|
||||||
|
internalNodes: Set<InternalNode>;
|
||||||
|
cell: Cell;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
tracks: Set<{
|
||||||
|
nodes: { firstNode: Node; secondNode: Node };
|
||||||
|
type: TrackType;
|
||||||
|
}>,
|
||||||
|
internalNodes: Set<InternalNode>,
|
||||||
|
cell: Cell,
|
||||||
|
) {
|
||||||
|
this.tracks = tracks;
|
||||||
|
this.internalNodes = internalNodes;
|
||||||
|
this.cell = cell;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue