RiftMayhem/Assets/Scripts/Resource.cs
2025-02-21 18:35:51 +00:00

86 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Resource : MonoBehaviour
{
[SerializeField] protected float currentValue;
[SerializeField] protected float maxValue;
[SerializeField] protected float baseMaxValue;
[SerializeField] protected float flatRegen;
[SerializeField] protected float percentRegen;
[SerializeField] protected float timeBetweenRegens;
protected bool canRegen = true;
public UnityEvent<float> onResourceChanged = new UnityEvent<float>();
protected float baseFlatRegen;
// Start is called before the first frame update
protected virtual void Start()
{
currentValue = maxValue;
onResourceChanged.Invoke(currentValue);
StartCoroutine(RegenResource());
}
public virtual void ChangeValue(float value)
{
currentValue += value;
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
onResourceChanged.Invoke(currentValue);
}
public float GetCurrentValue()
{
return currentValue;
}
public virtual float GetMaxValue()
{
return maxValue;
}
public virtual float GetBaseMaxValue()
{
return baseMaxValue;
}
public virtual void SetCurrentValue(float value)
{
currentValue = value;
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
onResourceChanged.Invoke(currentValue);
}
public virtual void SetMaxValue(float value)
{
maxValue = value;
}
protected IEnumerator RegenResource()
{
while (canRegen)
{
yield return new WaitForSeconds(timeBetweenRegens);
ChangeValue(flatRegen + (maxValue * percentRegen/100f));
}
StartCoroutine(RestartRegen());
}
protected IEnumerator RestartRegen()
{
while (!canRegen)
{
yield return new WaitForSeconds(timeBetweenRegens);
}
StartCoroutine(RegenResource());
}
}