Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- [RequireComponent(typeof(Animator))]
- public class Player : MonoBehaviour
- {
- [SerializeField] private int _health;
- [SerializeField] private List<Weapon> _weapons;
- [SerializeField] private Transform _shootPoint;
- private Weapon _currentWeapon;
- private int _currentWeaponNumber = 0;
- private int _currentHealth;
- private Animator _animator;
- public event UnityAction<int, int> HealthChanged;
- public event UnityAction<int> MoneyChanged;
- public int Money { get; private set; }
- private void Start()
- {
- _animator = GetComponent<Animator>();
- _currentHealth = _health;
- ChangeWeapon(_weapons[_currentWeaponNumber]);
- }
- private void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- StartCoroutine(_currentWeapon.Shoot(_shootPoint));
- }
- StopCoroutine(_currentWeapon.Shoot(_shootPoint));
- }
- public void ApplyDamage(int damage)
- {
- _currentHealth -= damage;
- HealthChanged?.Invoke(_currentHealth, _health);
- if (_currentHealth <= 0)
- Destroy(gameObject);
- }
- public void AddMoney(int reward)
- {
- Money += reward;
- MoneyChanged?.Invoke(Money);
- }
- public void BuyWeapon(Weapon weapon)
- {
- Money -= weapon.Price;
- MoneyChanged?.Invoke(Money);
- _weapons.Add(weapon);
- }
- public void NextWeapon()
- {
- if(_currentWeaponNumber == _weapons.Count - 1)
- {
- _currentWeaponNumber = 0;
- }
- else
- {
- _currentWeaponNumber++;
- }
- ChangeWeapon(_weapons[_currentWeaponNumber]);
- }
- public void PrevWeapon()
- {
- if (_currentWeaponNumber == 0)
- {
- _currentWeaponNumber = _weapons.Count - 1;
- }
- else
- {
- _currentWeaponNumber--;
- }
- ChangeWeapon(_weapons[_currentWeaponNumber]);
- }
- private void ChangeWeapon(Weapon weapon)
- {
- _currentWeapon = weapon;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement