Skip to content

Repository files navigation

🧩 Pathfinding Worker

VersionSmall sizeBuildTest

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

.

Documentation

.

Install

npm i pathfinding-worker

.

General

⚡️ Create worker thread

constpathfinding=newPathfinding(config: PathfindingConfig)
  • config - Pathfinding configuration
PropDescriptionDefault
taskFindingRateDelay to handle next task after all previous are done100 ms
maxStackSizeMax recursive depth to handle task in one tick128
resourceLimitsWorker resource limits-

⚡️ Terminate worker thread

pathfinding.destroy();

.

Layers

⚡️ Create a new layer of grid

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

⚡️ Get list of created layers

pathfinding.getLayers();

⚡️ Get layer by id

pathfinding.getLayer(id: string)
  • id - Layer id

⚡️ Remove exist layer of grid

layer.remove();

.

Finding

⚡️ Create pathfinder task

constidTask=layer.findPath(config: PathfindingTaskConfig,callback: PathfindingTaskCallback,)
  • config - Task configuration
PropDescriptionDefault
fromBegin tile position
toEnd tile position
diagonalsAllow diagonal directionstrue
  • callback - Callback with result
PropDescriptionType
pathPath to target cellArray<{ x, y }>
weightTotal path weightnumber

⚡️ 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

constwalkable=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

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

.

Example

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

About

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

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages