Advertisement
jwow22

AI Movement

Jan 4th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. [RequireComponent(typeof(AIFinder))]
  2. public class AIController : EntityController
  3. {
  4.     private Map map;
  5.     private Spawner _spawner;
  6.     private Pathfinding pathfinding;
  7.     private AIFinder aiFinder;
  8.  
  9.     private List<Tile> currentTilePath;
  10.     private Direction previousRandomDirection;
  11.    
  12.     // To perform every tick
  13.     public override void MovementTick()
  14.     {
  15.         ResetPathCosts();
  16.         if (headBody == null) { return; }
  17.         // If can find a lowest cost object
  18.         GridObject lowestCostObject = aiFinder.LowestCostObject();
  19.         if (lowestCostObject != null)
  20.         {
  21.             Tile targetTile = lowestCostObject.currentTile;
  22.  
  23.             // Find path to object
  24.             currentTilePath = pathfinding.FindPath(headBody.gridObject.currentTile, targetTile);
  25.  
  26.             // Move along path if it's valid
  27.             if (currentTilePath != null)
  28.             {
  29.                 MoveHeadToTile(currentTilePath[0]);
  30.                 return;
  31.             }
  32.         }
  33.  
  34.         // If can't find an object, it will try to move to a random valid neighbour
  35.         Tile randomValidTile = RandomValidTile();
  36.         if (randomValidTile == null)
  37.         {
  38.             // Move up and die if there's no valid neighbours
  39.             MoveHeadToTile(headBody.gridObject.currentTile.neighbourTiles[(int)Direction.Up]);
  40.             return;
  41.         }
  42.         // Move to random tile if path wasn't found
  43.         MoveHeadToTile(randomValidTile);
  44.     }
  45.    
  46.     // Move head to chosen tile
  47.     private void MoveHeadToTile(Tile tile)
  48.     {
  49.         headBody.MoveToTile(tile);
  50.         EvaluateBodyPositions();
  51.     }
  52.  
  53.     // Randomize until finding valid direction
  54.     private Tile RandomValidTile()
  55.     {
  56.         List<Tile> possibleTiles = new List<Tile>();
  57.         foreach (Tile tile in headBody.gridObject.currentTile.neighbourTiles)
  58.         {
  59.             if (tile.walkable)
  60.             {
  61.                 possibleTiles.Add(tile);
  62.             }
  63.         }
  64.  
  65.         // No possible valid tiles found
  66.         if (possibleTiles.Count == 0) { return null; }
  67.        
  68.         // Return random possible tile
  69.         int randomIndex = Random.Range(0, possibleTiles.Count);
  70.         return possibleTiles[randomIndex];
  71.     }
  72.  
  73.     private void ResetPathCosts()
  74.     {
  75.         for (int x = 0; x < map.size.x; x++)
  76.         {
  77.             for (int y = 0; y < map.size.y; y++)
  78.             {
  79.                 map.tileGrid[x, y].ResetCost();
  80.             }
  81.         }
  82.     }
  83.  
  84.     public override void Start()
  85.     {
  86.         base.Start();
  87.         // Object components
  88.         aiFinder = GetComponent<AIFinder>();
  89.        
  90.         pathfinding = FindObjectOfType<Pathfinding>();
  91.         map = FindObjectOfType<Map>();
  92.         _spawner = FindObjectOfType<Spawner>();
  93.     }
  94.    
  95.     private void OnDrawGizmos()
  96.     {
  97.         if (currentTilePath == null) { return; }
  98.         Gizmos.color = Color.black;
  99.         for (int i = 0; i < currentTilePath.Count; i++)
  100.         {
  101.             if (i == currentTilePath.Count - 1) { return; }
  102.             Gizmos.DrawLine(currentTilePath[i].worldPosition, currentTilePath[i + 1].worldPosition);
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement