UNPKG

@2d-game-grid/square

Version:
24 lines (23 loc) 952 B
import { ALL_DIRECTIONS, UniqueCellQueue } from '@2d-game-grid/core'; /** * @param cell The start cell * @param maxPathSteps The maximum amount of steps (including) to a cell that should be returned (start and end cell included) * @param options The options to customize the pathfinding * @returns All cells that are reachable (excluding the start cell) */ export function listReachableCells(cell, maxPathSteps, options) { const queue = new UniqueCellQueue(); const neighbors = cell.neighbors.list(ALL_DIRECTIONS); queue.ignore(cell); queue.addAll(neighbors); const reachableCells = []; while (queue.hasNext()) { const nextCell = queue.getNext(); const steps = cell.getPath(nextCell, options).length; if (steps >= 1 && steps <= maxPathSteps) { reachableCells.push(nextCell); queue.addAll(nextCell.neighbors.list(ALL_DIRECTIONS)); } } return reachableCells; }