@nichathan-gaming/map-generator
Version:
Creates and generates a 2 dimensional array with various path generation functions.
62 lines (51 loc) • 2.56 kB
JavaScript
import assertIndex from "../helpers/assertIndex.js";;
import mapGenerator from "../mapGenerator.js";
import getNextMoves from "./getNextMoves.js";
import { assertInstanceOf, assertType, basicTypes } from "@nichathan-gaming/assertions";
/**
* Generates the main path for the map that will end once it cannot move forward anymore
* @param {mapGenerator} map The current game map element
* @param {number} maxPathSize The maximum size for the path
* @param {[number, number]} startIndex The index that the path begins at
* @param {T} possiblePathValue A temporary value that denotes a part of the map that may be the next path
* @param {(a, b) => boolean} equalityFunction A function to determine if 2 elements are equal
*/
const firstWalk = (map, maxPathSize, startIndex, possiblePathValue) => {
assertInstanceOf(map, mapGenerator);
assertType(maxPathSize, basicTypes.number);
assertIndex(startIndex);
map.assertBasicType(possiblePathValue);
//initiates the possible path at the starting index
map.setValueAtIndex(startIndex, possiblePathValue);
//get the next moves for the walk
let nextIndexes = getNextMoves(map, maxPathSize, startIndex, possiblePathValue);
//start tracking the current path with the start index
const currentPath = [startIndex];
//This should always be greater than 0 when we can take the next step
if(nextIndexes.length > 0){
//get the next move for the path from the shuffled next moves
let currentIndex = nextIndexes.splice(0, 1)[0];
//while we can keep moving, continue moving
while(true){
//set this index as a path
map.setValueAtIndex(currentIndex, possiblePathValue);
//add the current move to the path
currentPath.push(currentIndex);
//get the next moves for the current index
nextIndexes = getNextMoves(map, maxPathSize, currentIndex, possiblePathValue);
//we have a move that we can make, continue moving
if(nextIndexes.length > 0){
currentIndex = nextIndexes.splice(0, 1)[0];
}
//we do not have any more moves to make, set the main path
else{
break;
};
};
};
//now that we have finished the path, go through currentPath to assign it to base value
currentPath.forEach(move=>{
map.setWalkableValueAtIndex(move);
});
};
export default firstWalk;