You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.0 KiB
C#
44 lines
1.0 KiB
C#
2 weeks ago
|
using UnityEngine;
|
||
|
|
||
|
public class StaminaManager : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private float maxStamina = 100f;
|
||
|
[SerializeField] private float staminaRegenRate = 10f;
|
||
|
[SerializeField] private float staminaRegenDelay = 1f;
|
||
|
|
||
|
private float currentStamina;
|
||
|
private float staminaRegenTimer;
|
||
|
|
||
|
public float CurrentStamina => currentStamina;
|
||
|
public float MaxStamina => maxStamina;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
currentStamina = maxStamina;
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
if (staminaRegenTimer > 0)
|
||
|
{
|
||
|
staminaRegenTimer -= Time.deltaTime;
|
||
|
}
|
||
|
else if (currentStamina < maxStamina)
|
||
|
{
|
||
|
currentStamina += staminaRegenRate * Time.deltaTime;
|
||
|
currentStamina = Mathf.Min(currentStamina, maxStamina);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public bool TryConsume(float amount)
|
||
|
{
|
||
|
if (currentStamina >= amount)
|
||
|
{
|
||
|
currentStamina -= amount;
|
||
|
staminaRegenTimer = staminaRegenDelay;
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|