diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-03-12 15:28:19 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-03-12 15:28:19 +0300 |
commit | 77b2f09883d4144c01802c5c75622e28f3864ca0 (patch) | |
tree | 5f6297e83295de990fd916b55e76af27e5a86a73 /src/Tile/Tile.ts | |
parent | 2e6a4a8761bf037283f7eb8dbfd57ab7b9e977a9 (diff) | |
download | carcassonne-engine-ts-77b2f09883d4144c01802c5c75622e28f3864ca0.tar.gz |
refactor: separate TileOnBoard class
Diffstat (limited to 'src/Tile/Tile.ts')
-rw-r--r-- | src/Tile/Tile.ts | 62 |
1 files changed, 11 insertions, 51 deletions
diff --git a/src/Tile/Tile.ts b/src/Tile/Tile.ts index 4eee263..4dd6ecb 100644 --- a/src/Tile/Tile.ts +++ b/src/Tile/Tile.ts @@ -1,8 +1,3 @@ -import _ from 'lodash'; -import Debug, { Debugger } from 'debug'; - -const debug = Debug('cell'); - export enum Direction { North, East, @@ -11,66 +6,31 @@ export enum Direction { } export enum Feature { - Empty = " ", - Road = "R", - Town = "T", - River = "I", - Church = "C", -} - -export interface Attachment { - attachTo: Tile; - side: Direction; - cell: Tile; - rotation: number; // Clockwise rotation of a cell + Empty = ' ', + Road = 'R', + Town = 'T', + River = 'I', + Church = 'C', } +// Abstract Tile data export default class Tile { center: Feature; - private sides: [Feature, Feature, Feature, Feature]; - neighbors: [Tile, Tile, Tile, Tile]; - private orientation: number // amount of 90-degree counter-clockwise rotations from original orientation + sides: [Feature, Feature, Feature, Feature]; shield?: boolean; - debug: Debugger; - - public constructor(center: Feature, sides: [Feature, Feature, Feature, Feature], orientation = 0, shield = false) { + public constructor(center: Feature, sides: [Feature, Feature, Feature, Feature], shield = false) { this.center = center; this.sides = sides; this.shield = shield; - this.orientation = orientation; - } - - print() { - console.log( ` ${this.getSide(Direction.North)} \n${this.getSide(Direction.West)}${this.center}${this.getSide(Direction.East)}\n ${this.getSide(Direction.South)} `); } getSide(direction: Direction) { - return this.sides[(((this.orientation + direction) % 4) + 4) % 4]; - } - - rotate(rotation = 1) { - debug(`Rotating ${rotation} clockwise`) - this.orientation = this.orientation - rotation; + return this.sides[((direction % 4) + 4) % 4]; } - getAttachments(cell: Tile) { - return _.flatten([0, 1, 2, 3].map(side => { - const item = this.getSide(side); - return [0, 1, 2, 3] - .filter(rotation => cell.getSide(side - rotation + 2) === item) - .map(rotation => ({ - cell, - rotation, - side, - attachTo: this as Tile - })) - })); - } - - attach(cell: Tile, side: Direction) { - if (this.neighbors[side]) throw new Error('There is something already attached to this side!'); - this.neighbors[side] = cell; + print() { + console.log( ` ${this.getSide(Direction.North)} \n${this.getSide(Direction.West)}${this.center}${this.getSide(Direction.East)}\n ${this.getSide(Direction.South)} `); } } |