Advertisement
Placido_GDD

BaseAILogic

Jan 3rd, 2022
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public abstract class BaseAILogic : MonoBehaviour
  6. {
  7.     public ShipHealth ship_Hp;
  8.     public ShipHealth enemy_Hp;
  9.     public ShipHealth ally_Hp;
  10.     public float moveSpeed;
  11.     public float rotSpeed;
  12.     public float hpFleeThreshold;
  13.     public float stoppingDist;
  14.     public float fleeDist;
  15.     protected float currDistP;
  16.     protected float currDistD;
  17.     protected Vector2 destination;
  18.     public GameObject shipAI;
  19.     public GameObject Player;
  20.     public GameObject Ally;
  21.     public bool isFleeing;
  22.     public bool isMoving;
  23.     public int LogicID;
  24.     public Vector3 startLoc;
  25.     // Start is called before the first frame update
  26.  
  27.     public virtual void Init()
  28.     {
  29.         Debug.Log("Initializing AI values");
  30.         startLoc = transform.position;
  31.         ship_Hp = shipAI.GetComponent<ShipHealth>();
  32.         Player = GameObject.FindGameObjectWithTag("Player_C");
  33.         enemy_Hp = Player.GetComponent<ShipHealth>();
  34.         if (Ally != null)
  35.         {
  36.             ally_Hp = Ally.GetComponent<ShipHealth>();
  37.         }
  38.     }
  39.     public float getDistance(Vector2 targetPos)
  40.     {
  41.         float distToTarget = Vector2.Distance(this.gameObject.transform.position, targetPos);
  42.         return distToTarget;
  43.     }
  44.  
  45.     public virtual void MoveToDestination(float distToPos,Vector2 targetPos,float stoppingDist)
  46.     {
  47.         if (distToPos >= stoppingDist)
  48.         {
  49.             this.transform.Translate((moveSpeed * 0.1f) * Time.deltaTime, 0.0f,0.0f);
  50.             LookAtTarget((Vector3)targetPos);
  51.         }
  52.     }
  53.  
  54.     public virtual void Flee(Vector2 location,float fleeDist)
  55.     {
  56.         Vector2 fleeVector = (location - (Vector2)this.transform.position) * fleeDist;
  57.         destination = (Vector2)this.transform.position - fleeVector;
  58.         MoveToDestination(getDistance(destination), destination, 0.0f);
  59.     }
  60.     void LookAtTarget(Vector3 targetPos)
  61.     {
  62.         Vector3 direction = targetPos - this.transform.position;
  63.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  64.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  65.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, (rotSpeed * 0.01f) * Time.deltaTime);
  66.     }
  67.  
  68.     Vector2 wanderTarget = Vector2.zero;
  69.     public void Wander(float wanderRadius,float wanderDistance,float wanderJitter)
  70.     {
  71.         wanderTarget += new Vector2(Random.Range(-1.0f, 1.0f) * wanderJitter,
  72.                                     Random.Range(-1.0f, 1.0f) * wanderJitter);
  73.         wanderTarget.Normalize();
  74.         wanderTarget *= wanderRadius;
  75.  
  76.         Vector2 targetLocal = wanderTarget + new Vector2(wanderDistance, 0);
  77.         Vector2 targetWorld = this.gameObject.transform.InverseTransformVector(targetLocal);
  78.         MoveToDestination(getDistance(targetWorld),targetWorld,0.0f);
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement