diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-03-14 20:52:08 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-03-14 20:52:08 +0300 |
commit | d64c44c432ccb5574909edd77b8b882e8543ef1b (patch) | |
tree | feb4035bf7301f9b627634695b7650027e5eecc4 /src/Tile/Tile.ts | |
parent | 9cf681ff497150e4d56b13bde4f57f4f0e9633c0 (diff) | |
download | carcassonne-engine-ts-d64c44c432ccb5574909edd77b8b882e8543ef1b.tar.gz |
refactor!: change internal edge representation
Diffstat (limited to 'src/Tile/Tile.ts')
-rw-r--r-- | src/Tile/Tile.ts | 68 |
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, + })); + })); } } |