Fast node.js pathfinding on workers for grid-based games
npm install pathfinding-worker



Fast node.js pathfinding on workers for grid-based games.
.
Documentation
- Install
- General
- Layers
- Finding
- Tile walkable
- Tile weight
- Example
.
``sh`
npm i pathfinding-worker
.
`ts`
const pathfinding = new Pathfinding(
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 | - |
`ts`
pathfinding.destroy();
.
`ts`
const layer = pathfinding.createLayer(
grid: PathfindingGrid,
)
- grid - _Grid with walkable tiles_
`ts`
pathfinding.getLayers();
`ts`
pathfinding.getLayer(
id: string
)
- id - _Layer id_
`ts`
layer.remove();
.
`ts`
const idTask = 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 |
`ts`
layer.cancel(id: number)
- id - _Task id_
.
`ts`
layer.setWalkable(
position: PathfindingPosition,
value: number,
)
- position - _Tile position_state
- - _Walkable state_
`ts`
const walkable = pathfinder.isWalkable(
position: PathfindingPosition,
)
- position - _Tile position_
.
`ts`
layer.setWeight(
position: PathfindingPosition,
value: number,
)
- position - _Tile position_value
- - _New weight_
`ts`
layer.resetWeight(
position: PathfindingPosition,
)
- position - _Tile position_
`ts`
const weight = layer.getWeight(
position: PathfindingPosition,
)
- position - _Tile position_
.
`ts
const pathfinding = new Pathfinding({
loopRate: 500,
});
const layer = 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);
}
);
``