diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-03-13 01:26:21 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-03-13 01:26:21 +0300 |
commit | 93379c8826034601bbaad017366ddd2e484574e3 (patch) | |
tree | 927e3ea802dea68c47a7ee49acf0943a64dcef25 | |
parent | edb54e7cc773d1b9485171e37f59296de52f2031 (diff) | |
download | carcassonne-engine-ts-93379c8826034601bbaad017366ddd2e484574e3.tar.gz |
feat: add position calculation on attach
-rw-r--r-- | src/Board/Board.test.ts | 28 | ||||
-rw-r--r-- | src/Board/Board.ts | 33 | ||||
-rw-r--r-- | src/Tile/TileOnBoard.ts | 11 |
3 files changed, 59 insertions, 13 deletions
diff --git a/src/Board/Board.test.ts b/src/Board/Board.test.ts index 7e99581..349a4aa 100644 --- a/src/Board/Board.test.ts +++ b/src/Board/Board.test.ts @@ -1,9 +1,10 @@ import assert from 'assert'; -import Tile, { Feature } from '../Tile/Tile'; +import Tile, { Feature, Direction } from '../Tile/Tile'; import TileOnBoard from '../Tile/TileOnBoard'; import Board from './Board'; const { Road, Town, Grass } = Feature; +const { North } = Direction; describe('Board', () => { describe('constructor', () => { @@ -28,4 +29,29 @@ describe('Board', () => { assert.deepStrictEqual(legalMoves[3], { side: 2, rotation: 2, attachTo, tile }); }); }); + + describe('attach', () => { + it('Should push new tile to the list and assign correct coordinates', () => { + const board = new Board(); + + const attachment = { + tile: new Tile(Town, [Grass, Town, Grass, Town]), + attachTo: board.tiles[0], + orientation: 0, + side: North, + }; + + const expectedTileOnBoard = new TileOnBoard( + Town, + [Grass, Town, Grass, Town], + false, + { x: 0, y: 1 }, + 0 + ); + + board.attach(attachment); + assert.strictEqual(board.tiles.length, 2); + assert.deepStrictEqual(board.tiles[1], expectedTileOnBoard); + }); + }); }); 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); diff --git a/src/Tile/TileOnBoard.ts b/src/Tile/TileOnBoard.ts index 374ae95..bc1e66d 100644 --- a/src/Tile/TileOnBoard.ts +++ b/src/Tile/TileOnBoard.ts @@ -2,14 +2,13 @@ import _ from 'lodash'; import Tile, { Feature, Direction } from './Tile'; export interface Attachment { - attachTo: Tile; + attachTo: TileOnBoard; side: Direction; tile: Tile; - rotation: number; // Clockwise rotation of a tile + orientation: number; // Clockwise rotation of a tile } export default class TileOnBoard extends Tile { - neighbors: [Tile, Tile, Tile, Tile]; orientation: number; // amount of 90-degree counter-clockwise rotations from original orientation position: { x: number; @@ -26,7 +25,6 @@ export default class TileOnBoard extends Tile { super(center, sides, shield); this.position = position; this.orientation = orientation; - this.neighbors = [null, null, null, null]; } getSide(direction: Direction) { @@ -52,9 +50,4 @@ export default class TileOnBoard extends Tile { })) })); } - - attach(tile: Tile, side: Direction) { - if (this.neighbors[side]) throw new Error('There is something already attached to this side!'); - this.neighbors[side] = tile; - } } |