All files BoardPosition.ts

87.5% Statements 7/8
87.5% Branches 7/8
83.33% Functions 5/6
87.5% Lines 7/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        1x           1x   867101x       240x                 838977x       22260x               26x      
/**
 * @author Timo Lehnertz
 */
 
import { Vec2 } from "./Vec2";
 
/**
 * Immutable BoardPosition class representing a location on the board
 * (0|0) is in the top left corner (Northwest)
 */
export class BoardPosition extends Vec2 {
  public isInBounds(width: number, height: number): boolean {
    return this.x >= 0 && this.y >= 0 && this.x < width && this.y < height;
  }
 
  public isEdge(width: number, height: number): boolean {
    return (
      this.x === 0 ||
      this.y === 0 ||
      this.x === width - 1 ||
      this.y === height - 1
    );
  }
 
  public add(b: Vec2): BoardPosition {
    return new BoardPosition(this.x + b.x, this.y + b.y);
  }
 
  public static create(instance: BoardPosition): BoardPosition {
    return new BoardPosition(instance.x, instance.y);
  }
 
  public setX(x: number): BoardPosition {
    return new BoardPosition(x, this.y);
  }
 
  public setY(y: number): BoardPosition {
    return new BoardPosition(this.x, y);
  }
}