2018-09-26 22:38:49 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class UnitHealth : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public int currentHealth;
|
|
|
|
|
public int maximumHealth = 10;
|
|
|
|
|
|
2018-09-30 14:35:17 +00:00
|
|
|
|
void Awake()
|
2018-09-26 22:38:49 +00:00
|
|
|
|
{
|
|
|
|
|
currentHealth = maximumHealth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Damage(int amount)
|
|
|
|
|
{
|
|
|
|
|
//Negative damage doesn't exist
|
|
|
|
|
if(amount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//Damage the unit
|
|
|
|
|
currentHealth -= amount;
|
|
|
|
|
//Do not go beyond zero
|
|
|
|
|
if(currentHealth < 0)
|
|
|
|
|
{
|
|
|
|
|
currentHealth = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Heal(int amount)
|
|
|
|
|
{
|
|
|
|
|
currentHealth += amount;
|
|
|
|
|
//Positive healing
|
|
|
|
|
if (amount >= 0)
|
|
|
|
|
{
|
|
|
|
|
if(currentHealth > maximumHealth)
|
|
|
|
|
{
|
|
|
|
|
currentHealth = maximumHealth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Negative healing: non-lethal!
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Prevent lethal damage
|
|
|
|
|
if(currentHealth <= 0)
|
|
|
|
|
{
|
|
|
|
|
currentHealth = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|