Skip to content

Commit

Permalink
FIX Static return list, as List<Tile> instead of List<Vector2Int>
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomikoski committed Mar 12, 2018
1 parent dfb86e9 commit d9520b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
8 changes: 5 additions & 3 deletions Assets/[Pathfinding]/Scripts/Gameplay/GameplayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ private void OnUserClickOnTilePosition( int targetX, int targetY )
return;
}

List<Vector2Int> result = Pathfinder.GetPath( selectedTilePosition, clickPosition );
foreach ( Vector2Int vector2Int in result )
List<Tile> result = Pathfinder.GetPath( selectedTilePosition, clickPosition );
int resultCount = result.Count;
for ( int i = 0; i < resultCount; ++i )
{
gridController.SetTileType(vector2Int, TileType.ROAD);
Tile tile = result[i];
tile.SetType( TileType.ROAD );
}

hasFirstNodeSelected = false;
Expand Down
1 change: 1 addition & 0 deletions Assets/[Pathfinding]/Scripts/Grid/GridController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,6 @@ public void Clear()
for ( int i = tiles.Length - 1; i >= 0; i-- )
SetTileType( i, TileType.EMPTY );
}

}
}
12 changes: 5 additions & 7 deletions Assets/[Pathfinding]/Scripts/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public static class Pathfinder
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>();

public static void Initialize( GridController targetGridController )
{
gridController = targetGridController;
openListPriorityQueue = new FastPriorityQueue<Tile>( gridController.GridSizeX * gridController.GridSizeY );
}

public static List<Vector2Int> GetPath( Vector2Int from, Vector2Int to )
public static List<Tile> GetPath( Vector2Int from, Vector2Int to )
{

int fromIndex = gridController.TilePosToIndex( from.x, from.y );
int toIndex = gridController.TilePosToIndex( to.x, to.y );

Expand Down Expand Up @@ -72,10 +72,10 @@ public static List<Vector2Int> GetPath( Vector2Int from, Vector2Int to )
}
}

List<Vector2Int> finalPath = new List<Vector2Int>();
finalPath.Clear();
while ( currentTile != initialTile )
{
finalPath.Add( new Vector2Int( currentTile.PositionX, currentTile.PositionY ) );
finalPath.Add( currentTile );
currentTile = currentTile.Parent;
}

Expand All @@ -99,14 +99,12 @@ private static float GetDistance( Tile targetFromTile, Tile targetToTile )
(fromPositionY - toPositionY);
}

private static Tile[] UpdateNeighbors( Tile targetTile )
private static void UpdateNeighbors( Tile targetTile )
{
neighbors[0] = GetNeighborAtDirection( targetTile, NeighborDirection.LEFT );
neighbors[1] = GetNeighborAtDirection( targetTile, NeighborDirection.TOP );
neighbors[2] = GetNeighborAtDirection( targetTile, NeighborDirection.RIGHT );
neighbors[3] = GetNeighborAtDirection( targetTile, NeighborDirection.DOWN );

return neighbors;
}

private static Tile GetNeighborAtDirection( Tile targetTile, NeighborDirection targetDirection )
Expand Down

0 comments on commit d9520b8

Please sign in to comment.