Advertisement
jwow22

A* Pathfinding

Jan 4th, 2022
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. public class Pathfinding : MonoBehaviour
  2. {
  3.     [NonSerialized] private Vector2Int startTilePos;
  4.     [NonSerialized] private Vector2Int targetTilePos;
  5.    
  6.     private List<Tile> pathList;
  7.    
  8.     public List<Tile> FindPath(Tile startTile, Tile targetTile)
  9.     {
  10.         if (startTile == targetTile)
  11.             return null;
  12.  
  13.         List<Tile> openList = new List<Tile>();
  14.         HashSet<Tile> closedList = new HashSet<Tile>();
  15.         openList.Add(startTile);
  16.  
  17.         while (openList.Count > 0)
  18.         {
  19.             // Iterate through Open List to get tile with lowest F Cost
  20.             Tile currentTile = openList[0];
  21.             for (int i = 1; i < openList.Count; i++)
  22.             {
  23.                 if (openList[i].fCost < currentTile.fCost ||
  24.                     openList[i].fCost == currentTile.fCost && openList[i].hCost < currentTile.hCost)
  25.                 {
  26.                     currentTile = openList[i];
  27.                 }
  28.             }
  29.            
  30.             // Moving tile from openList to closedList
  31.             openList.Remove(currentTile);
  32.             closedList.Add(currentTile);
  33.            
  34.             // If reached target tile, path was found
  35.             if (currentTile == targetTile)
  36.             {
  37.                 return BacktrackPath(startTile, targetTile);
  38.             }
  39.  
  40.             // Evaluate all neighbour tiles
  41.             foreach (Tile neighbourTile in currentTile.neighbourTiles)
  42.             {
  43.                 if (closedList.Contains(neighbourTile) || !neighbourTile.walkable)
  44.                     continue;
  45.  
  46.                 int movementCost = currentTile.gCost + TileDistanceCost(currentTile, neighbourTile);
  47.                
  48.                 // Neighbour tile is possible path?
  49.                 if (movementCost < neighbourTile.gCost || !openList.Contains(neighbourTile))
  50.                 {
  51.                     // Update cost values
  52.                     neighbourTile.gCost = movementCost;
  53.                     startTile.hCost = TileDistanceCost(currentTile, targetTile);
  54.                     neighbourTile.parentTile = currentTile;
  55.                    
  56.                     // Add neighbour tile to open list
  57.                     if (!openList.Contains(neighbourTile))
  58.                     {
  59.                         openList.Add(neighbourTile);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         return null;
  65.     }
  66.    
  67.     private List<Tile> BacktrackPath(Tile startTile, Tile targetTile)
  68.     {
  69.         List<Tile> tilePath = new List<Tile>();
  70.         Tile currentTile = targetTile;
  71.        
  72.         // Add saved parents of each tile to tilePath
  73.         while (currentTile != startTile)
  74.         {
  75.             tilePath.Add(currentTile);
  76.             currentTile = currentTile.parentTile;
  77.         }
  78.         tilePath.Reverse();
  79.         return tilePath;
  80.     }
  81.    
  82.     public int TileDistanceCost(Tile tileA, Tile tileB)
  83.     {
  84.         int cost = 0;
  85.         if (tileA == null || tileB == null)
  86.             return cost;
  87.  
  88.         int dx = Math.Abs(tileA.gridPosition.x - tileB.gridPosition.x);
  89.         int dy = Math.Abs(tileA.gridPosition.y - tileB.gridPosition.y);
  90.         int dTotal = dx + dy;
  91.  
  92.         cost = dTotal;
  93.         return cost;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement