Fast node.js pathfinding on workers for grid-based games.
.
Documentation
.
npm i pathfinding-worker.
constpathfinding=newPathfinding(config: PathfindingConfig)config- Pathfinding configuration
| Prop | Description | Default |
|---|---|---|
| taskFindingRate | Delay to handle next task after all previous are done | 100 ms |
| maxStackSize | Max recursive depth to handle task in one tick | 128 |
| resourceLimits | Worker resource limits | - |
pathfinding.destroy();.
constlayer=pathfinding.createLayer(grid: PathfindingGrid,)grid- Grid with walkable tiles
pathfinding.getLayers();pathfinding.getLayer(id: string)id- Layer id
layer.remove();.
constidTask=layer.findPath(config: PathfindingTaskConfig,callback: PathfindingTaskCallback,)config- Task configuration
| Prop | Description | Default |
|---|---|---|
| from | Begin tile position | |
| to | End tile position | |
| diagonals | Allow diagonal directions | true |
callback- Callback with result
| Prop | Description | Type |
|---|---|---|
| path | Path to target cell | Array<{ x, y }> |
| weight | Total path weight | number |
layer.cancel(id: number)id- Task id
.
layer.setWalkable(position: PathfindingPosition,value: number,)position- Tile positionstate- Walkable state
constwalkable=pathfinder.isWalkable(position: PathfindingPosition,)position- Tile position
.
layer.setWeight(position: PathfindingPosition,value: number,)position- Tile positionvalue- New weight
layer.resetWeight(position: PathfindingPosition,)position- Tile position
constweight=layer.getWeight(position: PathfindingPosition,)position- Tile position
.
constpathfinding=newPathfinding({loopRate: 500,});constlayer=pathfinding.createLayer([[true,true,true,true],[true,true,false,true],[true,false,false,true],[true,false,false,false],]);layer.findPath({from: {x: 0,y: 0},to: {x: 3,y: 2},},({ path, cost })=>{console.log("Result path:",path);console.log("Total cost:",cost);});