Placido_GDD

MVC EnemyData

Feb 10th, 2022 (edited)
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. public class EnemyData : MonoBehaviour
  6. {
  7.     public float moveSpeed;
  8.     public float health;
  9.     public float hpThreshold;
  10.     public Vector3 currentDestination;
  11.     public NavMeshAgent agent;
  12.     public EnemyDataContainer EDC;
  13.     public int logicID;
  14.     public Vector3 targetloc;
  15.     public float rotationSpeed;
  16.     public float visDist = 20.0f;
  17.     public float visAngle = 30.0f;
  18.     public float detectRange = 10.0f;
  19.     public bool rotateClockwise;
  20.     public bool isRotating;
  21.     public float rotateAngleAmt;
  22.     public float rotateDur;
  23.     public Quaternion newRoT;
  24.     public Quaternion reverseRoT;
  25.     //
  26.  
  27.     //
  28.     private void Start()
  29.     {
  30.         agent = GetComponent<NavMeshAgent>();
  31.         EDC = GameObject.FindGameObjectWithTag("EDC").GetComponent<EnemyDataContainer>();
  32.         EDC.enemies.Add(this.gameObject);
  33.         EDC.enemyData.Add(this);
  34.         newRoT =Quaternion.Euler(new Vector3(0,rotateAngleAmt,0));
  35.         reverseRoT = Quaternion.Euler(new Vector3(0, -rotateAngleAmt, 0));
  36.     }
  37.  
  38.     public void Seek(Vector3 location, NavMeshAgent agent)
  39.     {
  40.         agent.SetDestination(location);
  41.     }
  42.  
  43.     Vector3 wanderTarget = Vector3.zero;
  44.     public void Wander(GameObject thisObj,NavMeshAgent agent)
  45.     {
  46.         float wanderRadius = 10;
  47.         float wanderDistance = 10;
  48.         float wanderJitter = 1;
  49.         wanderTarget += new Vector3(Random.Range(-1.0f, 1.0f) * wanderJitter, 0, Random.Range(-1.0f, 1.0f) * wanderJitter);
  50.         wanderTarget.Normalize();
  51.         wanderTarget *= wanderRadius;
  52.  
  53.         Vector3 targetLocal = wanderTarget + new Vector3(0,0,wanderDistance);
  54.         Vector3 targetWorld = thisObj.transform.InverseTransformVector(targetLocal);
  55.         Seek(targetWorld, agent);
  56.     }
  57.  
  58.     public void Evade(GameObject target,GameObject thisPos,float agentSpeed,float playerSpeed,NavMeshAgent agent,PlayerControl pc)
  59.     {
  60.         Vector3 targetDir = target.transform.position - thisPos.transform.position;
  61.         float lookAhead = targetDir.magnitude / (agentSpeed + playerSpeed);
  62.         Flee(target.transform.position + target.transform.forward * lookAhead, agent);
  63.     }
  64.     public void Flee(Vector3 location, NavMeshAgent agent)
  65.     {
  66.         Vector3 fleeVector = location - this.transform.position;
  67.         agent.SetDestination(this.transform.position - fleeVector);
  68.     }
  69.  
  70.     public void Pursue(GameObject target, GameObject thisPos, float agentSpeed, float playerSpeed, NavMeshAgent agent, PlayerControl pc)
  71.     {
  72.         Vector3 targetDir = target.transform.position - thisPos.transform.position;
  73.         float relativeHeading = Vector3.Angle(thisPos.transform.forward, thisPos.transform.TransformVector(target.transform.forward));
  74.         float toTarget = Vector3.Angle(thisPos.transform.forward, thisPos.transform.TransformVector(targetDir));
  75.         if ((toTarget > 90 && relativeHeading < 20) || (pc.playerData.velocity.x != 0.0f && pc.playerData.velocity.z != 0.0f))
  76.         {
  77.             //Debug.Log("Seeking");
  78.             Seek(target.transform.position, agent);
  79.             return;
  80.         }
  81.         //Debug.Log("Pursuing");
  82.         float lookAhead = targetDir.magnitude / (agentSpeed + playerSpeed);
  83.         Seek(target.transform.position + target.transform.forward * lookAhead,agent);
  84.     }
  85.  
  86.     public void Hide(GameObject target,GameObject thisObj,NavMeshAgent agent)
  87.     {
  88.         float dist = Mathf.Infinity;
  89.         Vector3 chosenSpot = Vector3.zero;
  90.         Vector3 chosenDir = Vector3.zero;
  91.         GameObject chosenGO = WorldData.Instance.GetHidingSpots()[0];
  92.         for (int i = 0; i < WorldData.Instance.GetHidingSpots().Length; i++)
  93.         {
  94.             Vector3 hideDir = WorldData.Instance.GetHidingSpots()[i].transform.position - target.transform.position;
  95.             Vector3 hidePos = WorldData.Instance.GetHidingSpots()[i].transform.position + hideDir.normalized * 5;
  96.             if (Vector3.Distance(thisObj.transform.position, hidePos) < dist)
  97.             {
  98.                 chosenSpot = hidePos;
  99.                 chosenDir = hideDir;
  100.                 chosenGO = WorldData.Instance.GetHidingSpots()[i];
  101.                 dist = Vector3.Distance(thisObj.transform.position, hidePos);
  102.             }
  103.         }
  104.  
  105.         Collider hideCol = chosenGO.GetComponent<Collider>();
  106.         Ray backRay = new Ray(chosenSpot, -chosenDir.normalized);
  107.         RaycastHit info;
  108.         float distance = 100.0f;
  109.         hideCol.Raycast(backRay, out info, distance);
  110.  
  111.         Seek(info.point + chosenDir.normalized * 5,agent);
  112.     }
  113.  
  114.     public void Scan(GameObject thisObj,Quaternion newRoT,Quaternion reverseRoT,float rotDur,bool rotClockwise,bool isRotating)
  115.     {
  116.         if (rotateClockwise == true && isRotating == true)
  117.         {
  118.             rotateObject(thisObj, newRoT, rotDur);
  119.         }
  120.         else if (rotateClockwise == false && isRotating == true)
  121.         {
  122.             reverseRotation(thisObj, reverseRoT, rotDur);
  123.         }
  124.     }
  125.  
  126.    
  127.     void rotateObject(GameObject objToRotate, Quaternion newRoT, float duration)
  128.     {
  129.         Quaternion currentRoT = objToRotate.transform.rotation;
  130.         float counter = 0;
  131.         if (counter < duration)
  132.         {
  133.             counter += Time.deltaTime;
  134.             objToRotate.transform.rotation = Quaternion.Lerp(currentRoT, newRoT, counter / duration);
  135.         }
  136.     }
  137.    
  138.     void reverseRotation(GameObject objToRotate, Quaternion newRoT, float duration)
  139.     {
  140.         Quaternion currentRoT = objToRotate.transform.rotation;
  141.         float counter = 0;
  142.         if (counter < duration)
  143.         {
  144.             counter += Time.deltaTime;
  145.             objToRotate.transform.rotation = Quaternion.Lerp(currentRoT, newRoT, counter / duration);
  146.         }
  147.     }
  148.     public bool CanSeeTarget(GameObject target,GameObject thisObj)
  149.     {
  150.         Vector3 direction = target.transform.position - thisObj.transform.position;
  151.         float angle = Vector3.Angle(direction,thisObj.transform.forward);
  152.         if (direction.magnitude < visDist && angle < visAngle)
  153.         {
  154.             direction.y = 0;
  155.             if (direction.magnitude > detectRange)
  156.             {
  157.                 return true;
  158.             }
  159.             else
  160.             {
  161.                 return false;
  162.             }
  163.         }
  164.         else
  165.         {
  166.             return false;
  167.         }
  168.        
  169.     }
  170. }
  171.  
Add Comment
Please, Sign In to add comment