Advertisement
Placido_GDD

Soldier

Jun 28th, 2021
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Soldier : EnemyBase
  6. {
  7.     public bool soldier_InCombat;
  8.     public GameObject soldier_proj;
  9.     protected GameObject projectile;
  10.     public Transform muzzle;
  11.     public float delayBetweenShots;
  12.     public float proj_force;
  13.     public int b_BurstCount;
  14.     protected int burstCounter;
  15.     protected bool isPatrolling;
  16.     protected bool isReloading;
  17.     protected Transform soldier_Target;
  18.     protected Rigidbody2D proj_RB;
  19.    
  20.     public override void Init()
  21.     {
  22.         base.Init();
  23.         Health = base.health;
  24.         isReloading = false;
  25.         soldier_Target = base.player_script.transform;
  26.            if(base.waypoints.Length < 2)
  27.            {
  28.                base.anim.SetTrigger("Idle");
  29.                isPatrolling = false;
  30.                soldier_Target = base.player_script.transform;
  31.            }
  32.            else if(base.waypoints.Length >= 2)
  33.            {
  34.                //Debug.Log("Following waypoint paths");
  35.                isPatrolling = true;
  36.            }
  37.     }
  38.    
  39.      public override void Update()
  40.        {
  41.             SoldierAI_Update();
  42.        }
  43.        
  44.      void SoldierAI_Update()
  45.      {
  46.             if(isPatrolling == true)
  47.             {
  48.                 base.Update();
  49.             }
  50.            
  51.             if(isPatrolling == false)
  52.             {
  53.                 if(base.isDead == false)
  54.                 {
  55.                     base.DeathCheck();
  56.                     base.FaceTarget();
  57.                     base.TargetDistanceCheck();
  58.                     CombatCheck();
  59.                 }
  60.             }
  61.      }
  62.      void CombatCheck()
  63.      {
  64.         if(soldier_Target.position.y >= transform.position.y)
  65.         {
  66.             soldier_InCombat = base.inCombat;
  67.         }
  68.          else if(soldier_Target.position.y < transform.position.y)
  69.          {
  70.             soldier_InCombat = false;
  71.          }
  72.          
  73.          if(soldier_InCombat == true)
  74.                     {
  75.                         ShootAtTarget();                       
  76.                     }
  77.          else if(soldier_InCombat == false)
  78.                     {
  79.                         base.anim.SetBool("InCombat",false);
  80.                         base.anim.SetTrigger("Idle");
  81.                     }
  82.      }
  83.      
  84.      void ShootAtTarget()
  85.      {
  86.        
  87.        
  88.         base.anim.SetBool("IsShooting",true);
  89.         //check if in the state reloading if not instantiate proj and force
  90.         if(isReloading == false)
  91.         {
  92.             while(burstCounter <= b_BurstCount)
  93.             {
  94.                 Debug.Log("Burst Count:" + burstCounter);                          
  95.                 Rigidbody2D proj_RB;
  96.                 GameObject projectile;
  97.                 projectile = Instantiate(soldier_proj,muzzle.position,muzzle.rotation);
  98.                 proj_RB = projectile.GetComponent<Rigidbody2D>();
  99.                 if((base.isFlipped == false)&&(base.isFacingLeft == true))
  100.                 {
  101.                     proj_RB.AddForce(transform.right * -proj_force,ForceMode2D.Impulse);
  102.                 }
  103.                 else if((base.isFlipped == true)&&(base.isFacingLeft == false))
  104.                 {
  105.                     proj_RB.AddForce(transform.right * proj_force,ForceMode2D.Impulse);
  106.                 }
  107.                 burstCounter++;
  108.             }
  109.             base.anim.SetBool("IsShooting",false);
  110.             isReloading = true;
  111.             Debug.Log("Is reloading:" + isReloading);
  112.             StartCoroutine(SoldierFireRate());
  113.            
  114.         }
  115.        
  116.      }
  117.      
  118.      IEnumerator SoldierFireRate()
  119.     {
  120.         yield return new WaitForSeconds(delayBetweenShots);
  121.         isReloading = false;
  122.         base.anim.SetBool("IsShooting",false);
  123.         burstCounter = 0;
  124.         Debug.Log("Is reloading:" + isReloading);
  125.     }
  126.      
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement