All files / games-roguelike/src/game/map map.ts

100% Statements 21/21
90% Branches 9/10
100% Functions 8/8
100% Lines 21/21

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 68 69 70 71 7222x               22x             18x 18x   18x   18x   18x       1x       11x       11x       1951x       1x 1x   1950x         2x 2x               7x 24x 24x     7x       1951x      
import {
    FloorTile,
    MapGenerator,
    PositionInterface,
    TileFactory,
    TileInterface,
} from "../index";
 
export class GameMap {
    private generator: MapGenerator;
    private tiles: TileInterface[][];
    private width: number;
    private height: number;
 
    public constructor(width: number, height: number) {
        this.width = width;
        this.height = height;
 
        this.tiles = [];
 
        this.generator = new MapGenerator(this.width, this.height);
 
        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 (
            true === this.isTileOutOfBounds(position.x, position.y) ||
            null == this.tiles[position.x][position.y]
        ) {
            const tileFactory = new TileFactory();
            return tileFactory.create("null");
        } else {
            return this.tiles[position.x][position.y];
        }
    }
 
    public dig(position: PositionInterface): void {
        Eif (this.getTile(position).isDiggable) {
            this.tiles[position.x][position.y] = new FloorTile();
        }
    }
 
    public getRandomFloorPosition(): PositionInterface {
        let x: number;
        let y: number;
 
        do {
            x = Math.floor(Math.random() * this.width);
            y = Math.floor(Math.random() * this.height);
        } while (!this.getTile({ x, y }).isWalkable);
 
        return { x, y };
    }
 
    protected isTileOutOfBounds(x: number, y: number): boolean {
        return x < 0 || x >= this.width || y < 0 || y >= this.height;
    }
}