RiftMayhem/Assets/Scripts/Resource.cs
Pedro Gomes f1bef2485d Multiple improvements
- scrolling combat text
- updated inventory and stat panel UIs
- bugfix regens
- small changes to lighting in skellyard.
2025-09-18 17:40:08 +01:00

97 lines
2.3 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 baseRegenValue;
[SerializeField] protected float percentRegen;
[SerializeField] protected float timeBetweenRegens;
protected bool canRegen = true;
public UnityEvent<float> onResourceChanged = new UnityEvent<float>();
// Start is called before the first frame update
protected virtual void Start()
{
currentValue = maxValue;
//flatRegen = baseRegenValue;
onResourceChanged.Invoke(currentValue);
}
protected virtual void OnDisable()
{
StopAllCoroutines();
}
protected virtual void OnEnable()
{
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 float GetBaseRegenValue()
{
return baseRegenValue;
}
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());
}
}