UNPKG

peng-pathfinding

Version:

Comprehensive pathfinding library for grid based games

172 lines (166 loc) 6.39 kB
export = pfg; export as namespace pfg; declare namespace pfg { /** * 获取版本号 */ function version(): string; /** * 动态寻路者 */ class DynamicFinder { /** * 寻找路径,同步给出结果,不推荐这种方式,因为寻路可能会消耗很多时间 */ findPathSync(startX: number, startY: number, endX: number, endY: number, grid: Grid): Array<Array<number>>; /** * 寻找路径,异步给出结果 */ findPathAsync(startX: number, startY: number, endX: number, endY: number, grid: Grid): Promise<Array<Array<number>>>; /** * 创建寻路状态索引 * @param limitStep 步数限制,如果限制内未能找到路径则会返回推测的路径 * @return 返回寻路进行的状态 */ //create(startX: number, startY: number, endX: number, endY: number, limitStep: number): alu.logics.PathFindStatus; /** * 根据索引暂停寻路 * @param id */ //pause(id: number): alu.logics.PathFindStatus; /** * 根据索引继续寻路 */ //continue (id: number): alu.logics.PathFindStatus; } //对角运动 enum DiagonalMovement { //总是 Always = 1, //绝不 Never = 2, //最多只有一个障碍时 IfAtMostOneObstacle = 3, //仅在没有任何障碍时 OnlyWhenNoObstacles = 4 } /** * A* path-finder. Based upon https://github.com/bgrins/javascript-astar * @constructor * @param {object} opt * @param {boolean} opt.allowDiagonal Whether diagonal movement is allowed. * Deprecated, use diagonalMovement instead. * @param {boolean} opt.dontCrossCorners Disallow diagonal movement touching * block corners. Deprecated, use diagonalMovement instead. * @param {DiagonalMovement} opt.diagonalMovement Allowed diagonal movement. * @param {function} opt.heuristic Heuristic function to estimate the distance * (defaults to manhattan). * @param {number} opt.weight Weight to apply to the heuristic to allow for * suboptimal paths, in order to speed up the search. */ class AStarFinder { constructor(opt: { allowDiagonal: boolean, dontCrossCorners: boolean, diagonalMovement: DiagonalMovement, heuristic: Function, weight: number }); /** * 寻找并返回一条路径 * Find and return the the path. * @param {number} startX - 起点X坐标 * @param {number} startY - 起点Y坐标 * @param {number} endX - 终点X坐标 * @param {number} endY - 终点Y坐标 * @param {Grid} grid - 网格地图 * @return {Array<Array<number>>} The path, including both start and * end positions. */ findPath(startX, startY, endX, endY, grid); } /** * The Grid class, which serves as the encapsulation of the layout of the nodes. * @constructor * @param {number|Array<Array<(number|boolean)>>} width_or_matrix Number of columns of the grid, or matrix * @param {number} height Number of rows of the grid. * @param {Array<Array<(number|boolean)>>} [matrix] - A 0-1 matrix * representing the walkable status of the nodes(0 or false for walkable). * If the matrix is not supplied, all the nodes will be walkable. */ class Grid { /**合并网格数组返回新的网格 */ public static Merge(arr: Grid[]): Grid; constructor(x: number, y: number,size:number, width_or_matrix: number | Array<Array<(number | boolean)>>, height?: number, matrix?:Array<Array<(number | boolean)>>); /**坐标X */ public x: number; /**坐标Y */ public y: number; /**单个格子的尺寸 */ public size:number; /**宽度 */ public width: number; /**高度 */ public height: number; public nodes: Node[][]; /**设置是否可以通行*/ public setWalkableAt(x, y, walkable); /**设置代价系数 */ public setCostRate(x, y, r); /**增加代价系数 */ public addCostRate(x, y, r); public clone(): Grid; public merge(grid: Grid): Grid; } class Cell{ public x:number; public y:number; public f:number; public g:number; /**到终点的代价*/ public h:number; /**代价系数 */ public r:number; public walkable:boolean; public opened:boolean; constructor(x:number, y:number, walkable?,r?); } /** * 代价函数 * @namespace pfg.Heuristic * @description A collection of heuristic functions. */ namespace Heuristic { /** * 曼哈顿距离,指的是直角折线的距离,即 X + Y * Manhattan distance. * @param {number} dx - Difference in x. * @param {number} dy - Difference in y. * @return {number} dx + dy */ export function manhattan(dx, dy); /** * 欧氏距离,也就是直线距离 * Euclidean distance. * @param {number} dx - Difference in x. * @param {number} dy - Difference in y. * @return {number} sqrt(dx * dx + dy * dy) */ export function euclidean(dx, dy); /** * 八进制距离,斜线距离的特殊形式,允许存在折线的直线距离 * Octile distance. * @param {number} dx - Difference in x. * @param {number} dy - Difference in y. * @return {number} (dx < dy) ? F * dx + dy : F * dy + dx; */ export function octile(dx, dy); /** * 切比雪夫距离,斜线距离的特殊形式,即X或者Y中取最大值 * Chebyshev distance. * @param {number} dx - Difference in x. * @param {number} dy - Difference in y. * @return {number} max(dx, dy) */ export function chebyshev(dx, dy); } }