Last active
December 29, 2016 05:46
-
-
Save jweimann/4b62948fd627afe74be0e94e3b1b0e85 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 UnityEngine.UI.Slider hpBarSlider; | |
[SerializeField] | |
private ParticleSystem deathParticlePrefab; | |
private int _currentHp; | |
private void Start() | |
{ | |
currentHp = startingHp; | |
} | |
internal void TakeDamage(int amount) | |
{ | |
if (amount <= 0) | |
throw new ArgumentOutOfRangeException("Invalid Damage amount specified: " + amount); | |
currentHp -= amount; | |
UpdateUI(); | |
if (currentHp <= 0) | |
Die(); | |
} | |
private void UpdateUI() | |
{ | |
var currentHpPct = (float)currentHp / (float)startingHp; | |
hpBarSlider.value = currentHpPct; | |
} | |
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