metahunt/Assets/Textures/HealthandShield/Healthbar/Scripts/ManaScript.cs
2024-02-11 13:53:18 +05:30

152 lines
4.3 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Improved_Health_Bars.Scripts
{
public class ManaScript : MonoBehaviour
{
public Image manabar; // sets ManaBar Image
public Image damagebar; // sets DamageBar Image
public Image blockbar; // sets BlockBar Image
public Text manaCountTxt; // sets mana amount if defined
public float barCapacity = 1; // sets manas visible area between 0.1f-1f
public float maxMana = 100; // sets maximum mana as number
public float currentMana = 100; // sets current mana as number
public int naturalManaRate = 10; //sets natural mana recovery mana rate
public float naturalManaInterval = 4f; // sets natural mara recovery trigger second
public bool triggerNaturalMana = false; // sets enabling natural mana recovery
private float barFilled = 1;
private IEnumerator setImagesCoroutine;
private IEnumerator naturalManaCoroutine;
void Start()
{
currentMana = maxMana;
if (barCapacity < 0.1f)
{
barCapacity = 0.1f;
Debug.LogWarning("Barfilled must be between 0.1 and 1");
}
if (blockbar)
{
blockbar.fillAmount = 1 - barCapacity;
}
manabar.fillAmount = barCapacity;
damagebar.fillAmount = barCapacity;
if (manaCountTxt)
{
manaCountTxt.text = ((int) currentMana).ToString("N0") + " / " + ((int) maxMana).ToString("N0");
}
if (triggerNaturalMana)
{
StopNaturalMana();
naturalManaCoroutine = NaturalMana(naturalManaRate, naturalManaInterval);
StartCoroutine(naturalManaCoroutine);
}
}
public void DamageMana(float dmg)
{
currentMana -= dmg;
if (currentMana <= 0)
{
currentMana = 0;
}
SetIntoScreen();
}
public void RestoreMana(float res)
{
currentMana += (int) res;
if (currentMana > maxMana)
{
currentMana = maxMana;
}
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;
manabar.fillAmount = barCapacity;
damagebar.fillAmount = barCapacity;
}
public void SetNaturalMana()
{
StopNaturalMana();
naturalManaCoroutine = NaturalMana(naturalManaRate, naturalManaInterval);
StartCoroutine(naturalManaCoroutine);
}
public void StopNaturalMana()
{
if (naturalManaCoroutine != null)
{
StopCoroutine(naturalManaCoroutine);
}
}
void SetIntoScreen()
{
barFilled = barCapacity * currentMana / maxMana;
manabar.fillAmount = barFilled;
if (damagebar.fillAmount > manabar.fillAmount)
{
if (setImagesCoroutine != null)
{
StopCoroutine(setImagesCoroutine);
}
setImagesCoroutine = setImages();
StartCoroutine(setImagesCoroutine);
}
else
{
damagebar.fillAmount = manabar.fillAmount;
}
if (manaCountTxt)
{
manaCountTxt.text = ((int) currentMana).ToString("N0") + " / " + ((int) maxMana).ToString("N0");
}
}
IEnumerator setImages()
{
yield return new WaitForSeconds(0.25f);
while (manabar.fillAmount < damagebar.fillAmount)
{
damagebar.fillAmount -= 0.005f;
yield return new WaitForSeconds(0.015f);
}
}
IEnumerator NaturalMana(float mana, float interval)
{
while (true)
{
yield return new WaitForSeconds(interval);
RestoreMana(mana);
}
}
}
}