Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Fighter : MonoBehaviour, IAction, IModifier
- {
- public Fighter instance;
- public Health target;
- public Animator anim;
- public float timeBetweenAttacks = 1f;
- public float timeSinceLastAttack = 0;
- public PlayerWeapon DefultWeapon = null;
- [SerializeField] Transform handTrans;
- public PlayerWeapon currentwep = null;
- public string defultWeaponName = "Staff";
- public CapsuleCollider Capcolider;
- private void Start()
- {
- instance = this;
- anim = GetComponent<Animator>();
- PlayerWeapon weapon = UnityEngine.Resources.Load<PlayerWeapon>(defultWeaponName);
- EquipWeapon(currentwep);
- }
- private void Update()
- {
- timeSinceLastAttack += Time.deltaTime;
- if (target == null) return;
- if (target.isDead) return;
- if (!IsInRange())
- {
- GetComponent<Mover>().MoveTo(target.transform.position);
- } else
- {
- GetComponent<Mover>().Cancel();
- Attack(gameObject);
- AttackBehavior();
- }
- }
- public void EquipWeapon(PlayerWeapon weapon)
- {
- currentwep = weapon;
- Animator animator = GetComponent<Animator>();
- weapon.Spawn(handTrans, animator);
- }
- public Health GetTar()
- {
- return target;
- }
- private void AttackBehavior()
- {
- transform.LookAt(transform.position);
- if(timeSinceLastAttack > timeBetweenAttacks)
- {
- anim.ResetTrigger("StopAttack");
- anim.SetTrigger("Attack");
- timeSinceLastAttack = 0;
- }
- }
- public bool IsInRange()
- {
- return Vector3.Distance(transform.position, target.transform.position) < currentwep.GetRange();
- }
- public bool CanAttack(GameObject combatTar)
- {
- if (combatTar == null)
- {
- return false;
- }
- Health tarToTest = combatTar.GetComponent<Health>();
- return tarToTest != null;
- }
- public void Attack(GameObject combattarget)
- {
- GetComponent<ActionScedular>().StartAction(this);
- target = combattarget.GetComponent<Health>();
- }
- public void Cancel()
- {
- StopAttack();
- target = null;
- }
- void StopAttack()
- {
- GetComponent<Animator>().ResetTrigger("Attack");
- GetComponent<Animator>().SetTrigger("StopAttack");
- }
- void Hit ()
- {
- if (target == null) { return; }
- float damage = GetComponent<BaseStats>().GetStat(Stat.Damage);
- if (currentwep.HasProjectile())
- {
- currentwep.LaunchProjectile(handTrans, target, gameObject, damage);
- } else
- {
- Debug.Log("Hit" + target, target);
- target.TakeDamage(gameObject, damage);
- anim.Play("Hit");
- }
- }
- void Shoot()
- {
- Hit();
- }
- public IEnumerable<float> GetAddMod(Stat stat)
- {
- if (stat == Stat.Damage)
- {
- yield return currentwep.GetDamage();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement