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.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/StaminaManager.cs

90 lines
2.7 KiB
C#

using UnityEngine;
public class StaminaManager : MonoBehaviour
{
[SerializeField] private float maxStamina = 100f;
[SerializeField] private float staminaRegenRate = 10f;
[SerializeField] private float staminaRegenDelay = 2f;
private float currentStamina;
private float staminaRegenTimer;
private bool isConsumingStamina;
public float CurrentStamina => currentStamina;
public float MaxStamina => maxStamina;
public bool IsStaminaDepleted => currentStamina <= 0;
private void Start()
{
currentStamina = maxStamina;
Debug.Log($"[StaminaManager] Stamina initialized. Max Stamina: {maxStamina}");
}
private void Update()
{
if (isConsumingStamina)
{
staminaRegenTimer = staminaRegenDelay; // Reset regen delay if consuming stamina
}
else
{
// Start regen countdown after sprinting stops
if (staminaRegenTimer > 0)
{
staminaRegenTimer -= Time.deltaTime;
}
else if (currentStamina < maxStamina)
{
RegenerateStamina();
}
}
}
public bool TryConsume(float amount)
{
if (currentStamina >= amount)
{
currentStamina -= amount;
isConsumingStamina = true;
Debug.Log($"[StaminaManager] Consumed {amount} stamina. Remaining: {currentStamina}/{maxStamina}");
if (currentStamina <= 0)
{
currentStamina = 0;
Debug.Log("[StaminaManager] Stamina depleted! Stopping consumption.");
StopConsuming();
}
return true;
}
Debug.Log("[StaminaManager] Not enough stamina!");
return false;
}
public void StartConsuming()
{
isConsumingStamina = true;
staminaRegenTimer = staminaRegenDelay; // Reset regen delay when consumption starts
}
public void StopConsuming()
{
isConsumingStamina = false; // Stop consuming and allow regen countdown
staminaRegenTimer = staminaRegenDelay; // Ensure regen starts after delay
Debug.Log("[StaminaManager] Stopped consuming. Regen will start after delay.");
}
private void RegenerateStamina()
{
float previousStamina = currentStamina;
currentStamina += staminaRegenRate * Time.deltaTime;
currentStamina = Mathf.Min(currentStamina, maxStamina);
Debug.Log($"[StaminaManager] Regenerating: {previousStamina} → {currentStamina}/{maxStamina}");
if (currentStamina == maxStamina)
{
Debug.Log("[StaminaManager] Fully recovered!");
}
}
}