fuzzyworld

dao-game / peerjs-browser-bug / files / src / types.ts

Branch peerjs-browser-bug · default branch · all branches

types.ts

Blame: the commit that last changed each line · file · history

ed1a45fd 2022-03-05 Pavel Safronov1export class Coord {
47dfe48a 2022-03-20 Pavel Safronov2 public row: number;
3 public col: number;
ed1a45fd 2022-03-05 Pavel Safronov4
47dfe48a 2022-03-20 Pavel Safronov5 constructor(row: number, col: number) {
6 this.row = row;
7 this.col = col;
8 }
ed1a45fd 2022-03-05 Pavel Safronov9
47dfe48a 2022-03-20 Pavel Safronov10 public add(coord: Coord): Coord {
11 return new Coord(this.row + coord.row, this.col + coord.col);
12 }
ed1a45fd 2022-03-05 Pavel Safronov13
47dfe48a 2022-03-20 Pavel Safronov14 sub(coord: Coord): Coord {
15 return new Coord(this.row - coord.row, this.col - coord.col);
16 }
ed1a45fd 2022-03-05 Pavel Safronov17
47dfe48a 2022-03-20 Pavel Safronov18 /**
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 Safronov28
47dfe48a 2022-03-20 Pavel Safronov29 equals(otherCoord: Coord): boolean {
30 const areEqual = this.col === otherCoord.col && this.row === otherCoord.row;
31 return areEqual;
32 }
ed1a45fd 2022-03-05 Pavel Safronov33
47dfe48a 2022-03-20 Pavel Safronov34 // 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 Safronov48}
5648264d 2022-03-22 Pavel Safronov49export interface Move {
50 source: Coord;
51 dest: Coord;
52 playerId: string;
ed1a45fd 2022-03-05 Pavel Safronov53}
54
2909d324 2022-03-16 Pavel Safronov55export interface MoveResult {
47dfe48a 2022-03-20 Pavel Safronov56 isTerminal: boolean;
57 message?: string;
ed1a45fd 2022-03-05 Pavel Safronov58}