using System.Collections; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace Improved_Health_Bars.Scripts { public class HealthScript : MonoBehaviour { public Image healthbar; // sets HealthBar Image public Image damagebar; // sets DamageBar Image public Image blockbar; // sets BlockBar Image public Text healthCountTxt; // sets health amount if defined public float barCapacity = 1; // sets healths visible area between 0.1f-1f public float maxHP = 100; // sets maximum health as number public float currentHP = 100; // sets current health as number public float dmgReduction = 0; // sets damage reduction from taken damage eg: %25 dmg reduction means 100taken dmg deals 75dmg public float naturalHealingRate = 2; //sets natural healings heal rate public float naturalHealingInterval = 2; // sets natural healing trigger second public float dangerHealthPercent = 0.15f; // sets danger life color start value: %15 percent default public float cautionHealthPercent = 0.35f; // sets caution life color start value: %35 percent default public bool triggerNaturalHealing = false; // sets enabling natural healing public bool enableMultiColor = false; // sets change healthbar color depends on its value public UnityEvent onDead; // sets an event and triggers it if health reaches 0 public Color healthyColor; // sets healthy life color public Color warningColor; // sets caution life color 1 public Color warningDarkColor; // sets caution life color 2 public Color dangerColor; // sets danger life color 1 public Color dangerDarkColor; // sets danger life color 2 public bool isDead; // sets target is dead or alive private float barFilled = 1; private IEnumerator setImagesCoroutine; private IEnumerator colorLightOnCoroutine; private IEnumerator naturalHealingCoroutine; private IEnumerator dotDmgCoroutine; void Start() { currentHP = maxHP; if (barCapacity < 0.1f) { barCapacity = 0.1f; Debug.LogWarning("Barfilled must be between 0.1 and 1"); } if (blockbar) { blockbar.fillAmount = 1 - barCapacity; } healthbar.fillAmount = barCapacity; damagebar.fillAmount = barCapacity; if (healthCountTxt) { healthCountTxt.text = currentHP.ToString("N0") + " / " + maxHP.ToString("N0"); } if (triggerNaturalHealing) { SetNaturalHealing(); } SetIntoScreen(); } public void DamageHealth(float dmg) { currentHP -= (dmg - (dmg * dmgReduction)); if (currentHP <= 0) { currentHP = 0; Dead(); } SetIntoScreen(); } public void RestoreHealth(float res) { currentHP += (int) res; if (currentHP > maxHP) { currentHP = maxHP; } SetIntoScreen(); } public void ChangeBarFilled() { if (barCapacity < 0.1f) { barCapacity = 0.1f; Debug.LogWarning("Barfilled must be between 0.1 and 1"); } blockbar.fillAmount = 1 - barCapacity; healthbar.fillAmount = barCapacity; damagebar.fillAmount = barCapacity; } public void SetNaturalHealing() { StopNaturalHealing(); naturalHealingCoroutine = NaturalHeal(naturalHealingRate, naturalHealingInterval); StartCoroutine(naturalHealingCoroutine); } public void SetDotDamage(int dmg, float interval, float duration = 0) { StopDotDmg(); dotDmgCoroutine = DotDamage(dmg, interval, duration); StartCoroutine(dotDmgCoroutine); } public void StopNaturalHealing() { if (naturalHealingCoroutine != null) { StopCoroutine(naturalHealingCoroutine); } } public void StopDotDmg() { if (dotDmgCoroutine != null) { StopCoroutine(dotDmgCoroutine); } } void Dead() { if (!isDead) { isDead = true; Debug.Log("You Are Dead"); if (dotDmgCoroutine != null) { StopCoroutine(dotDmgCoroutine); } if (naturalHealingCoroutine != null) { StopCoroutine(naturalHealingCoroutine); } if (colorLightOnCoroutine != null) { StopCoroutine(colorLightOnCoroutine); } onDead.Invoke(); } } void SetIntoScreen() { barFilled = barCapacity * currentHP / maxHP; healthbar.fillAmount = barFilled; if (colorLightOnCoroutine != null) { StopCoroutine(colorLightOnCoroutine); } if (enableMultiColor) { if (currentHP / maxHP < dangerHealthPercent) { healthbar.color = dangerColor; colorLightOnCoroutine = LightColor(dangerDarkColor, dangerColor, 0.05f); StartCoroutine(colorLightOnCoroutine); } else if (currentHP / maxHP < cautionHealthPercent) { healthbar.color = warningColor; colorLightOnCoroutine = LightColor(warningDarkColor, warningColor, 0.02f); StartCoroutine(colorLightOnCoroutine); } else { healthbar.color = healthyColor; } } if (damagebar.fillAmount > healthbar.fillAmount) { if (setImagesCoroutine != null) { StopCoroutine(setImagesCoroutine); } setImagesCoroutine = setImages(); StartCoroutine(setImagesCoroutine); } else { damagebar.fillAmount = healthbar.fillAmount; } if (healthCountTxt) { healthCountTxt.text = currentHP.ToString("N0") + " / " + maxHP.ToString("N0"); } } IEnumerator setImages() { yield return new WaitForSeconds(0.25f); while (healthbar.fillAmount < damagebar.fillAmount) { damagebar.fillAmount -= 0.005f; yield return new WaitForSeconds(0.015f); } } IEnumerator LightColor(Color dark, Color light, float pow) { while (true) { float t = 0; while (t <= 1) { healthbar.color = Color.Lerp(light, dark, t); yield return new WaitForSeconds(0.01f); t += pow; } t = 0; while (t <= 1) { healthbar.color = Color.Lerp(dark, light, t); yield return new WaitForSeconds(0.01f); t += pow; } } } IEnumerator DotDamage(float dmg, float interval, float duration = 0) { if (duration > 0) { while (duration > 0) { yield return new WaitForSeconds(interval); duration -= interval; DamageHealth(dmg); } } else { while (true) { yield return new WaitForSeconds(interval); DamageHealth(dmg); } } } IEnumerator NaturalHeal(float heal, float interval, float duration = 0) { if (duration > 0) { while (duration > 0) { yield return new WaitForSeconds(interval); DamageHealth(heal); duration -= interval; } } else { while (true) { yield return new WaitForSeconds(interval); RestoreHealth(heal); } } } } }