From d3693a5a4bc9f5c382e523eee5a9c13c7ab6fc62 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 13 Mar 2022 15:42:26 +0300 Subject: refactor: separate calculateNewTilePosition method --- src/Board/Board.ts | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Board/Board.ts b/src/Board/Board.ts index 5f38088..b9a2136 100644 --- a/src/Board/Board.ts +++ b/src/Board/Board.ts @@ -88,8 +88,8 @@ export default class Board { })); } - isLegalAttachment(attachment: Attachment) { - const { tile, side, attachTo, orientation } = attachment; + calculateNewTilePosition(attachment: Attachment): TileOnBoard['position'] { + const { attachTo: { position }, side } = attachment const xIncrement = { [East]: 1, @@ -101,10 +101,16 @@ export default class Board { [South]: -1, }; - const position = { - x: attachTo.position.x + (xIncrement[side] || 0), - y: attachTo.position.y + (yIncrement[side] || 0), + return { + x: position.x + (xIncrement[side] || 0), + y: position.y + (yIncrement[side] || 0), }; + } + + isLegalAttachment(attachment: Attachment) { + const { tile, side, orientation } = attachment; + + const position = this.calculateNewTilePosition(attachment); const isBusy = _.find(this.tiles, { position }); if (isBusy) return false; @@ -119,26 +125,15 @@ export default class Board { } attach(attachment: Attachment) { - const { tile, attachTo, side, orientation } = attachment; - - const xIncrement = { - [East]: 1, - [West]: -1, - }; + const { tile, orientation } = attachment; - const yIncrement = { - [North]: 1, - [South]: -1, - }; + const position = this.calculateNewTilePosition(attachment); const tileOnBoard = new TileOnBoard( tile.center, tile.sides, tile.shield, - { - x: attachTo.position.x + (xIncrement[side] || 0), - y: attachTo.position.y + (yIncrement[side] || 0), - }, + position, orientation, ); -- cgit v1.2.3