All files / projects-dev/libs/games/src/maps generator-cellular.ts

100% Statements 52/52
100% Branches 9/9
100% Functions 3/3
100% Lines 52/52

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 531x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 14x 14x 560x 560x 560x 29600x 29600x 29600x 560x 14x 14x 14x 14x 28x 59200x 28x 28x 28x 28x 14x 8x  
import * as ROT from "rot-js";
 
import { TileFactory, TileInterface } from "../tiles/index";
 
import { MapGeneratorInterface } from "./interfaces";
 
export class CellularMapGenerator implements MapGeneratorInterface {
    protected width: number;
 
    protected height: number;
 
    protected tiles: TileInterface[][] = [];
 
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    private readonly generator: any;
 
    // private generator: Cellular;
    private readonly totalIterations = 3;
 
    private readonly tileFactory: TileFactory;
 
    public constructor(width: number, height: number, tileFactory: TileFactory) {
        this.width = width;
        this.height = height;
        this.tileFactory = tileFactory;
 
        this.generator = new ROT.Map.Cellular(width, height);
        this.generator.randomize(0.5);
    }
 
    public generate(): TileInterface[][] {
        // eslint-disable-next-line no-loops/no-loops
        for (let x = 0; x < this.width; x++) {
            this.tiles.push([]);
            // eslint-disable-next-line no-loops/no-loops
            for (let y = 0; y < this.height; y++) {
                // eslint-disable-next-line security/detect-object-injection
                this.tiles[x].push(this.tileFactory.create("null"));
            }
        }
 
        // Iteratively smoothen the map
        // eslint-disable-next-line no-loops/no-loops
        for (let index = 0; index < this.totalIterations - 1; index++) {
            this.generator.create((x: number, y: number, v: number): void => {
                this.tiles[x][y] = v === 1 ? this.tileFactory.create("floor") : this.tileFactory.create("wall");
            });
        }
 
        return this.tiles;
    }
}