EgonMilanVotrubec

FindNearestEnemy.cs

Jun 11th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System.Threading.Tasks;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine.AI;
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Linq;
  8.  
  9. public enum AgentType
  10. {
  11.     None = 0,
  12.     Player,
  13.     Enemy
  14. }
  15.  
  16. public interface IAgent
  17. {
  18.     AgentType AgentType { get; }
  19.     GameObject gameObject { get; }
  20.     Vector3 Position { get; }
  21. }
  22.  
  23. public struct AgentHitPair
  24. {
  25.     public IAgent Agent1;
  26.     public IAgent Agent2;
  27. }
  28.  
  29.  
  30. public class FindNearestEnemy : MonoBehaviour
  31. {
  32.     public static FindNearestEnemy instance;
  33.  
  34.     private Teamup team;
  35.     private List<GameObject> Enemies;
  36.  
  37.     public float DistanceBetweenAgents;
  38.  
  39.     public bool Searching { get; private set; }
  40.  
  41.     private List<IAgent> Agents;
  42.  
  43.     private List<AgentHitPair> AgentHits;
  44.  
  45.     private float DistanceBetweenAgentsSquare;
  46.  
  47.     public bool EnemySearched = false;
  48.     private NavMeshAgent nma;
  49.  
  50.     public bool NearestTarget = false;
  51.  
  52.     private void Awake ( )
  53.     {
  54.         team = GetComponent<Teamup> ( );
  55.         nma = GetComponent<NavMeshAgent> ( );
  56.  
  57.         if ( instance == null )
  58.         {
  59.             instance = this;
  60.         }
  61.     }
  62.  
  63.  
  64.     private void Start ( )
  65.     {
  66.         FindEnemies ( );
  67.     }
  68.  
  69.  
  70.     public void FindEnemies ( )
  71.     {
  72.  
  73.         Agents = new List<IAgent> ( );
  74.         var Allenemies = FindObjectsOfType<Teamup> ( );
  75.         string nametolookfor = GetComponent<Teamup> ( ).OppositeTeamTag;
  76.         for ( int i = 0; i < Allenemies.Length; i++ )
  77.         {
  78.             if ( Allenemies [ i ].gameObject.GetComponent<Teamup> ( ).OppositeTeamTag == nametolookfor )
  79.             {
  80.                 Agents.Add ( ( IAgent ) Allenemies [ i ] );
  81.             }
  82.         }
  83.         AgentHits = new List<AgentHitPair> ( );
  84.         DistanceBetweenAgentsSquare = DistanceBetweenAgents * DistanceBetweenAgents;
  85.         Searching = true;
  86.  
  87.         Search ( );
  88.  
  89.     }
  90.     private async void Search ( )
  91.     {
  92.         do
  93.         {
  94.             AgentHits.Clear ( );
  95.  
  96.             // Do a brute force distance check between each agent.
  97.             for ( int i = 0; i < Agents.Count; i++ )
  98.             {
  99.                 for ( int j = 0; j < Agents.Count; j++ )
  100.                 {
  101.                     if ( Agents [ i ] == Agents [ j ] )
  102.                         continue;
  103.  
  104.                     var magnitude = ( Agents [ i ].Position - Agents [ j ].Position ).sqrMagnitude;
  105.                     if ( magnitude <= DistanceBetweenAgentsSquare )
  106.                     {
  107.                         // The two agents are closer than DistanceBetweenAgentsSquare distance to each other.
  108.                         AgentHits.Add ( new AgentHitPair { Agent1 = Agents [ i ], Agent2 = Agents [ j ] } );
  109.                     }
  110.                 }
  111.             }
  112.  
  113.             // AgentHits now holds the Agents that are "close" to each other.
  114.             if ( AgentHits.Count > 0 )
  115.             {
  116.                 StringBuilder output = new StringBuilder ( );
  117.                 for ( int i = 0; i < AgentHits.Count; i++ )
  118.                 {
  119.                     output.AppendLine ( $"AgentHits : {AgentHits [ i ].Agent1.gameObject.name} [{AgentHits [ i ].Agent1.AgentType}] is close to {AgentHits [ i ].Agent2.gameObject.name} [{AgentHits [ i ].Agent2.AgentType}]." );
  120.                     EnemySearched = true;
  121.                     Vector3 dir = AgentHits [ i ].Agent2.gameObject.transform.position - this.transform.position;
  122.                     dir.y = 0;
  123.                     //nma.destination = AgentHits[i].Agent2.GameObject.transform.position;
  124.                     GetComponent<Teamup> ( ).Target = AgentHits [ i ].Agent2.gameObject.transform;
  125.                     NearestTarget = true;
  126.                 }
  127.             }
  128.  
  129.             await Task.Delay ( 1000 );
  130.         }
  131.         while ( Searching );
  132.     }
  133.  
  134.  
  135.     private void OnDisable ( )
  136.     {
  137.         Searching = false;
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment