Advertisement
Askor

Player

Jan 7th, 2022
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5.  
  6. [RequireComponent(typeof(Animator))]
  7. public class Player : MonoBehaviour
  8. {
  9.     [SerializeField] private int _health;
  10.     [SerializeField] private List<Weapon> _weapons;
  11.     [SerializeField] private Transform _shootPoint;
  12.  
  13.     private Weapon _currentWeapon;
  14.     private int _currentWeaponNumber = 0;
  15.     private int _currentHealth;
  16.     private Animator _animator;
  17.  
  18.     public event UnityAction<int, int> HealthChanged;
  19.     public event UnityAction<int> MoneyChanged;
  20.  
  21.     public int Money { get; private set; }
  22.  
  23.     private void Start()
  24.     {
  25.         _animator = GetComponent<Animator>();
  26.         _currentHealth = _health;
  27.  
  28.         ChangeWeapon(_weapons[_currentWeaponNumber]);
  29.     }
  30.  
  31.     private void Update()
  32.     {
  33.         if (Input.GetMouseButtonDown(0))
  34.         {
  35.             StartCoroutine(_currentWeapon.Shoot(_shootPoint));
  36.         }
  37.  
  38.         StopCoroutine(_currentWeapon.Shoot(_shootPoint));
  39.     }
  40.  
  41.     public void ApplyDamage(int damage)
  42.     {
  43.         _currentHealth -= damage;
  44.         HealthChanged?.Invoke(_currentHealth, _health);
  45.  
  46.         if (_currentHealth <= 0)
  47.             Destroy(gameObject);
  48.     }
  49.  
  50.     public void AddMoney(int reward)
  51.     {
  52.         Money += reward;
  53.         MoneyChanged?.Invoke(Money);
  54.     }
  55.  
  56.     public void BuyWeapon(Weapon weapon)
  57.     {
  58.         Money -= weapon.Price;
  59.         MoneyChanged?.Invoke(Money);
  60.         _weapons.Add(weapon);
  61.     }
  62.  
  63.     public void NextWeapon()
  64.     {
  65.         if(_currentWeaponNumber == _weapons.Count - 1)
  66.         {
  67.             _currentWeaponNumber = 0;
  68.         }
  69.         else
  70.         {
  71.             _currentWeaponNumber++;
  72.         }
  73.  
  74.         ChangeWeapon(_weapons[_currentWeaponNumber]);
  75.     }
  76.  
  77.     public void PrevWeapon()
  78.     {
  79.         if (_currentWeaponNumber == 0)
  80.         {
  81.             _currentWeaponNumber = _weapons.Count - 1;
  82.         }
  83.         else
  84.         {
  85.             _currentWeaponNumber--;
  86.         }
  87.  
  88.         ChangeWeapon(_weapons[_currentWeaponNumber]);
  89.     }
  90.  
  91.     private void ChangeWeapon(Weapon weapon)
  92.     {
  93.         _currentWeapon = weapon;
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement