Advertisement
Guest User

Unity C# Unit getPathToDestination

a guest
May 12th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1.  
  2.     public Queue<Vector2Int> getPathToDestination(Vector2Int destinationCoordinates) {
  3.         //Prepare array of tile costs for pathfinding; set default value to maximum
  4.         int uncheckedTileValue = int.MaxValue;
  5.         int[][] tileCosts = new int[Map._Instance.Width][];
  6.         for (int x = 0; x < Map._Instance.Width; x++) {
  7.             tileCosts[x] = new int[Map._Instance.Height];
  8.             for (int y = 0; y < Map._Instance.Height; y++) {
  9.                 tileCosts[x][y] = uncheckedTileValue;
  10.             }
  11.         }
  12.  
  13.         //Prepare to begin pathfinding: starting with unit's current location, calculate out the movement costs for each tile
  14.         tileCosts[location.x][location.y] = 0;
  15.         Queue<Vector2Int> currentNeighbors = new Queue<Vector2Int>();
  16.         Queue<Vector2Int> nextNeighbors = new Queue<Vector2Int>();
  17.        
  18.         //Initialize cycle with starting neighbors
  19.         Vector2Int[] startNeighborCoords = MapCoordinateUtility.getNeighborCoordsOfLocation(destinationCoordinates);
  20.         foreach (Vector2Int startNeighbor in startNeighborCoords) {
  21.             if (canAccessTile(startNeighbor)) {
  22.                 Debug.Log("Start Neighbor: "+startNeighbor.ToString());
  23.                 nextNeighbors.Enqueue(startNeighbor);
  24.             }
  25.         }
  26.  
  27.         int currentDifficulty = 1;
  28.         while (nextNeighbors.Count > 0) {
  29.             Debug.Log("Calculating difficulties for step: "+currentDifficulty);
  30.  
  31.             //Check for break condition: path to destination already found
  32.             if (tileCosts[destinationCoordinates.x][destinationCoordinates.y] != uncheckedTileValue) {
  33.                 Debug.Log("Difficulty for destination has been found: "+ tileCosts[destinationCoordinates.x][destinationCoordinates.y]);
  34.                 break;
  35.             }
  36.  
  37.             //Shift next neighbors over to current neighbors
  38.             currentNeighbors = nextNeighbors;
  39.             nextNeighbors = new Queue<Vector2Int>();
  40.  
  41.             while (currentNeighbors.Count > 0) {
  42.                 //Notate current search's difficulty
  43.                 Vector2Int currentSearchLocation = currentNeighbors.Dequeue();
  44.                 Debug.Log("Notating difficulty for " + currentSearchLocation.ToString());
  45.                 if (tileCosts[currentSearchLocation.x][currentSearchLocation.y] == uncheckedTileValue) {
  46.                     tileCosts[currentSearchLocation.x][currentSearchLocation.y] = currentDifficulty;
  47.                 }
  48.  
  49.                 //Check for adjacent unchecked tiles
  50.                 Vector2Int[] foundNextNeighbors = MapCoordinateUtility.getNeighborCoordsOfLocation(currentSearchLocation);
  51.                 foreach (Vector2Int nextNeighbor in foundNextNeighbors) {
  52.                     if (!canAccessTile(nextNeighbor)) {
  53.                         //Tile not navigable; ignore
  54.                         continue;
  55.                     } else if (tileCosts[nextNeighbor.x][nextNeighbor.y] != uncheckedTileValue) {
  56.                         //Value was already visited; do not add to queue
  57.                         continue;
  58.                     } else {
  59.                         nextNeighbors.Enqueue(nextNeighbor);
  60.                     }
  61.                 }
  62.             }
  63.             //Increase the difficulty of future notations before proceeding to the next search ring
  64.             currentDifficulty++;
  65.         }
  66.  
  67.         //Check if destination has a real difficulty; return null if path not found
  68.         if (tileCosts[destinationCoordinates.x][destinationCoordinates.y] == uncheckedTileValue) {
  69.             return null;
  70.         }
  71.  
  72.         //Construct path, searching from the destination back to the source
  73.         LinkedList<Vector2Int> path = new LinkedList<Vector2Int>();
  74.         path.AddFirst(destinationCoordinates);
  75.         while (!location.Equals(path.First.Value)) {
  76.             int currentPathDistance = tileCosts[path.First.Value.x][path.First.Value.y];
  77.             Vector2Int[] potentialNextSteps = MapCoordinateUtility.getNeighborCoordsOfLocation(path.First.Value);
  78.             foreach (Vector2Int potentialNextStep in potentialNextSteps) {
  79.                 if (!Map._Instance.isTileInMap(potentialNextStep)) {
  80.                     //Neighbor location is not within the map; skip
  81.                     continue;
  82.                 } else if (tileCosts[potentialNextStep.x][potentialNextStep.y] == uncheckedTileValue) {
  83.                     //This tile was not on the list of potential paths; skip
  84.                     continue;
  85.                 } else if (tileCosts[potentialNextStep.x][potentialNextStep.y] < currentPathDistance) {
  86.                     path.AddFirst(potentialNextStep);
  87.                     break;
  88.                 }
  89.             }
  90.         }
  91.  
  92.         //Load path into queue
  93.         Queue<Vector2Int> foundPath = new Queue<Vector2Int>();
  94.         foreach (Vector2Int step in path) {
  95.             foundPath.Enqueue(step);
  96.         }
  97.         return foundPath;
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement