@nichathan-gaming/map-generator
Version:
Creates and generates a 2 dimensional array with various path generation functions.
55 lines (45 loc) • 2.06 kB
JavaScript
import mapGenerator from "../../mapGenerator.js";
import countNeighbors from "../countNeighbors.js";
import shuffle from "../shuffle.js";
import isValidHole from './isValidHole.js';
import {assertInstanceOf, assertType, basicTypes} from '@nichathan-gaming/assertions';
/**
* Fills all of the holes in the map
* @param {mapGenerator} map The current game map element
* @param {number} maxPathSize The maximum size for the path
*/
const fillHoles = (map, maxPathSize) => {
assertInstanceOf(map, mapGenerator);
assertType(maxPathSize, basicTypes.number);
//get the dimensions of the map
const width = map.getWidth();
const height = map.getHeight();
let availableIndexes = [];
//fill the holes left in the map where there is an unwalkable value with a neighbor count that is greater than 0 and less than or equal to the maxPathSize
for(let i = 0; i < height; i++){
for(let j = 0; j < width; j++){
const index = [i, j];
const neighborCount = countNeighbors(map, 1, index, map.getWalkableValue(), false);
if(map.isIndexUnwalkable(index) && neighborCount > 0 && neighborCount < 3){
availableIndexes.push(index);
};
};
};
availableIndexes = shuffle(availableIndexes);
availableIndexes.forEach(el=>{
if(isValidHole(map, el)){
map.setWalkableValueAtIndex(el);
};
});
//fill the holes left in the map where there is an unwalkable value with a neighbor count that is greater than 0 and less than or equal to the maxPathSize
for(let i = 0; i < height; i++){
for(let j = 0; j < width; j++){
const index = [i, j];
const neighborCount = countNeighbors(map, 1, index, map.getWalkableValue(), false);
if(map.isIndexUnwalkable(index) && neighborCount > 0 && neighborCount <= maxPathSize){
map.setWalkableValueAtIndex(index);
};
};
};
};
export default fillHoles;