Advertisement
centner_dc

UI_Slider_Health

Sep 29th, 2020
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using Tools;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4.  
  5. namespace Player
  6. {
  7.     public class Health : MonoBehaviour, IHealthy
  8.     {
  9.         [SerializeField] private float _amount;
  10.         [SerializeField] private float _maxAmount = 100f;
  11.  
  12.         public event UnityAction<float> HealthChanged;
  13.  
  14.         public float Amount => _amount;
  15.         public float MaxAmount => _maxAmount;
  16.  
  17.         public void Cure(float cure)
  18.         {
  19.             cure.VerifyPositive(nameof(cure));
  20.  
  21.             _amount += cure;
  22.             _amount = Mathf.Min(_amount, _maxAmount);
  23.             HealthChanged?.Invoke(_amount);
  24.         }
  25.  
  26.         public void Damage(float damage)
  27.         {
  28.             damage.VerifyPositive(nameof(damage));
  29.  
  30.             _amount -= damage;
  31.             _amount = Mathf.Max(_amount, 0f);
  32.             HealthChanged?.Invoke(_amount);
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement