@@ -28,6 +28,12 @@ function pathTo(node){
2828 return path . reverse ( ) ;
2929}
3030
31+ function getHeap ( ) {
32+ return new BinaryHeap ( function ( node ) {
33+ return node . f ;
34+ } ) ;
35+ }
36+
3137var astar = {
3238 init : function ( graph ) {
3339 for ( var i = 0 , len = graph . nodes . length ; i < len ; ++ i ) {
@@ -40,11 +46,6 @@ var astar = {
4046 node . parent = null ;
4147 }
4248 } ,
43- heap : function ( ) {
44- return new BinaryHeap ( function ( node ) {
45- return node . f ;
46- } ) ;
47- } ,
4849
4950 /**
5051 * Perform an A* Search on a graph given a start and end node.
@@ -64,7 +65,7 @@ var astar = {
6465 var heuristic = options . heuristic || astar . heuristics . manhattan ,
6566 closest = options . closest || false ;
6667
67- var openHeap = astar . heap ( ) ,
68+ var openHeap = getHeap ( ) ,
6869 closestNode = start ; // set the start node to be the closest if required
6970
7071 start . h = heuristic ( start , end ) ;
@@ -87,10 +88,10 @@ var astar = {
8788 // Find all neighbors for the current node.
8889 var neighbors = graph . neighbors ( currentNode ) ;
8990
90- for ( var i = 0 , il = neighbors . length ; i < il ; ++ i ) {
91+ for ( var i = 0 , il = neighbors . length ; i < il ; ++ i ) {
9192 var neighbor = neighbors [ i ] ;
9293
93- if ( neighbor . closed || neighbor . isWall ( ) ) {
94+ if ( neighbor . closed || neighbor . isWall ( ) ) {
9495 // Not a valid node to process, skip to next neighbor.
9596 continue ;
9697 }
@@ -100,7 +101,7 @@ var astar = {
100101 var gScore = currentNode . g + neighbor . getCost ( currentNode ) ,
101102 beenVisited = neighbor . visited ;
102103
103- if ( ! beenVisited || gScore < neighbor . g ) {
104+ if ( ! beenVisited || gScore < neighbor . g ) {
104105
105106 // Found an optimal (so far) path to this node. Take score for node to see how good it is.
106107 neighbor . visited = true ;
@@ -174,6 +175,7 @@ function Graph(gridIn, options) {
174175 }
175176 }
176177}
178+
177179Graph . prototype . neighbors = function ( node ) {
178180 var ret = [ ] ,
179181 x = node . x ,
0 commit comments