GoodNoodle

Player Manager

Jul 3rd, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerManager : MonoBehaviour
  6. {
  7.     AnimManager animManager;
  8.     PlayerMovement playerMovement;
  9.     public Fighter fighter;
  10.     public Health target;
  11.    
  12.     public float horInput;
  13.     public float vertInput;
  14.  
  15.     public float moveAmount;
  16.  
  17.     public static PlayerManager instance;
  18.  
  19.     public GameObject player;
  20.  
  21.     public PlayerWeapon DefultWeapon = null;
  22.  
  23.     public PlayerWeapon currentwep = null;
  24.  
  25.     public AnimatorOverrideController animatorOverride = null;
  26.  
  27.     [SerializeField] Transform handTrans;
  28.  
  29.     public string defultWeaponName = "Staff";
  30.  
  31.     public Item item;
  32.  
  33.     public GameObject enemy;
  34.  
  35.     public AIController aiController;
  36.    
  37.  
  38.     // Start is called before the first frame update
  39.     void Start()
  40.     {
  41.         aiController = GetComponent<AIController>();
  42.         fighter = GetComponent<Fighter>();
  43.         enemy = GameObject.FindWithTag("Enemy");
  44.         target = GetComponent<Health>();
  45.         animManager = GetComponent<AnimManager>();
  46.         playerMovement = GetComponent<PlayerMovement>();
  47.         instance = this;
  48.  
  49.         PlayerWeapon weapon = UnityEngine.Resources.Load<PlayerWeapon>(defultWeaponName);
  50.         SpawnWeapon(currentwep);
  51.     }
  52.  
  53.     // Update is called once per frame
  54.     void Update()
  55.     {
  56.         if (target.isDead) return;
  57.         EquipWeapon(currentwep);
  58.         animManager.UpdateAnimValues(0, moveAmount);
  59.         moveAmount = Mathf.Clamp01(Mathf.Abs(horInput) + Mathf.Abs(vertInput));
  60.  
  61.          
  62.             AttackBehavior();
  63.        
  64.        
  65.     }
  66.  
  67.     private void FixedUpdate()
  68.     {
  69.         if (Input.GetKeyDown(KeyCode.M))
  70.         {
  71.             InventoryManager.instance.AddItem(item);
  72.         }
  73.  
  74.         if (Input.GetKeyDown(KeyCode.N))
  75.         {
  76.             InventoryManager.instance.RemoveItem(item);
  77.         }
  78.     }
  79.  
  80.     private void SpawnWeapon(PlayerWeapon weapon)
  81.     {
  82.         currentwep = weapon;
  83.         Animator animator = GetComponent<Animator>();
  84.         weapon.Spawn(handTrans, animator);
  85.  
  86.         var overrideController = animator.runtimeAnimatorController as AnimatorOverrideController;
  87.         if (animatorOverride != null)
  88.         {
  89.             animator.runtimeAnimatorController = animatorOverride;
  90.         }
  91.         else if (overrideController != null)
  92.         {
  93.             animator.runtimeAnimatorController = overrideController.runtimeAnimatorController;
  94.         }
  95.     }
  96.  
  97.  
  98.  
  99.     public void Hit()
  100.     {
  101.  
  102.         animManager.Hit();
  103.         float damage = GetComponent<BaseStats>().GetStat(Stat.Damage);
  104.        
  105.             if (currentwep.HasProjectile())
  106.             {
  107.                 currentwep.LaunchProjectile(handTrans, target, gameObject, damage);
  108.             }
  109.             else
  110.             {
  111.            
  112.             target.TakeDamage(gameObject, damage);
  113.             }
  114.        
  115.        
  116.     }
  117.  
  118.     public void Attack(GameObject combattarget)
  119.     {
  120.        
  121.         target = combattarget.GetComponent<Health>();
  122.     }
  123.     public void EquipWeapon(PlayerWeapon weapon)
  124.     {
  125.         currentwep = weapon;
  126.         Animator animator = GetComponent<Animator>();
  127.         weapon.Spawn(handTrans, animator);
  128.     }
  129.  
  130.     public IEnumerable<float> GetAddMod(Stat stat)
  131.     {
  132.         if (stat == Stat.Damage)
  133.         {
  134.             yield return currentwep.GetDamage();
  135.         }
  136.     }
  137.    
  138.  
  139.  
  140.     private void AttackBehavior()
  141.     {
  142.        
  143.         Attack(enemy);
  144.     }
  145. }
  146.  
Add Comment
Please, Sign In to add comment