Skip to content

Commit

Permalink
FIX removing closeList and using a flag in the Tile to check if is be…
Browse files Browse the repository at this point in the history
…en used in the current search
  • Loading branch information
brunomikoski committed Mar 13, 2018
1 parent 047cd72 commit a2f9ab2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Assets/[Pathfinding]/Scripts/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public static class Pathfinder
private static GridController gridController;

private static FastPriorityQueue<Tile> openListPriorityQueue;
private static HashSet<Tile> closedList = new HashSet<Tile>();
private static Tile[] neighbors = new Tile[4];
private static List<Tile> finalPath = new List<Tile>();

Expand All @@ -37,7 +36,7 @@ public static List<Tile> GetPath( Vector2Int from, Vector2Int to )
{
currentTile = openListPriorityQueue.Dequeue();

closedList.Add( currentTile );
currentTile.ToggleInCloseList( true );

if ( currentTile == destinationTile )
break;
Expand All @@ -50,7 +49,7 @@ public static List<Tile> GetPath( Vector2Int from, Vector2Int to )
if ( neighbourPathTile == null )
continue;

if ( closedList.Contains( neighbourPathTile ) )
if ( neighbourPathTile.InCloseList)
continue;

bool isAtOpenList = openListPriorityQueue.Contains( neighbourPathTile );
Expand All @@ -76,16 +75,19 @@ public static List<Tile> GetPath( Vector2Int from, Vector2Int to )
}

finalPath.Clear();
while ( currentTile != initialTile )
while ( currentTile.Parent != null && currentTile != initialTile )
{
finalPath.Add( currentTile );
currentTile.ToggleInCloseList( false );

Tile cachedCurrenTile = currentTile;
currentTile = currentTile.Parent;
cachedCurrenTile.SetParent( null );
}

finalPath.Add( initialTile );

openListPriorityQueue.Clear();
closedList.Clear();
return finalPath;
}

Expand Down

0 comments on commit a2f9ab2

Please sign in to comment.