UNPKG

@nichathan-gaming/map-generator

Version:

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

48 lines (39 loc) 1.64 kB
import mapGenerator from '../../mapGenerator.js'; import assertIndex from '../assertIndex.js'; import { assertInstanceOf } from '@nichathan-gaming/assertions'; /** * Determines if the search index has a base value below and above and not left or right and vice versa * @param {mapGenerator} map The current game map element * @param {[number, number]} searchIndex The index to search on * @returns {boolean} If this is a valid hole or not */ const isValidHole = (map, searchIndex) => { assertInstanceOf(map, mapGenerator); assertIndex(searchIndex); const searchIndexes = [ [searchIndex[0]+1, searchIndex[1]], [searchIndex[0]-1, searchIndex[1]], [searchIndex[0], searchIndex[1]+1], [searchIndex[0], searchIndex[1]-1], ]; //has base value above let hasValAbove = map.isValidIndex(searchIndexes[0]) ? map.isIndexWalkable(searchIndexes[0]) : true; //has base value below let hasValBelow = map.isValidIndex(searchIndexes[1]) ? map.isIndexWalkable(searchIndexes[1]) : true; //has base value right let hasValRight = map.isValidIndex(searchIndexes[2]) ? map.isIndexWalkable(searchIndexes[2]) : true; //has base value left let hasValLeft = map.isValidIndex(searchIndexes[3]) ? map.isIndexWalkable(searchIndexes[3]) : true; const vert = hasValAbove && hasValBelow && !(hasValLeft || hasValRight); const hori = hasValLeft && hasValRight && !(hasValAbove || hasValBelow); return vert || hori; }; export default isValidHole;