1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
}
}
|