UNPKG

@nichathan-gaming/map-generator

Version:

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

33 lines (27 loc) 1.12 kB
import mapGenerator from "../mapGenerator.js"; import getRandomRange from '../helpers/getRandomRange.js'; import coinFlip from '../helpers/coinFlip.js'; import assertIndex from "../helpers/assertIndex.js"; import { assertInstanceOf } from "@nichathan-gaming/assertions"; /** * Crawls along the map randomly * @param {mapGenerator} map The current game map element * @param {index} startIndex The index to start crawling at * @param {index} minIndex The minimum crawling index, either -1 or 0 */ const crawl = (map, startIndex, minIndex) => { assertInstanceOf(map, mapGenerator); assertIndex(startIndex); assertIndex(minIndex); const width = map.getWidth(); const height = map.getHeight(); while(true){ map.setUnwalkableValueAtIndex(startIndex); if(coinFlip()) startIndex[0] += getRandomRange(minIndex[0], 2); else startIndex[1] += getRandomRange(minIndex[1], 2); if (startIndex[0] < 0 || startIndex[0] > width || startIndex[1] < 0 || startIndex[1] > height) { break; }; }; }; export default crawl;