aboutsummaryrefslogtreecommitdiff
path: root/src/Tile/TileOnBoard.ts
blob: 0fa8005155e03fc566e4d7330a2998f4625f2959 (plain)
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
47
48
49
50
51
import _ from 'lodash';
import Tile, { Feature, Direction } from './Tile';

export interface Attachment {
  attachTo: TileOnBoard;
  side: Direction;
  tile: Tile;
  orientation: number; // Clockwise rotation of a tile
}

export default class TileOnBoard extends Tile {
  orientation: number; // amount of 90-degree clockwise rotations from original orientation
  position: {
    x: number;
    y: number;
  }

  constructor(
    center: Feature,
    sides: [Feature, Feature, Feature, Feature],
    shield = false,
    position = { x: 0, y: 0 },
    orientation = 0
  ) {
    super(center, sides, shield);
    this.position = position;
    this.orientation = orientation;
  }

  getSide(direction: Direction) {
    return super.getSide(direction - this.orientation);
  }

  rotate(rotation = 1) {
    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(orientation => tile.getSide(side - orientation + 2) === item)
        .map(orientation => ({
          tile,
          orientation,
          side,
          attachTo: this as Tile
        }))
    }));
  }
}