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 counter-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) { // We want to rotate clockwise, but orientation stores counter-clockwise rotations, // therefore use the difference, not sum. 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 })) })); } }