From 93379c8826034601bbaad017366ddd2e484574e3 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 13 Mar 2022 01:26:21 +0300 Subject: feat: add position calculation on attach --- src/Board/Board.ts | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/Board/Board.ts') diff --git a/src/Board/Board.ts b/src/Board/Board.ts index 2c4a215..4ba7a4c 100644 --- a/src/Board/Board.ts +++ b/src/Board/Board.ts @@ -1,8 +1,9 @@ import _ from 'lodash'; -import Tile, { Feature } from '../Tile/Tile'; -import TileOnBoard from '../Tile/TileOnBoard'; +import Tile, { Feature, Direction } from '../Tile/Tile'; +import TileOnBoard, { Attachment } from '../Tile/TileOnBoard'; const { Road, Town, Grass } = Feature; +const { North, East, South, West } = Direction; export default class Board { tiles: TileOnBoard[]; @@ -16,10 +17,36 @@ export default class Board { this.tiles.forEach(tile => tile.print()); } - getAttachments(tile: Tile) { + getAttachments(tile: Tile): Attachment[] { return _.flatten(this.tiles.map(attachTo => attachTo.getAttachments(tile))); } + attach(attachment: Attachment) { + const { tile, attachTo, side } = attachment; + + const xIncrement = { + [East]: 1, + [West]: -1, + }; + + const yIncrement = { + [North]: 1, + [South]: -1, + }; + + const tileOnBoard = new TileOnBoard( + tile.center, + tile.sides, + tile.shield, + { + x: attachTo.position.x + (xIncrement[side] || 0), + y: attachTo.position.y + (yIncrement[side] || 0), + }, + ); + + this.tiles.push(tileOnBoard); + } + getLegalMoves(tile: Tile) { const attachments = this.getAttachments(tile); console.log(attachments); -- cgit v1.2.3