PathFinding. PathFinder

Grid pathfinder using A* with two optional smoothing passes.

Constructor

new PathFinder(source)

Parameters:
NameTypeDescription
sourceTileCollisionLayer | Vector2

Either a TileCollisionLayer (size and walkability auto-derived) or a Vector2 grid size (user overrides isWalkable).

Example
// Tile-layer driven (most common):
 const pf = new PathFinder(myTileCollisionLayer);
 const path = pf.findPath(player.pos, mousePos);

 // Bare grid with custom walkability:
 const pf = new PathFinder(vec2(50, 50));
 pf.isWalkable = (x, y) => myGrid[y*50 + x] === 0;

Members

debug

Properties
TypeDescription
boolean

If true, draw debug visualization during findPath

debugTime

Properties
TypeDescription
number

Debug primitive lifetime in seconds (0 disables drawing)

heuristicWeight

Properties
TypeDescription
number

A* heuristic multiplier (1 = admissible, higher = greedier)

maxLoop :number|undefined

Type:
  • number | undefined
Properties
TypeDescription
number | undefined

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

nodes :Array.<PathFinderNode>

Type:
  • Array.<PathFinderNode>
Properties
TypeDescription
Array.<PathFinderNode>

Flat row-major array of size.x*size.y nodes

searchGaveUp

Properties
TypeDescription
boolean

True when the last search stopped at maxLoop with no path, so it gave up rather than that there is no way through

size :Vector2

Type:
  • Vector2
Properties
TypeDescription
Vector2

Grid dimensions in tiles

smoothPath

Properties
TypeDescription
boolean

If true, post-process paths with two-pass smoothing

tileLayer :TileCollisionLayer|undefined

Type:
  • TileCollisionLayer | undefined
Properties
TypeDescription
TileCollisionLayer | undefined

Tile layer driving walkability, if any

Methods

buildNodeData()

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.

findPath(startPos, endPos, rebuildopt) → {Array.<Vector2>}

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.

Parameters:
NameTypeAttributesDefaultDescription
startPosVector2

World-space start

endPosVector2

World-space end

rebuildboolean<optional>
true

Whether to call buildNodeData first

Returns:
Type: 
Array.<Vector2>

getCost(x, y) → {number}

Default extra cost for stepping on a cell. Returns 0 (free) by default. Override to add cost-weighted terrain (mud, swamp, etc).

Parameters:
NameTypeDescription
xnumber

Tile x

ynumber

Tile y

Returns:
Type: 
number

getNearestClearNode(worldPos, searchRangeopt, rebuildopt) → {PathFinderNode|null}

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.

Parameters:
NameTypeAttributesDefaultDescription
worldPosVector2
searchRangenumber<optional>
10

Max box-radius in tiles

rebuildboolean<optional>
true

Whether to call buildNodeData first

Returns:
Type: 
PathFinderNode | null

getNode(x, y) → {PathFinderNode|null}

Get the node at tile coords, or null if out of bounds.

Parameters:
NameTypeDescription
xnumber
ynumber
Returns:
Type: 
PathFinderNode | null

isWalkable(x, y) → {boolean}

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.

Parameters:
NameTypeDescription
xnumber

Tile x

ynumber

Tile y

Returns:
Type: 
boolean

tileToWorld(x, y) → {Vector2}

Convert integer tile coords to the world-space center of that tile.

Parameters:
NameTypeDescription
xnumber
ynumber
Returns:
Type: 
Vector2

worldToTile(worldPos) → {Vector2}

Convert a world-space position to integer tile coords (no clamping).

Parameters:
NameTypeDescription
worldPosVector2
Returns:
Type: 
Vector2