All files / libs/games/src/maps map.ts

100% Statements 67/67
100% Branches 17/17
100% Functions 8/8
100% Lines 67/67

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 10x 10x 27x 27x 18x 18x 18x 14x 14x 18x 27x 27x 27x 3x 3x 27x 27x 10x 10x 27x 27x 10x 10x 27x 27x 23x 23x 22x 23x 1x 1x 22x 22x 23x 27x 27x 3x 2x 2x 3x 27x 27x 23x 23x 27x  
import { FloorTile, TileFactory, TileInterface } from "../tiles/index";
 
import { MapGeneratorInterface, MapParametersInterface, PositionInterface } from "./interfaces";
 
export class GameMap {
    private readonly generator?: MapGeneratorInterface;
 
    private readonly height: number;
 
    private readonly tiles: TileInterface[][];
 
    private readonly width: number;
 
    private readonly tileFactory: TileFactory;
 
    public constructor(parameters: MapParametersInterface) {
        this.width = parameters.width;
        this.height = parameters.height;
        this.tiles = [];
        this.tileFactory = parameters.tileFactory;
 
        if (parameters.tiles != undefined) {
            this.tiles = parameters.tiles;
        }
 
        if (this.tiles.length === 0) {
            this.generator = parameters.generator;
 
            if (this.generator != undefined) {
                this.tiles = this.generator.generate();
            }
        }
    }
 
    public getTiles(): TileInterface[][] {
        return this.tiles;
    }
 
    public getWidth(): number {
        return this.width;
    }
 
    public getHeight(): number {
        return this.height;
    }
 
    public getTile(position: PositionInterface): TileInterface {
        if (
            this.isTileOutOfBounds(position.x, position.y) === true ||
            this.tiles[position.x][position.y] == undefined
        ) {
            return this.tileFactory.create("null");
        }
 
        return this.tiles[position.x][position.y];
    }
 
    public dig(position: PositionInterface): void {
        if (this.getTile(position).isDiggable) {
            this.tiles[position.x][position.y] = new FloorTile();
        }
    }
 
    protected isTileOutOfBounds(x: number, y: number): boolean {
        return x < 0 || x >= this.width || y < 0 || y >= this.height;
    }
}