aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-13 15:42:26 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-13 15:42:26 +0300
commitd3693a5a4bc9f5c382e523eee5a9c13c7ab6fc62 (patch)
tree416488eedca9a8f747c4554495c913ed6c7508e1
parent1c9b62aa3b5bbfdc0fde7a2b343066fda66441b3 (diff)
downloadcarcassonne-engine-ts-d3693a5a4bc9f5c382e523eee5a9c13c7ab6fc62.tar.gz
refactor: separate calculateNewTilePosition method
-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,
);