@nichathan-gaming/map-generator
Version:
Creates and generates a 2 dimensional array with various path generation functions.
40 lines (33 loc) • 1.5 kB
JavaScript
import countNeighbors from "../helpers/countNeighbors.js";
import mapGenerator from "../mapGenerator.js";
import shuffle from "../helpers/shuffle.js";
import compareIndexes from "../helpers/compareIndexes.js";
import { assertInstanceOf, assertArray } from "@nichathan-gaming/assertions";
/**
* Search through the map and get the shuffled available starting points for the next paths
* that are not touching the main path
* @param {mapGenerator} map The current game map element
* @param {[number, number][]} badPaths The indexes that should be ignored
* @returns {[number, number][]} A shuffled index array of the possible starting points for the next path
*/
const getAvailableIndexes = (map, badPaths) => {
assertInstanceOf(map, mapGenerator);
assertArray(badPaths);
//track available indexes
let locations = [];
//get width and height of the map to search all indexes
const height = map.getHeight();
const width = map.getWidth();
for(let i = 0; i < height; i++){
for(let j = 0; j < width; j++){
//get the index at the current location
const index = [i, j];
if(!badPaths.some(el=>compareIndexes(el, index)) && countNeighbors(map, 1, index, map.getWalkableValue(), false) === 0){
//if it is valid, add it to locations
locations.push(index);
};
};
};
return shuffle(locations);
};
export default getAvailableIndexes;