UNPKG

@nichathan-gaming/map-generator

Version:

Creates and generates a 2 dimensional array with various path generation functions.

34 lines (31 loc) 1.65 kB
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"; /** * Gets a shuffled array of possible indexes that are filtered based on if the index is within the bounds of the map, * if the index is not already part of the map, * and that the total number of neighbors that are base elements or possible path values is less than or equal to the max path size * @param {mapGenerator} map The current game map element * @param {number} maxPathSize The maximum size for the path * @param {[number, number]} searchIndex The index to get moves at * @param {T} possiblePathValue The value that denotes a path * @returns {[number, number][]} A shuffled and filtered array of indexes that this index can be moved at */ const getNextMoves = (map, maxPathSize, searchIndex, possiblePathValue) => { assertInstanceOf(map, mapGenerator); assertType(maxPathSize, basicTypes.number); assertIndex(searchIndex); map.assertBasicType(possiblePathValue); return getConnectedIndexes(map, searchIndex).filter(element => !map.isValueAtIndexEqualToValue(element, possiblePathValue) && ( ( countNeighbors(map, 1, element, possiblePathValue, false) + countNeighbors(map, 1, element, map.getWalkableValue(), false) ) <= maxPathSize ) ); }; export default getNextMoves;