dao-game / peerjs-browser-bug / files / src / types.ts
Branch peerjs-browser-bug · default branch · all branches
types.ts
| 1 | export class Coord { |
| 2 | public row: number; |
| 3 | public col: number; |
| 4 | |
| 5 | constructor(row: number, col: number) { |
| 6 | this.row = row; |
| 7 | this.col = col; |
| 8 | } |
| 9 | |
| 10 | public add(coord: Coord): Coord { |
| 11 | return new Coord(this.row + coord.row, this.col + coord.col); |
| 12 | } |
| 13 | |
| 14 | sub(coord: Coord): Coord { |
| 15 | return new Coord(this.row - coord.row, this.col - coord.col); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Finds if otherCoord is contained within a rectangle that sits between origin and this Coord |
| 20 | * @param otherCoord |
| 21 | */ |
| 22 | contains(otherCoord: Coord): boolean { |
| 23 | const xRatio = otherCoord.col / this.col; |
| 24 | const yRatio = otherCoord.row / this.row; |
| 25 | const xyInsideSquare = xRatio >= 0 && xRatio < 1 && yRatio >= 0 && yRatio < 1; |
| 26 | return xyInsideSquare; |
| 27 | } |
| 28 | |
| 29 | equals(otherCoord: Coord): boolean { |
| 30 | const areEqual = this.col === otherCoord.col && this.row === otherCoord.row; |
| 31 | return areEqual; |
| 32 | } |
| 33 | |
| 34 | // clock face directions |
| 35 | static get allDirections(): Coord[] { |
| 36 | const allDirections = [ |
| 37 | new Coord(1, 0), // up |
| 38 | new Coord(1, 1), // up-right |
| 39 | new Coord(0, 1), // right |
| 40 | new Coord(-1, 1), // right-down |
| 41 | new Coord(-1, 0), // down |
| 42 | new Coord(-1, -1), // down-left |
| 43 | new Coord(0, -1), // left |
| 44 | new Coord(1, -1), // left-up |
| 45 | ]; |
| 46 | return allDirections; |
| 47 | } |
| 48 | } |
| 49 | export interface Move { |
| 50 | source: Coord; |
| 51 | dest: Coord; |
| 52 | playerId: string; |
| 53 | } |
| 54 | |
| 55 | export interface MoveResult { |
| 56 | isTerminal: boolean; |
| 57 | message?: string; |
| 58 | } |