Skip to content

Instantly share code, notes, and snippets.

@jweimann
Last active December 29, 2016 05:11
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/2346b48da592775a04ca238efb808572 to your computer and use it in GitHub Desktop.
Save jweimann/2346b48da592775a04ca238efb808572 to your computer and use it in GitHub Desktop.
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