Make a path finder over a tile layer or a grid
source TileCollisionLayer|Vector2Either a TileCollisionLayer
(size and walkability auto-derived) or a Vector2 grid size (user
overrides isWalkable).
// Tile-layer driven (most common):
const pf = new PathFinder(myTileCollisionLayer);
const path = pf.findPath(player.pos, mousePos);
// Bare grid with custom walkability:
const gridFinder = new PathFinder(vec2(50, 50));
gridFinder.isWalkable = (x, y) => myGrid[y*50 + x] === 0;
If true, draw debug visualization during findPath
Debug primitive lifetime in seconds (0 disables drawing)
A* heuristic multiplier (1 = admissible, higher = greedier)
Most A* expansions before giving up, undefined for the number of cells,
so a search always finishes; a lower one caps the time a search takes, see searchGaveUp
Flat row-major array of size.x*size.y nodes
True when the last search stopped at maxLoop with no path, so it gave up rather
than that there is no way through
If true, post-process paths with two-pass smoothing
Tile layer driving walkability, if any
Reset all nodes and re-populate walkable / cost / posWorld from the
current isWalkable / getCost overrides. Called at the start of
findPath; call it directly before searches made with rebuild=false.
Find a path from startPos to endPos in world space. Returns an array
of world-space Vector2 points; empty array if no path exists.
Start and end are snapped to the nearest walkable tile (a costed one
counts), within 10 tiles. Intermediate points are tile centers unless the
string-pulling smoothing pass moves them off-grid.
By default, calls buildNodeData() first, which asks isWalkable and
getCost about every cell. When finding many paths with unchanged
walkability, pass rebuild=false and call buildNodeData() once
externally; the paths found are the same.
startPos Vector2World-space start
endPos Vector2World-space end
rebuild boolean optional, default trueWhether to call buildNodeData first
Default extra cost for stepping on a cell. Returns 0 (free) by default.
Override to add cost-weighted terrain (mud, swamp, etc).
x numberTile x
y numberTile y
Find the clear (walkable, zero-cost) node closest to the given world
position. Spirals outward in expanding boxes until a clear node is
found or the search range is exhausted. Useful for snapping a click
or NPC spawn position to the nearest open tile.
By default, calls buildNodeData() first so it works correctly on a
fresh PathFinder. If you're calling it many times in a row with
unchanged walkability, pass rebuild=false and call buildNodeData()
once externally to avoid redundant work.
worldPos Vector2searchRange number optional, default 10Max box-radius in tiles
rebuild boolean optional, default trueWhether to call buildNodeData first
Get the node at tile coords, or null if out of bounds
x numbery number
Default walkability: if a tile layer was provided, returns true when the
cell has no solid (positive) collision data, so negative data is open
like it is to the engine's collision; otherwise returns true. Override on
the instance or via a subclass.
x numberTile x
y numberTile y
Convert integer tile coords to the world-space center of that tile
x numbery number
Convert a world-space position to integer tile coords (no clamping)
worldPos Vector2