Skip to content

🧩 Fast node.js pathfinding on workers for grid-based games

Notifications You must be signed in to change notification settings

neki-dev/pathfinding-worker

Repository files navigation

🧩 Pathfinding Worker

Version Small size Build Test

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

.

Documentation

.

Install

npm i pathfinding-worker

.

General

⚡️ Create worker thread

const pathfinding = new Pathfinding(
  config: PathfindingConfig
)
  • config - Pathfinding configuration
Prop Description Default
loopRate Finding loop rate 200 ms
resourceLimits Worker resource limits -

⚡️ Terminate worker thread

pathfinding.destroy()

.

Layers

⚡️ Create a new layer of grid

const layer = pathfinding.createLayer(
  grid: PathfindingGrid,
)
  • grid - Grid with walkable tiles

⚡️ Remove exist layer of grid

layer.remove()

.

Finding

⚡️ Create pathfinder task

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

⚡️ Cancel pathfinder task

layer.cancel(id: number)
  • id - Task id

.

Tile walkable

⚡️ Set walkable state

layer.setWalkable(
  position: PathfindingPosition,
  value: number,
)
  • position - Tile position
  • state - Walkable state

⚡️ Get walkable state

const walkable = pathfinder.isWalkable(
  position: PathfindingPosition,
)
  • position - Tile position

.

Tile weight

⚡️ Set weight

layer.setWeight(
  position: PathfindingPosition,
  value: number,
)
  • position - Tile position
  • value - New weight

⚡️ Reset weight

layer.resetWeight(
  position: PathfindingPosition,
)
  • position - Tile position

⚡️ Get weight

const weight = layer.getWeight(
  position: PathfindingPosition,
)
  • position - Tile position

.

Example

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);
})