aboutsummaryrefslogtreecommitdiff
path: root/src/Tile/Tile.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/Tile/Tile.ts')
-rw-r--r--src/Tile/Tile.ts68
1 files changed, 50 insertions, 18 deletions
diff --git a/src/Tile/Tile.ts b/src/Tile/Tile.ts
index 5bc6a7a..2e6f2ba 100644
--- a/src/Tile/Tile.ts
+++ b/src/Tile/Tile.ts
@@ -1,40 +1,72 @@
+import _ from 'lodash';
+
export enum Direction {
North,
East,
South,
West
}
-
-export enum Feature {
- Grass = 'G',
- Road = 'R',
- Town = 'T',
- River = 'I',
- Church = 'C',
+export interface Attachment {
+ attachTo: Tile;
+ edge: Direction;
+ tile: Tile;
+ orientation: number;
}
-
-// Abstract Tile data
export default class Tile {
- center: Feature;
- sides: [Feature, Feature, Feature, Feature];
+ center: string;
+ private _edges: string;
shield?: boolean;
+ private _orientation: number; // Clockwise rotation of a tile
+ position: {
+ x: number;
+ y: number;
+ }
+
public constructor(
- center: Feature,
- sides: [Feature, Feature, Feature, Feature],
- shield = false
+ center: string,
+ edges: string,
+ shield = false,
+ position = { x: 0, y: 0 },
+ orientation = 0
) {
this.center = center;
- this.sides = sides;
+ this._edges = edges;
this.shield = shield;
+ this.position = position;
+ this.orientation = orientation;
+ }
+
+ public set orientation(orientation) {
+ this._orientation = ((orientation % 4) + 4) % 4;
+ }
+
+ public get orientation() {
+ return this._orientation;
}
- getSide(direction: Direction) {
- return this.sides[((direction % 4) + 4) % 4];
+ public get edges() {
+ return _.repeat(this._edges, 2).slice(4 - this.orientation, 8 - this.orientation);
}
print() {
- console.log( ` ${this.getSide(Direction.North)} \n${this.getSide(Direction.West)}${this.center}${this.getSide(Direction.East)}\n ${this.getSide(Direction.South)} `);
+ console.log( ` ${this.edges[Direction.North]} \n${this.edges[Direction.West]}${this.center}${this.edges[Direction.East]}\n ${this.edges[Direction.South]} `);
+ }
+
+ getAttachments(tile: Tile): Attachment[] {
+ return _.flatten(_.map(this.edges, (feature, edge) => {
+ const oppositeEdge = (edge + 2) % 4;
+
+ return _
+ .map(tile.edges, (candidate, index) => candidate === feature ? index : -1)
+ .filter(index => index !== -1)
+ .map(index => ({
+ edge,
+ orientation: (((oppositeEdge - index) % 4) + 4) % 4,
+ attachTo: this,
+ tile,
+ }));
+ }));
}
}