Pro_Unit

Health

Nov 30th, 2020
413
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using Platformer.Gameplay;
  3. using UnityEngine;
  4. using static Platformer.Core.Simulation;
  5.  
  6. namespace Platformer.Mechanics
  7. {
  8.     /// <summary>
  9.     /// Represebts the current vital statistics of some game entity.
  10.     /// </summary>
  11.     public class Health : MonoBehaviour
  12.     {
  13.         /// <summary>
  14.         /// The maximum hit points for the entity.
  15.         /// </summary>
  16.         public int maxHP = 1;
  17.  
  18.         /// <summary>
  19.         /// Indicates if the entity should be considered 'alive'.
  20.         /// </summary>
  21.         public bool IsAlive => currentHP > 0;
  22.  
  23.         int currentHP;
  24.  
  25.         /// <summary>
  26.         /// Increment the HP of the entity.
  27.         /// </summary>
  28.         public void Increment()
  29.         {
  30.             currentHP = Mathf.Clamp(currentHP + 1, 0, maxHP);
  31.         }
  32.  
  33.         /// <summary>
  34.         /// Decrement the HP of the entity. Will trigger a HealthIsZero event when
  35.         /// current HP reaches 0.
  36.         /// </summary>
  37.         public void Decrement()
  38.         {
  39.             currentHP = Mathf.Clamp(currentHP - 1, 0, maxHP);
  40.             if (currentHP == 0)
  41.             {
  42.                 var ev = Schedule<HealthIsZero>();
  43.                 ev.health = this;
  44.             }
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Decrement the HP of the entitiy until HP reaches 0.
  49.         /// </summary>
  50.         public void Die()
  51.         {
  52.             while (currentHP > 0) Decrement();
  53.         }
  54.  
  55.         void Awake()
  56.         {
  57.             currentHP = maxHP;
  58.         }
  59.     }
  60. }
  61.  
RAW Paste Data