using System.Collections; using System.Collections.Generic; using UnityEngine; public class ManaManager : MonoBehaviour { [Header("Mana")] public float maxMana = 100f; public float mana = 100f; [Header("Regen")] public bool enableRegen = true; public float regenPerSecond = 8f; public float regenDelayAfterSpend = 1.0f; float regenResumeTime = 0f; void Awake() { mana = Mathf.Clamp(mana, 0, maxMana); } void Update() { if (!enableRegen) return; if (Time.time < regenResumeTime) return; if (mana >= maxMana) return; mana = Mathf.Min(maxMana, mana + regenPerSecond * Time.deltaTime); } public bool Has(float amount) => mana >= amount; public bool Spend(float amount) { if (amount <= 0f) return true; if (mana < amount) return false; mana -= amount; regenResumeTime = Time.time + Mathf.Max(0f, regenDelayAfterSpend); return true; } public void Refill(float amount) { if (amount <= 0f) return; mana = Mathf.Min(maxMana, mana + amount); } public void SetMax(float newMax, bool keepRatio = true) { newMax = Mathf.Max(1f, newMax); float ratio = maxMana > 0 ? mana / maxMana : 1f; maxMana = newMax; mana = keepRatio ? Mathf.Clamp01(ratio) * maxMana : Mathf.Min(mana, maxMana); } }