Skip to content

Instantly share code, notes, and snippets.

@jweimann
Last active December 29, 2016 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweimann/010633108b3b639887dc95110ccf9b0f to your computer and use it in GitHub Desktop.
Save jweimann/010633108b3b639887dc95110ccf9b0f to your computer and use it in GitHub Desktop.
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 { };
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()
{
PlayDeathParticle();
GameObject.Destroy(this.gameObject);
}
private void PlayDeathParticle()
{
var deathparticle = Instantiate(deathParticlePrefab, transform.position, transform.rotation);
Destroy(deathparticle, 4f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment