diff options
Diffstat (limited to 'src/Board/Board.ts')
-rw-r--r-- | src/Board/Board.ts | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/src/Board/Board.ts b/src/Board/Board.ts index ef4b4a5..b114ff1 100644 --- a/src/Board/Board.ts +++ b/src/Board/Board.ts @@ -57,7 +57,58 @@ export default class Board { } getAttachments(tile: Tile): Attachment[] { - return _.flatten(this.tiles.map(attachTo => attachTo.getAttachments(tile))); + return _ + .flatten(this.tiles.map(attachTo => attachTo.getAttachments(tile))) + .filter((attachment: Attachment) => this.isLegalAttachment(attachment)); + } + + previewAttachment(attachment: Attachment) { + console.log(attachment); + const previewBoard = _.cloneDeep(this); + previewBoard.attach(attachment); + previewBoard.print(); + } + + isLegalAttachment(attachment: Attachment) { + const { tile, side, attachTo, orientation } = attachment; + + const xIncrement = { + [East]: 1, + [West]: -1, + }; + + const yIncrement = { + [North]: 1, + [South]: -1, + }; + + const position = { + x: attachTo.position.x + (xIncrement[side] || 0), + y: attachTo.position.y + (yIncrement[side] || 0), + }; + + const isBusy = _.find(this.tiles, { position }); + if (isBusy) return false; + + // TODO: compute this array at runtime + const neighborIncrements = [ + { x: 0, y: -1, side: 0 }, + { x: -1, y: 0, side: 1 }, + { x: 0, y: 1, side: 2 }, + { x: 1, y: 0, side: 3 }, + ]; + + return _.every(_.map(neighborIncrements, ({ x, y, side }) => { + const neighbor = _.find(this.tiles, { + position: { + x: position.x + x, + y: position.y + y, + } + }); + if (!neighbor) return true; + if (neighbor.getSide(side) !== tile.getSide(side - orientation + 2)) return false; + return true; + })); } attach(attachment: Attachment) { @@ -86,11 +137,4 @@ export default class Board { this.tiles.push(tileOnBoard); } - - previewAttachment(attachment: Attachment) { - console.log(attachment); - const previewBoard = _.cloneDeep(this); - previewBoard.attach(attachment); - previewBoard.print(); - } } |