2.3.0 • Published 10 months ago

pathfinding-worker v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

🧩 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

PropDescriptionDefault
loopRateFinding loop rate200 ms
resourceLimitsWorker 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

PropDescriptionDefault
fromBegin tile position
toEnd tile position
diagonalsAllow diagonal directionstrue
  • 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);
})
2.3.0

10 months ago

2.1.2

11 months ago

2.2.0

10 months ago

2.1.1

11 months ago

2.1.4

11 months ago

2.1.3

11 months ago

2.1.5

11 months ago

2.1.0

11 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.0.1

11 months ago

2.0.0

11 months ago

1.2.0

12 months ago

1.1.0

12 months ago

1.0.1

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago