Advertisement
Placido_GDD

EnemyBase

Jun 23rd, 2021 (edited)
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyBase : MonoBehaviour
  6. {
  7.      [SerializeField]
  8.       protected float health;
  9.       [SerializeField]
  10.       protected float  speed;
  11.       [SerializeField]
  12.       protected float idleTime;
  13.       [SerializeField]
  14.       protected Transform [] waypoints;
  15.       [SerializeField]
  16.       protected int WpID;
  17.       protected Transform targetWp;
  18.       [SerializeField]
  19.       protected Transform target;
  20.       protected Animator anim;
  21.       protected SpriteRenderer sprite;
  22.       protected bool isHit = false;
  23.       //Player position variable
  24.       [SerializeField]
  25.       protected PlayerControl player_script;
  26.       //Distance check variable.
  27.      [SerializeField]
  28.       protected float d_Check;
  29.       protected float d_ToTarget;
  30.       protected float player_dist;
  31.       [SerializeField]
  32.       protected float aggroDist;
  33.       protected Vector3 t_Direction;
  34.       protected BoxCollider2D boxCollider2D;
  35.       public bool isDead = false;
  36.       [SerializeField]
  37.       protected bool isFacingLeft;
  38.       protected bool inCombat = false;
  39.       protected bool inReverse;
  40.       protected bool isAggroed;
  41.       [SerializeField]
  42.       protected bool isRanged;
  43.       public bool isFlipped;
  44.       public float deathTimer;
  45.       public float Health{get;set;}
  46.       //
  47.       //public GameObject[] _lootTable;
  48.       //public Vector3 _dropOffset;
  49.       //protected Diamond _diamondScript;
  50.    
  51.     public virtual void Init()
  52.      {
  53.          anim = GetComponentInChildren<Animator>();
  54.          sprite = GetComponentInChildren<SpriteRenderer>();
  55.          boxCollider2D = GetComponent<BoxCollider2D>();
  56.          inReverse = false;
  57.          isFlipped = false;
  58.          WpID = 0;
  59.          if(waypoints.Length >= 2)
  60.          {
  61.              targetWp = waypoints[WpID];
  62.          }
  63.          else if(waypoints.Length < 2)
  64.          {
  65.              Debug.Log("Not Enough waypoints");
  66.          }
  67.          player_script = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerControl>();
  68.      }
  69.     // Start is called before the first frame update
  70.     void Start()
  71.     {
  72.          Init();
  73.     }
  74.  
  75.     // Update is called once per frame
  76.     public virtual void Update()
  77.     {
  78.          /* if((anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))&& (anim.GetBool("InCombat") == false))
  79.           {
  80.               return;
  81.           } */
  82.         AILogic(); 
  83.         DeathCheck();
  84.            
  85.     }
  86.    
  87.    
  88.     void AILogic()
  89.     {
  90.         //runs code based on the bool variable "isRanged"
  91.         if(isRanged == false)
  92.         {
  93.             MeleeAILogic();
  94.         }
  95.         else if(isRanged == true)
  96.         {
  97.             RangeAILogic();
  98.         }
  99.        
  100.     }
  101.     void RangeAILogic()
  102.     {
  103.         if(isDead == false)
  104.         {
  105.             FaceTarget();
  106.             TargetDistanceCheck();
  107.             if(inCombat == false)
  108.             {
  109.                 RangedPatrolLogic();
  110.             }
  111.         }
  112.     }
  113.     void MeleeAILogic()
  114.     {
  115.         if(isDead == false)
  116.             {
  117.                 if((inCombat == false)&&(waypoints.Length >= 2))
  118.                 {
  119.                     if(isAggroed == false)
  120.                     {
  121.                         if(isDead == false)
  122.                         {
  123.                             PatrolLogic();
  124.                         }
  125.                        
  126.                         anim.SetBool("InCombat",false);
  127.                         target = targetWp;
  128.                     }
  129.                    
  130.                 }
  131.                 else if(inCombat == true)
  132.                 {
  133.                     target = player_script.transform;
  134.                     anim.SetBool("InCombat",true);
  135.                     FaceTarget();
  136.                                        
  137.                 }
  138.                 if((isAggroed == true)&&(inCombat == false))
  139.                 {
  140.                     AggroLogic();
  141.                 }
  142.                 FaceTarget();
  143.                 TargetDistanceCheck();
  144.                
  145.             }
  146.     }
  147.     public virtual void DeathCheck()
  148.      {
  149.          if(Health == 0)
  150.          {
  151.              isDead = true;
  152.          }
  153.          if(isDead == true)
  154.          {
  155.              //boxCollider2D.enabled = false;
  156.              anim.SetTrigger("Death");
  157.              Destroy(this.gameObject,deathTimer);
  158.              //play death anim
  159.              //destroy this game object
  160.          }
  161.          
  162.      }
  163.      
  164.      void AggroLogic()
  165.      {
  166.          FaceTarget();
  167.          target = player_script.transform;
  168.          if(transform.position.x != target.position.x)
  169.          {
  170.              transform.position = Vector3.MoveTowards(transform.position,target.position,speed * Time.deltaTime);
  171.              anim.SetBool("IsWalking",true);       
  172.          }
  173.      }
  174.     public void TargetDistanceCheck()
  175.     {
  176.         player_dist = Vector3.Distance(transform.localPosition,player_script.transform.localPosition);
  177.         //Debug.Log("distance to player:" + player_dist);
  178.         if(player_dist <= d_Check)
  179.         {
  180.             //Debug.Log("Player target found");
  181.             target = player_script.transform;
  182.             FaceTarget();
  183.             inCombat = true;
  184.             anim.SetBool("InCombat",true);
  185.         }
  186.         else if(player_dist > d_Check)
  187.         {
  188.             //Debug.Log("Target lost");
  189.             //target = targetWp;
  190.             FaceTarget();
  191.             inCombat = false;
  192.             anim.SetBool("InCombat",false);
  193.         }
  194.        
  195.         AggroDistanceCheck(player_dist);
  196.        
  197.     }
  198.    
  199.     void AggroDistanceCheck(float t_distance)
  200.     {
  201.        
  202.         if(t_distance <= aggroDist)
  203.         {          
  204.             isAggroed = true;
  205.             //Debug.Log("Aggro'd:" + isAggroed);
  206.         }
  207.         else if(t_distance > aggroDist)
  208.         {
  209.             isAggroed = false;
  210.             //Debug.Log("Aggro'd:" + isAggroed);
  211.         }
  212.         d_ToTarget = t_distance;
  213.     }
  214.     void RangedPatrolLogic()
  215.     {
  216.        
  217.     }
  218.     void PatrolLogic()
  219.     {
  220.          
  221.             if((transform.position.x != targetWp.position.x)&&(isHit == false))
  222.             {
  223.                 if(inCombat == false)
  224.                 {
  225.                     anim.SetBool("IsWalking",true);
  226.                     transform.position = Vector3.MoveTowards(transform.position,targetWp.position,speed * Time.deltaTime);
  227.                 }
  228.             }
  229.             if((transform.position.x == targetWp.position.x)&&(isHit == false))
  230.             {
  231.                 if((inCombat == false)&&(inReverse == false))
  232.                 {
  233.                     if(WpID < waypoints.Length - 1)
  234.                     {
  235.                         WpID++;
  236.                         targetWp = waypoints[WpID];
  237.                         target = targetWp;
  238.                        
  239.                     }
  240.                     else if(WpID == waypoints.Length - 1)
  241.                     {
  242.                         inReverse = true;
  243.                     }
  244.                 }
  245.                 else if((inCombat == false)&&(inReverse == true))
  246.                 {
  247.                     if(WpID > 0)
  248.                     {
  249.                         WpID --;
  250.                         targetWp = waypoints[WpID];
  251.                         target = targetWp;
  252.    
  253.                     }
  254.                     else if(WpID == 0)
  255.                     {
  256.                         inReverse = false;
  257.                     }
  258.                 }
  259.             }
  260.            
  261.            
  262.            
  263.     }
  264.        
  265.     public virtual void FaceTarget()
  266.         {
  267.             //determine where the target is and it's distance to this object
  268.             //determine if the target is either on the left or right of this object
  269.             //check if this object's sprite is facing left
  270.             //flip the sprite to face target
  271.             if((targetWp != null)&&(isAggroed == false))
  272.             {
  273.                 target = targetWp;
  274.             }
  275.             else if((targetWp == null)||(isAggroed == true))
  276.             {
  277.                 target = player_script.transform;
  278.             }
  279.             t_Direction = target.localPosition - transform.localPosition;
  280.             //Debug.Log("target direction:" + t_Direction);
  281.             if(isFacingLeft == true)
  282.             {
  283.                 if(t_Direction.x > 0)
  284.                 {
  285.                     //180 rotation
  286.                     isFlipped = true;
  287.                     Vector3 eulerRotation = transform.rotation.eulerAngles;
  288.                     transform.rotation = Quaternion.Euler(eulerRotation.x,180,eulerRotation.z);
  289.                 }
  290.                 else if(t_Direction.x < 0)
  291.                 {
  292.                     //0 rotation
  293.                     isFlipped = false;
  294.                     Vector3 eulerRotation = transform.rotation.eulerAngles;
  295.                     transform.rotation = Quaternion.Euler(eulerRotation.x,0,eulerRotation.z);
  296.                 }
  297.             }
  298.             else if(isFacingLeft == false)
  299.             {
  300.                 if(t_Direction.x > 0)
  301.                 {
  302.                     //0 rotation
  303.                     isFlipped = false;
  304.                     Vector3 eulerRotation = transform.rotation.eulerAngles;
  305.                     transform.rotation = Quaternion.Euler(eulerRotation.x,0,eulerRotation.z);
  306.                 }
  307.                 else if(t_Direction.x < 0)
  308.                 {
  309.                     //180 rotation
  310.                     isFlipped = true;
  311.                     Vector3 eulerRotation = transform.rotation.eulerAngles;
  312.                     transform.rotation = Quaternion.Euler(eulerRotation.x,180,eulerRotation.z);
  313.                 }
  314.             }
  315.             //float distance = Vector3.Distance(transform.localPosition,player_script.transform.localPosition);
  316.             //Vector3 direction = player_script.transform.localPosition - transform.localPosition;
  317.            
  318.         }
  319.     public void Damage()
  320.     {
  321.         if(isDead != true)
  322.         {
  323.             //Enemy takes damage
  324.         }
  325.     }
  326.    
  327. }
  328.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement