Advertisement
jwow22

AI Object Finder

Jan 4th, 2022
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. public class AIFinder : MonoBehaviour
  2. {
  3.     [SerializeField] public bool enableFinder;
  4.     private AIController aiController;
  5.     private Spawner _spawner;
  6.     private Pathfinding _pathfinding;
  7.  
  8.     // Closest object of type
  9.     public GridObject ClosestObjectOfType(ObjectType objectType)
  10.     {
  11.         if (_spawner.spawnedObjects == null) { return null; }
  12.        
  13.         int index = 0;
  14.         int flag = 0;
  15.         int max = 99999;
  16.         for (int i = 0; i < _spawner.spawnedObjects.Count; i++)
  17.         {
  18.             GridObject gridObject = _spawner.spawnedObjects[i];
  19.             if (gridObject.objectType == objectType)
  20.             {
  21.                 flag = 1;
  22.                 int distanceCost = _pathfinding.TileDistanceCost(gridObject.currentTile, aiController.headBody.gridObject.currentTile);
  23.                 if (distanceCost < max)
  24.                 {
  25.                     max = distanceCost;
  26.                     index = i;
  27.                 }
  28.             }
  29.         }
  30.         if (flag == 0) { return null; }
  31.         return _spawner.spawnedObjects[index];
  32.     }
  33.    
  34.     // Closest object out of all objects
  35.     public GridObject ClosestObject()
  36.     {
  37.         if (_spawner.spawnedObjects == null) { return null; }
  38.        
  39.         int index = 0;
  40.         int max = 99999;
  41.         for (int i = 0; i < _spawner.spawnedObjects.Count; i++)
  42.         {
  43.             GridObject gridObject = _spawner.spawnedObjects[i];
  44.             int distanceCost = _pathfinding.TileDistanceCost(gridObject.currentTile, aiController.headBody.gridObject.currentTile);
  45.             if (distanceCost < max)
  46.             {
  47.                 max = distanceCost;
  48.                 index = i;
  49.             }
  50.         }
  51.         Debug.DrawLine(aiController.headBody.transform.position, _spawner.spawnedObjects[index].currentTile.worldPosition, Color.green, .1f);
  52.         return _spawner.spawnedObjects[index];
  53.     }
  54.  
  55.     // Object with the lowest cost of distance + weight
  56.     public GridObject LowestCostObject()
  57.     {
  58.         if (_spawner.spawnedObjects == null || _spawner.spawnedObjects.Count == 0) { return null; }
  59.  
  60.         int index = 0;
  61.         int max = 99999;
  62.         for (int i = 0; i < _spawner.spawnedObjects.Count; i++)
  63.         {
  64.             if (_spawner.spawnedObjects[i] == null) { continue; }
  65.            
  66.             GridObject gridObject = _spawner.spawnedObjects[i];
  67.            
  68.             int distanceCost = _pathfinding.TileDistanceCost(gridObject.currentTile, aiController.headBody.gridObject.currentTile);
  69.             int totalCost = distanceCost + gridObject.GetWeight();
  70.             if (totalCost < max)
  71.             {
  72.                 max = totalCost;
  73.                 index = i;
  74.             }
  75.         }
  76.         return _spawner.spawnedObjects[index];
  77.     }
  78.  
  79.     private void Start()
  80.     {
  81.         // Object components
  82.         aiController = GetComponent<AIController>();
  83.        
  84.         _pathfinding = FindObjectOfType<Pathfinding>();
  85.         _spawner = FindObjectOfType<Spawner>();
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement