aboutsummaryrefslogtreecommitdiff
path: root/src/Tile/TileOnBoard.ts
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-12 15:28:19 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-12 15:28:19 +0300
commit77b2f09883d4144c01802c5c75622e28f3864ca0 (patch)
tree5f6297e83295de990fd916b55e76af27e5a86a73 /src/Tile/TileOnBoard.ts
parent2e6a4a8761bf037283f7eb8dbfd57ab7b9e977a9 (diff)
downloadcarcassonne-engine-ts-77b2f09883d4144c01802c5c75622e28f3864ca0.tar.gz
refactor: separate TileOnBoard class
Diffstat (limited to 'src/Tile/TileOnBoard.ts')
-rw-r--r--src/Tile/TileOnBoard.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Tile/TileOnBoard.ts b/src/Tile/TileOnBoard.ts
new file mode 100644
index 0000000..65f3731
--- /dev/null
+++ b/src/Tile/TileOnBoard.ts
@@ -0,0 +1,46 @@
+import _ from 'lodash';
+import Tile, { Feature, Direction } from './Tile';
+
+export interface Attachment {
+ attachTo: Tile;
+ side: Direction;
+ tile: Tile;
+ rotation: number; // Clockwise rotation of a tile
+}
+
+export default class TileOnBoard extends Tile {
+ neighbors: [Tile, Tile, Tile, Tile];
+ private orientation: number // amount of 90-degree counter-clockwise rotations from original orientation
+
+ constructor(center: Feature, sides: [Feature, Feature, Feature, Feature], shield = false, orientation = 0) {
+ super(center, sides, shield);
+ this.orientation = orientation;
+ }
+
+ getSide(direction: Direction) {
+ return this.sides[(((this.orientation + direction) % 4) + 4) % 4];
+ }
+
+ rotate(rotation = 1) {
+ this.orientation = 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 => ({
+ tile,
+ rotation,
+ side,
+ attachTo: this as 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;
+ }
+}