Last active
December 29, 2016 05:11
-
-
Save jweimann/2346b48da592775a04ca238efb808572 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; | |
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() | |
{ | |
GameObject.Destroy(this.gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment