Created
June 12, 2015 14:02
-
-
Save ashblue/ab6663ae0fe66b2f6fdb to your computer and use it in GitHub Desktop.
Unity health management component.
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 UnityEngine; | |
using System.Collections; | |
namespace Adnc.Combat { | |
[System.Serializable] | |
public class StatHealth { | |
[Tooltip("Set health manually")] | |
[SerializeField] int health; | |
int healthMax; | |
public int Health { | |
get { return health; } | |
set { health = Mathf.Max(Mathf.Min(value, healthMax), 0); } | |
} | |
public int HealthMax { | |
get { return healthMax; } | |
set { | |
healthMax = value; | |
if (health > healthMax) health = healthMax; | |
} | |
} | |
[Tooltip("Use a curve based on the player's level to determine health. Manually setting the number always takes priority over a curve.")] | |
[SerializeField] AnimationCurve healthCurve; | |
[Tooltip("How much health is healed per minute")] | |
public int healthRegen; | |
float healthRegenPool; | |
[Tooltip("Alt to manually setting health regen (see health curve)")] | |
[SerializeField] AnimationCurve healthRegenCurve; | |
[Tooltip("Reduces the total damage taken by this number (min of 1)")] | |
public int defense; | |
[Tooltip("Alt to manually setting defense (see health curve)")] | |
[SerializeField] AnimationCurve defenseCurve; | |
[Tooltip("Doesn't die when reaching 0 health")] | |
public bool immortal; | |
[Tooltip("Never takes damage, still receives attacks")] | |
public bool invulnerable; | |
public void ReceiveHealth (int val) { | |
health = Mathf.Min(val + health, healthMax); | |
} | |
public void RemoveHealth (int amount) { | |
if (invulnerable) return; | |
int damage = Mathf.Min(1, amount - defense); | |
health = Mathf.Min(0, health - damage); | |
} | |
public bool IsDead () { | |
if (immortal) { | |
return false; | |
} | |
return health <= 0; | |
} | |
// Calculates, finalizes, and sets stat curves | |
public void Setup () { | |
if (healthCurve.length > 0 && health == 0) { | |
healthMax = (int)healthCurve.Evaluate(Sm.exp.level); | |
health = healthMax; | |
} else { | |
healthMax = health; | |
} | |
if (defenseCurve.length > 0 && defense == 0) { | |
defense = (int)defenseCurve.Evaluate(Sm.exp.level); | |
} | |
if (healthRegenCurve.length > 0 && healthRegen == 0) { | |
healthRegen = (int)healthRegenCurve.Evaluate(Sm.exp.level); | |
} | |
} | |
// Makes health regen stat generate additional hitpoints over time | |
// @NOTE Designed to be called in the update loop | |
public void HealthRegen () { | |
healthRegenPool += healthRegen / 60f * Time.deltaTime; | |
if (healthRegenPool > 1f) { | |
int addHealth = (int)Mathf.Floor(healthRegenPool); | |
ReceiveHealth(addHealth); | |
healthRegenPool -= addHealth; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment