Created
December 30, 2016 19:55
-
-
Save unity3dcollege/814d768b995d77263b31e5b64742806e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public class StandardHealth : MonoBehaviour, IHealth | |
{ | |
[SerializeField] private int startingHealth = 100; | |
private int currentHealth; | |
public event Action<float> OnHPPctChanged = delegate { }; | |
public event Action OnDied = delegate { }; | |
private void Start() | |
{ | |
currentHealth = startingHealth; | |
} | |
public float CurrentHpPct | |
{ | |
get { return (float) currentHealth / (float) startingHealth; } | |
} | |
public void TakeDamage(int amount) | |
{ | |
if (amount <= 0) | |
throw new ArgumentOutOfRangeException("Invalid Damage amount specified: " + amount); | |
currentHealth -= amount; | |
OnHPPctChanged(CurrentHpPct); | |
if (CurrentHpPct <= 0) | |
Die(); | |
} | |
private void Die() | |
{ | |
OnDied(); | |
GameObject.Destroy(this.gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment