diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-03-13 13:57:40 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-03-13 13:57:40 +0300 |
commit | 1d6519f9f97e1d6d4046e2caf21b81dfe277992d (patch) | |
tree | 2cf88899cd87efd4045eb0dbbd00c00b669de9f9 | |
parent | f74f6478a16564cf48b55ae2968fe44740eb92ab (diff) | |
download | carcassonne-engine-ts-1d6519f9f97e1d6d4046e2caf21b81dfe277992d.tar.gz |
feat: only use clockwise turns for orientation
-rw-r--r-- | src/Tile/TileOnBoard.test.ts | 11 | ||||
-rw-r--r-- | src/Tile/TileOnBoard.ts | 14 |
2 files changed, 10 insertions, 15 deletions
diff --git a/src/Tile/TileOnBoard.test.ts b/src/Tile/TileOnBoard.test.ts index 70fc5a1..de2685d 100644 --- a/src/Tile/TileOnBoard.test.ts +++ b/src/Tile/TileOnBoard.test.ts @@ -42,15 +42,12 @@ describe('TileOnBoard', () => { const attachTo = new TileOnBoard(Town, [Road, Town, Town, Road]) const tile = new TileOnBoard(Road, [Grass, Road, Road, Grass]) - tile.print(); - attachTo.print(); - const attachments = attachTo.getAttachments(tile); assert.strictEqual(attachments.length, 4); - assert.deepStrictEqual(attachments[0], { side: 0, rotation: 0, tile, attachTo }); - assert.deepStrictEqual(attachments[1], { side: 0, rotation: 1, tile, attachTo }); - assert.deepStrictEqual(attachments[2], { side: 3, rotation: 0, tile, attachTo }); - assert.deepStrictEqual(attachments[3], { side: 3, rotation: 3, tile, attachTo }); + assert.deepStrictEqual(attachments[0], { side: 0, orientation: 0, tile, attachTo }); + assert.deepStrictEqual(attachments[1], { side: 0, orientation: 1, tile, attachTo }); + assert.deepStrictEqual(attachments[2], { side: 3, orientation: 0, tile, attachTo }); + assert.deepStrictEqual(attachments[3], { side: 3, orientation: 3, tile, attachTo }); }); }); }); diff --git a/src/Tile/TileOnBoard.ts b/src/Tile/TileOnBoard.ts index bc1e66d..0fa8005 100644 --- a/src/Tile/TileOnBoard.ts +++ b/src/Tile/TileOnBoard.ts @@ -9,7 +9,7 @@ export interface Attachment { } export default class TileOnBoard extends Tile { - orientation: number; // amount of 90-degree counter-clockwise rotations from original orientation + orientation: number; // amount of 90-degree clockwise rotations from original orientation position: { x: number; y: number; @@ -28,23 +28,21 @@ export default class TileOnBoard extends Tile { } getSide(direction: Direction) { - return super.getSide(direction + this.orientation); + return super.getSide(direction - this.orientation); } rotate(rotation = 1) { - // We want to rotate clockwise, but orientation stores counter-clockwise rotations, - // therefore use the difference, not sum. - this.orientation = this.orientation - rotation; + this.orientation += rotation; } getAttachments(tile: Tile): Attachment[] { return _.flatten([0, 1, 2, 3].map(side => { const item = this.getSide(side); return [0, 1, 2, 3] - .filter(rotation => tile.getSide(side - rotation + 2) === item) - .map(rotation => ({ + .filter(orientation => tile.getSide(side - orientation + 2) === item) + .map(orientation => ({ tile, - rotation, + orientation, side, attachTo: this as Tile })) |