Package Exports
- astar-typescript
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (astar-typescript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
AStar-Typescript
The A* search algorithm library in TypeScript.
AStar-TypeScript is an A* pathfinding API written in TypeScript to use for your HTML5 games or other browser-based projects.
This library was influenced and inspired by @qioa - PathFinding.js, @bgrins - javascript-astar, @prettymuchbryce - easystarjs and @redblobgames.
Buy me a coffee
Whether you use this project, have learned something from it, or just like it, please consider supporting it by buying me a coffee.
Installation
npm install astar-typescript --save
yarn add astar-typescript
bower install astar-typescript --save
Import
TypeScript
import { AStarFinder } from "astar-typescript";
Javascript
let AStarFinder = require("astar-typescript");
AMD
define(function(require,exports,module){
let AStarFinder = require('astar-typescript');
});
Usage
Create an astar instance:
private aStarInstance: AStarFinder;
Load grid data:
Using an array (hardcoded or from a Tilemap-Editor)
0 = walkable 1 = not walkable
let myMatrix = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 1, 0],
[0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 0]
];
this.aStarInstance = new AStarFinder({
grid: {
width: 8,
height: 8,
matrix: myMatrix
}
});
or randomly generated array from width and height
this.aStarInstance = new AStarFinder({
grid: {
width: 8,
height: 8
}
});
Get the path:
let startPos = { x: 0, y: 0 };
let goalPos = { x: 4, y: 5 };
let myPathway = this.aStarInstance.findPath(startPos, goalPos);
license
MIT