项目作者: jreyesribes

项目描述 :
An implementation of the A-STAR algorythm made with TypeScript and LoDash.
高级语言: TypeScript
项目地址: git://github.com/jreyesribes/A-STAR.git
创建时间: 2016-04-13T19:57:55Z
项目社区:https://github.com/jreyesribes/A-STAR

开源协议:MIT License

下载


A-STAR.ts

Pretty self-explanatory
This is a graph-based implementation of the A* algorithm… made with TypeScript and LoDash

because I love TypeScript and I happen to be using it at the moment to make the game that made me develop this library in the first place

The key difference with what I’ve seen so far when it comes to javascript A is that
unlike the traditional implementation, most are based on grids, so to speak, instead of graphs,
so if you need a graph-based A
implementation, you are in luck!

So how do I use it

Include A-STAR.js

Instantiate the ASTAR.Map class like so:

  1. var map = new ASTAR.Map(graph);

with “graph” being a JSON representation of your graph, in the following format:

  1. [
  2. {"x": x,"y": y,"connectedTo": [nodeIndex, anotherNodeIndex]},
  3. {"x": x,"y": y,"connectedTo": [nodeIndex, anotherNodeIndex]}.......
  4. ]

node indexes starting with 0 being the first node in your array

The Map class is rather simple, yet hopefully entails all the functionality you need,
it provides 3 methods:

  1. map.path(A, B);

The above will return the shortest path from node A to B

  1. var map2 = map.append({"x": x,"y": y,"connectedTo": [nodeIndex]});

This will return a copy of “map” with a newly appended node
Specially useful when most of your Map won’t change but you need it to be somewhat flexible

  1. var map2 = map.remove(nodeIndex);

This will return a copy of “map” without the node with the specified index

  1. map.dump();

This will return a JSON representation of “map”, in the previously specified format.

Also, I left a publicly accessible ASTAR.distance(x1, y1, x2, y2)
because you never know when it might be useful, it certainly has been to me!

Reach me

If you find this graph implementation of A* helpful, reach me! You could contribute, or tell me what
you miss in this library!

Buy me a coffee

If you appreciate this library, please consider donating, it would be much appreciated!

Click here to lend your support to: A-STAR.ts and make a donation at pledgie.com !