diff options
Diffstat (limited to 'src/Cell/Cell.ts')
-rw-r--r-- | src/Cell/Cell.ts | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/src/Cell/Cell.ts b/src/Cell/Cell.ts index 23fdc9e..70bb792 100644 --- a/src/Cell/Cell.ts +++ b/src/Cell/Cell.ts @@ -1,5 +1,8 @@ +import _ from 'lodash'; import Debug, { Debugger } from 'debug'; +const debug = Debug('cell'); + export enum Direction { North, East, @@ -15,9 +18,18 @@ export enum Item { Church = "C", } +export interface Attachment { + attachTo: Cell; + side: Direction; + cell: Cell; + rotation: number; // Clockwise rotation of a cell +} + + export default class Cell { center: Item; private sides: [Item, Item, Item, Item]; + neighbors: [Cell, Cell, Cell, Cell]; private orientation: number // amount of 90-degree counter-clockwise rotations from original orientation shield?: boolean; @@ -28,12 +40,6 @@ export default class Cell { this.sides = sides; this.shield = shield; this.orientation = orientation; - - this.debug = Debug('cell'); - } - - toString() { - return; } print() { @@ -45,11 +51,26 @@ export default class Cell { } rotate(rotation = 1) { - this.debug(`Rotating ${rotation} clockwise`) + debug(`Rotating ${rotation} clockwise`) this.orientation = this.orientation - rotation; } - isAttachable(cell: Cell, side: Direction) { - return (this.getSide(side) === cell.getSide(side + 2)); + getAttachments(cell: Cell) { + 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 Cell + })) + })); + } + + attach(cell: Cell, side: Direction) { + if (this.neighbors[side]) throw new Error('There is something already attached to this side!'); + this.neighbors[side] = cell; } } |