Constructor
new PathFinder(source)
| Name | Type | Description |
|---|---|---|
source | TileCollisionLayer | | Either a TileCollisionLayer (size and walkability auto-derived) or a Vector2 grid size (user overrides isWalkable). |
- Source
// 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| Type | Description |
|---|---|
| boolean | If true, draw debug visualization during findPath |
- Source
debugTime
Properties| Type | Description |
|---|---|
| number | Debug primitive lifetime in seconds (0 disables drawing) |
- Source
heuristicWeight
Properties| Type | Description |
|---|---|
| number | A* heuristic multiplier (1 = admissible, higher = greedier) |
- Source
maxLoop :number|undefined
- number |
undefined
| Type | Description |
|---|---|
| number | | 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 |
- Source
nodes :Array.<PathFinderNode>
- Array.<PathFinderNode>
| Type | Description |
|---|---|
| Array.<PathFinderNode> | Flat row-major array of size.x*size.y nodes |
- Source
searchGaveUp
Properties| Type | Description |
|---|---|
| boolean | True when the last search stopped at maxLoop with no path, so it gave up rather than that there is no way through |
- Source
size :Vector2
- Vector2
| Type | Description |
|---|---|
| Vector2 | Grid dimensions in tiles |
- Source
smoothPath
Properties| Type | Description |
|---|---|
| boolean | If true, post-process paths with two-pass smoothing |
- Source
tileLayer :TileCollisionLayer|undefined
- TileCollisionLayer |
undefined
| Type | Description |
|---|---|
| TileCollisionLayer | | Tile layer driving walkability, if any |
- Source
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.
- Source
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.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
startPos | Vector2 | World-space start | ||
endPos | Vector2 | World-space end | ||
rebuild | boolean | <optional> | true | Whether to call buildNodeData first |
- Source
- 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).
| Name | Type | Description |
|---|---|---|
x | number | Tile x |
y | number | Tile y |
- Source
- 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.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
worldPos | Vector2 | |||
searchRange | number | <optional> | 10 | Max box-radius in tiles |
rebuild | boolean | <optional> | true | Whether to call buildNodeData first |
- Source
- Type:
- PathFinderNode |
null
getNode(x, y) → {PathFinderNode|null}
Get the node at tile coords, or null if out of bounds.
| Name | Type | Description |
|---|---|---|
x | number | |
y | number |
- Source
- 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.
| Name | Type | Description |
|---|---|---|
x | number | Tile x |
y | number | Tile y |
- Source
- Type:
- boolean
tileToWorld(x, y) → {Vector2}
Convert integer tile coords to the world-space center of that tile.
| Name | Type | Description |
|---|---|---|
x | number | |
y | number |
- Source
- Type:
- Vector2
worldToTile(worldPos) → {Vector2}
Convert a world-space position to integer tile coords (no clamping).
| Name | Type | Description |
|---|---|---|
worldPos | Vector2 |
- Source
- Type:
- Vector2