@nichathan-gaming/map-generator
Version:
Creates and generates a 2 dimensional array with various path generation functions.
36 lines (30 loc) • 1.38 kB
JavaScript
import countNeighbors from '../helpers/countNeighbors.js';
import mapGenerator from '../mapGenerator.js';
import getConnectedIndexes from '../helpers/getConnectedIndexes.js';
import assertIndex from '../helpers/assertIndex.js';
import {assertInstanceOf, assertType, basicTypes} from '@nichathan-gaming/assertions';
/**
* Walks through the map's multArray and and assigns a walkable path
* @param {mapGenerator} map The current game map element
* @param {number} maxPathSize The maximun size of the path
* @param {[number, number]} startIndex The index to start the path at
* @returns {mapGenerator} The map with the current path walked
*/
const recursiveGenerator = (map, maxPathSize, startIndex) =>{
assertInstanceOf(map, mapGenerator);
assertType(maxPathSize, basicTypes.number);
assertIndex(startIndex);
//we cannot move to this index, do not continue
if(countNeighbors(map, 1, startIndex, map.getWalkableValue(), false) > maxPathSize){
return map;
};
//we can move at this index, set the value
map.setWalkableValueAtIndex(startIndex);
//recursively add to the path
getConnectedIndexes(map, startIndex).forEach(element=>{
map = recursiveGenerator(map, maxPathSize, element);
});
//return the current path
return map;
};
export default recursiveGenerator;