@2d-game-grid/square
Version:
A simple square grid made for games
23 lines (22 loc) • 852 B
JavaScript
import {ALL_DIRECTIONS, UniqueCellQueue} from '@2d-game-grid/core'
/**
* @param cell The start cell
* @param maxDistance The maximum distance (including) to a cell that should be returned
* @param algorithm The algorithm that should be used for the distance calculation
* @returns All cells that are in the distance (excluding the start cell)
*/
export function listCellsInDistance(cell, maxDistance, algorithm) {
const queue = new UniqueCellQueue()
const neighbors = cell.neighbors.list(ALL_DIRECTIONS)
queue.ignore(cell)
queue.addAll(neighbors)
const cellsInDistance = []
while (queue.hasNext()) {
const nextCell = queue.getNext()
if (nextCell.getDistance(cell, algorithm) <= maxDistance) {
cellsInDistance.push(nextCell)
queue.addAll(nextCell.neighbors.list(ALL_DIRECTIONS))
}
}
return cellsInDistance
}