@2d-game-grid/square
Version:
A simple square grid made for games
30 lines (29 loc) • 1.2 kB
JavaScript
import {Grid as FinderGrid} from 'pathfinding'
import {mapCells} from './mapper/mapCells.js'
import {mapPathfindingDiagonalMovement} from './mapper/mapPathfindingDiagonalMovement.js'
import {mapWalkableMatrix} from './mapper/mapWalkableMatrix.js'
import {mapFinder} from './mapper/mapFinder.js'
import {mapHeuristic} from './mapper/mapHeuristic.js'
/**
* @param grid The grid
* @param start The coordinate that the path should start
* @param end The coordinate that the path should end
* @param options The options to customize the pathfinding
* @returns The shortest path including the start and the end cell
*/
export function getPath(grid, start, end, options) {
const {
algorithm = 'A_STAR',
diagonalMovement = 'ALWAYS',
heuristic = 'MANHATTAN',
isWalkable = () => true,
} = options || {}
const matrix = mapWalkableMatrix(grid, isWalkable)
const pathfindingGrid = new FinderGrid(matrix)
const finder = mapFinder(algorithm, {
diagonalMovement: mapPathfindingDiagonalMovement(diagonalMovement),
heuristic: mapHeuristic(grid, heuristic),
})
const path = finder.findPath(start.col, start.row, end.col, end.row, pathfindingGrid)
return mapCells(grid, path)
}