@nichathan-gaming/map-generator
Version:
Creates and generates a 2 dimensional array with various path generation functions.
39 lines (32 loc) • 1.65 kB
JavaScript
import countNeighbors from "../helpers/countNeighbors.js";
import mapGenerator from "../mapGenerator.js";
import compareIndexes from "../helpers/compareIndexes.js";
import getConnectedIndexes from "../helpers/getConnectedIndexes.js";
import assertIndex from "../helpers/assertIndex.js";
import { basicTypes, assertType, assertArray, assertInstanceOf } from "@nichathan-gaming/assertions";
/**
* Searches the indexes connected to the search index that are connected to the main path
* @param {mapGenerator} map The current game map element
* @param {[number, number]} searchIndex The index to search at
* @param {[number, number][]} currentPath The current path that has been walked along
* @param {number} maxPathSize The maximum size that the path can be
* @returns {[number, number][]} An array of indexes connected to the main path
*/
const getWilsonsConnectedIndexes = (map, searchIndex, currentPath, maxPathSize) => {
assertInstanceOf(map, mapGenerator);
assertIndex(searchIndex);
assertArray(currentPath);
assertType(maxPathSize, basicTypes.number);
//get the indexes connected to this index
let connectedIndexes = getConnectedIndexes(map, searchIndex);
//filter out bad indexes
connectedIndexes = connectedIndexes.filter(el=>{
if(currentPath.includes(el) || compareIndexes(el, searchIndex)){
return false;
};
const neighbors = countNeighbors(map, 1, el, map.getWalkableValue(), false) ;
return neighbors > 0 && neighbors <= maxPathSize;
});
return connectedIndexes;
};
export default getWilsonsConnectedIndexes;