@esengine/pathfinding
Version:
寻路算法库,支持A*、广度优先等算法,适用于Cocos Creator、Laya等游戏引擎
153 lines (143 loc) • 5.18 kB
TypeScript
/**
* @esengine/pathfinding v1.0.8
* TypeScript definitions
*/
interface IVector2 {
x: number;
y: number;
}
interface IComparableVector2 extends IVector2 {
equals?(other: IVector2): boolean;
}
declare class Vector2Utils {
private static readonly HASH_MULTIPLIER;
private static readonly MAX_COORD;
static equals(a: IVector2, b: IVector2): boolean;
static create(x: number, y: number): IVector2;
static clone(vector: IVector2): IVector2;
static add(a: IVector2, b: IVector2): IVector2;
static manhattanDistance(a: IVector2, b: IVector2): number;
static distance(a: IVector2, b: IVector2): number;
static toHash(vector: IVector2): number;
static toKey(vector: IVector2): string;
static fromHash(hash: number): IVector2;
}
interface IPriorityQueueNode {
priority: number;
}
declare class PriorityQueue<T extends IPriorityQueueNode> {
private _heap;
private _size;
get size(): number;
get isEmpty(): boolean;
clear(): void;
enqueue(item: T): void;
dequeue(): T | undefined;
peek(): T | undefined;
private _bubbleUp;
private _bubbleDown;
private _swap;
}
interface IAstarGraph<T extends IVector2> {
getNeighbors(node: T): T[];
cost(from: T, to: T): number;
heuristic(node: T, goal: T): number;
isNodePassable(node: T): boolean;
}
declare class AStarNode implements IPriorityQueueNode {
node: IVector2;
priority: number;
gCost: number;
hCost: number;
parent: AStarNode | null;
hash: number;
constructor(node: IVector2, gCost?: number, hCost?: number, parent?: AStarNode | null);
updateCosts(gCost: number, hCost: number, parent?: AStarNode | null): void;
updateNode(node: IVector2, gCost?: number, hCost?: number, parent?: AStarNode | null): void;
reset(): void;
}
declare class AStarPathfinder {
private static _nodePool;
private static _tempPath;
private static _getNode;
private static _recycleNode;
static search<T extends IVector2>(graph: IAstarGraph<T>, start: T, goal: T): {
found: boolean;
goalNode?: AStarNode;
openSetNodes?: AStarNode[];
};
static searchPath<T extends IVector2>(graph: IAstarGraph<T>, start: T, goal: T): T[];
private static reconstructPathFromNode;
static hasPath<T extends IVector2>(graph: IAstarGraph<T>, start: T, goal: T): boolean;
static clearPool(): void;
static getPoolStats(): {
poolSize: number;
maxPoolSize: number;
};
}
declare class AstarGridGraph implements IAstarGraph<IVector2> {
dirs: IVector2[];
walls: IVector2[];
weightedNodes: IVector2[];
defaultWeight: number;
weightedNodeWeight: number;
private _width;
private _height;
private _neighbors;
private _wallsSet;
private _weightedNodesSet;
private _wallsDirty;
private _weightedNodesDirty;
constructor(width: number, height: number);
addWall(wall: IVector2): void;
addWalls(walls: IVector2[]): void;
clearWalls(): void;
addWeightedNode(node: IVector2): void;
addWeightedNodes(nodes: IVector2[]): void;
clearWeightedNodes(): void;
private _updateHashSets;
isNodeInBounds(node: IVector2): boolean;
isNodePassable(node: IVector2): boolean;
search(start: IVector2, goal: IVector2): boolean;
searchPath(start: IVector2, goal: IVector2): IVector2[];
getNeighbors(node: IVector2): IVector2[];
cost(from: IVector2, to: IVector2): number;
heuristic(node: IVector2, goal: IVector2): number;
getStats(): {
walls: number;
weightedNodes: number;
gridSize: string;
wallsSetSize: number;
weightedNodesSetSize: number;
};
}
interface IUnweightedGraph<T extends IVector2> {
getNeighbors(node: T): T[];
}
declare class BreadthFirstPathfinder {
static search<T extends IVector2>(graph: IUnweightedGraph<T>, start: T, goal: T, cameFrom?: Map<number, T>): boolean;
static searchPath<T extends IVector2>(graph: IUnweightedGraph<T>, start: T, goal: T): T[];
static reconstructPath<T extends IVector2>(cameFrom: Map<number, T>, start: T, goal: T): T[];
}
declare class UnweightedGraph<T extends IVector2> implements IUnweightedGraph<T> {
edges: Map<T, T[]>;
addEdgesForNode(node: T, neighbors: T[]): this;
getNeighbors(node: T): T[];
}
declare class UnweightedGridGraph implements IUnweightedGraph<IVector2> {
private static readonly CARDINAL_DIRS;
private static readonly COMPASS_DIRS;
walls: IVector2[];
private _width;
private _height;
private _dirs;
private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: IVector2): boolean;
isNodePassable(node: IVector2): boolean;
getNeighbors(node: IVector2): IVector2[];
searchPath(start: IVector2, goal: IVector2): IVector2[];
hasPath(start: IVector2, goal: IVector2): boolean;
}
export { AStarPathfinder, AstarGridGraph, BreadthFirstPathfinder, PriorityQueue, UnweightedGraph, UnweightedGridGraph, Vector2Utils };
export type { IAstarGraph, IComparableVector2, IPriorityQueueNode, IUnweightedGraph, IVector2 };