Last active
December 29, 2016 06:18
-
-
Save jweimann/0b5a45a7ee5b5946a233398cacd96b59 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 NPC : MonoBehaviour | |
{ | |
[SerializeField] private int startingHp = 100; | |
[SerializeField] private ParticleSystem deathParticlePrefab; | |
private int currentHp; | |
private float CurrentHpPct | |
{ | |
get { return (float) currentHp / (float) startingHp; } | |
} | |
public event Action<float> OnHPPctChanged = delegate { }; | |
public event Action OnNPCDied = delegate { }; | |
private void Start() | |
{ | |
currentHp = startingHp; | |
} | |
internal void TakeDamage(int amount) | |
{ | |
if (amount <= 0) | |
throw new ArgumentOutOfRangeException("Invalid Damage amount specified: " + amount); | |
currentHp -= amount; | |
OnHPPctChanged(CurrentHpPct); | |
if (currentHp <= 0) | |
Die(); | |
} | |
private void Die() | |
{ | |
OnNPCDied(); | |
GameObject.Destroy(this.gameObject); | |
} | |
}c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment