aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Board/Board.ts33
1 files 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,
);