Advertisement
GoodNoodle

Fighter

Jul 1st, 2021 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Fighter : MonoBehaviour, IAction, IModifier
  6. {
  7.     public Fighter instance;
  8.    public Health target;
  9.    
  10.     public Animator anim;
  11.     public float timeBetweenAttacks = 1f;
  12.     public  float timeSinceLastAttack = 0;
  13.    
  14.     public PlayerWeapon DefultWeapon = null;
  15.     [SerializeField] Transform handTrans;
  16.  
  17.  
  18.     public PlayerWeapon currentwep = null;
  19.     public string defultWeaponName = "Staff";
  20.  
  21.     public CapsuleCollider Capcolider;
  22.     private void Start()
  23.     {
  24.         instance = this;
  25.         anim = GetComponent<Animator>();
  26.         PlayerWeapon weapon = UnityEngine.Resources.Load<PlayerWeapon>(defultWeaponName);
  27.         EquipWeapon(currentwep);
  28.     }
  29.     private void Update()
  30.     {
  31.         timeSinceLastAttack += Time.deltaTime;
  32.         if (target == null) return;
  33.         if (target.isDead) return;
  34.            
  35.        
  36.         if (!IsInRange())
  37.         {
  38.             GetComponent<Mover>().MoveTo(target.transform.position);
  39.         } else
  40.         {
  41.             GetComponent<Mover>().Cancel();
  42.             Attack(gameObject);
  43.             AttackBehavior();
  44.  
  45.  
  46.         }
  47.     }
  48.  
  49.     public void EquipWeapon(PlayerWeapon weapon)
  50.     {
  51.         currentwep = weapon;
  52.         Animator animator = GetComponent<Animator>();
  53.         weapon.Spawn(handTrans, animator);
  54.     }
  55.  
  56.     public Health GetTar()
  57.     {
  58.         return target;
  59.     }
  60.  
  61.     private void AttackBehavior()
  62.     {
  63.         transform.LookAt(transform.position);
  64.  
  65.         if(timeSinceLastAttack > timeBetweenAttacks)
  66.         {
  67.             anim.ResetTrigger("StopAttack");
  68.             anim.SetTrigger("Attack");
  69.            
  70.             timeSinceLastAttack = 0;
  71.            
  72.         }
  73.  
  74.  
  75.        
  76.     }
  77.     public bool IsInRange()
  78.     {
  79.       return  Vector3.Distance(transform.position, target.transform.position) < currentwep.GetRange();
  80.     }
  81.  
  82.     public bool CanAttack(GameObject combatTar)
  83.     {
  84.        
  85.         if (combatTar == null)
  86.         {
  87.             return false;
  88.         }
  89.         Health tarToTest = combatTar.GetComponent<Health>();
  90.         return tarToTest != null;
  91.     }
  92.     public void Attack(GameObject combattarget)
  93.     {
  94.         GetComponent<ActionScedular>().StartAction(this);
  95.         target = combattarget.GetComponent<Health>();
  96.     }
  97.  
  98.     public void Cancel()
  99.     {
  100.         StopAttack();
  101.         target = null;
  102.     }
  103.  
  104.     void StopAttack()
  105.     {
  106.         GetComponent<Animator>().ResetTrigger("Attack");
  107.        GetComponent<Animator>().SetTrigger("StopAttack");
  108.     }
  109.  
  110.     void Hit ()
  111.     {
  112.        
  113.         if (target == null) { return; }
  114.  
  115.        
  116.         float damage = GetComponent<BaseStats>().GetStat(Stat.Damage);
  117.         if (currentwep.HasProjectile())
  118.         {
  119.             currentwep.LaunchProjectile(handTrans, target,  gameObject, damage);
  120.         } else
  121.         {
  122.             Debug.Log("Hit" + target, target);
  123.             target.TakeDamage(gameObject, damage);
  124.             anim.Play("Hit");
  125.  
  126.         }
  127.  
  128.     }
  129.  
  130.     void Shoot()
  131.     {
  132.         Hit();
  133.     }
  134.  
  135.     public IEnumerable<float> GetAddMod(Stat stat)
  136.     {
  137.         if (stat == Stat.Damage)
  138.         {
  139.             yield return currentwep.GetDamage();
  140.         }
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement