Skip to content

Commit

Permalink
FIX converting neighbors foreach into for, and reversing it
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomikoski committed Mar 11, 2018
1 parent ec80d08 commit 7b1b9f4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Assets/[Pathfinding]/Scripts/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void Initialize( GridController targetGridController )

public static List<Vector2Int> GetPath( Vector2Int from, Vector2Int to )
{
closedList.Clear();

int fromIndex = gridController.TilePosToIndex( from.x, from.y );
int toIndex = gridController.TilePosToIndex( to.x, to.y );
Expand All @@ -47,8 +46,9 @@ public static List<Vector2Int> GetPath( Vector2Int from, Vector2Int to )

UpdateNeighbors( currentTile );

foreach ( Tile neighbourPathTile in neighbors )
for ( int i = neighbors.Length - 1; i >= 0; --i )
{
Tile neighbourPathTile = neighbors[i];
if ( neighbourPathTile == null )
continue;

Expand Down Expand Up @@ -85,16 +85,21 @@ public static List<Vector2Int> GetPath( Vector2Int from, Vector2Int to )

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


private static float GetDistance( Tile targetFromTile, Tile targetToTile )
{
return (targetFromTile.PositionX - targetToTile.PositionY) *
(targetFromTile.PositionX - targetToTile.PositionX) +
(targetFromTile.PositionY - targetToTile.PositionY) *
(targetFromTile.PositionY - targetToTile.PositionY);
int fromPositionX = targetFromTile.PositionX;
int toPositionX = targetToTile.PositionX;
int fromPositionY = targetFromTile.PositionY;
int toPositionY = targetToTile.PositionY;
return (fromPositionX - toPositionX) *
(fromPositionX - toPositionX) +
(fromPositionY - toPositionY) *
(fromPositionY - toPositionY);
}

private static Tile[] UpdateNeighbors( Tile targetTile )
Expand Down Expand Up @@ -141,5 +146,6 @@ private static void GetNeighbourPosition( Tile targetTile, NeighborDirection tar
break;
}
}

}
}

0 comments on commit 7b1b9f4

Please sign in to comment.