dao-game / peerjs-browser-bug / files / src / types.ts
Branch peerjs-browser-bug · default branch · all branches
types.ts
ed1a45fd 2022-03-05 Pavel Safronov | 1 | export class Coord { |
47dfe48a 2022-03-20 Pavel Safronov | 2 | public row: number; |
| 3 | public col: number; | |
ed1a45fd 2022-03-05 Pavel Safronov | 4 | |
47dfe48a 2022-03-20 Pavel Safronov | 5 | constructor(row: number, col: number) { |
| 6 | this.row = row; | |
| 7 | this.col = col; | |
| 8 | } | |
ed1a45fd 2022-03-05 Pavel Safronov | 9 | |
47dfe48a 2022-03-20 Pavel Safronov | 10 | public add(coord: Coord): Coord { |
| 11 | return new Coord(this.row + coord.row, this.col + coord.col); | |
| 12 | } | |
ed1a45fd 2022-03-05 Pavel Safronov | 13 | |
47dfe48a 2022-03-20 Pavel Safronov | 14 | sub(coord: Coord): Coord { |
| 15 | return new Coord(this.row - coord.row, this.col - coord.col); | |
| 16 | } | |
ed1a45fd 2022-03-05 Pavel Safronov | 17 | |
47dfe48a 2022-03-20 Pavel Safronov | 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 | } | |
4918da41 2022-03-17 Pavel Safronov | 28 | |
47dfe48a 2022-03-20 Pavel Safronov | 29 | equals(otherCoord: Coord): boolean { |
| 30 | const areEqual = this.col === otherCoord.col && this.row === otherCoord.row; | |
| 31 | return areEqual; | |
| 32 | } | |
ed1a45fd 2022-03-05 Pavel Safronov | 33 | |
47dfe48a 2022-03-20 Pavel Safronov | 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 | } | |
ed1a45fd 2022-03-05 Pavel Safronov | 48 | } |
5648264d 2022-03-22 Pavel Safronov | 49 | export interface Move { |
| 50 | source: Coord; | |
| 51 | dest: Coord; | |
| 52 | playerId: string; | |
ed1a45fd 2022-03-05 Pavel Safronov | 53 | } |
| 54 | ||
2909d324 2022-03-16 Pavel Safronov | 55 | export interface MoveResult { |
47dfe48a 2022-03-20 Pavel Safronov | 56 | isTerminal: boolean; |
| 57 | message?: string; | |
ed1a45fd 2022-03-05 Pavel Safronov | 58 | } |