66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Core player stats: health and stamina.
|
|
/// Stamina drains while sprinting, regens when not.
|
|
/// If stamina hits zero the player is exhausted and can't sprint again until it fully refills.
|
|
/// </summary>
|
|
public class Player : MonoBehaviour
|
|
{
|
|
[Header("Health")]
|
|
public float maxHealth = 100f;
|
|
public float health = 100f;
|
|
|
|
[Header("Stamina")]
|
|
public float maxStamina = 100f;
|
|
public float stamina = 100f;
|
|
public float staminaDrain = 22f; // per second while sprinting
|
|
public float staminaRegen = 12f; // per second while not sprinting
|
|
public float regenDelay = 1.2f; // seconds after stopping sprint before regen kicks in
|
|
|
|
// ─── State ───────────────────────────────────────────────────────
|
|
[HideInInspector] public bool isSprinting = false;
|
|
[HideInInspector] public bool isExhausted = false; // true until stamina fully refills
|
|
|
|
private float _regenTimer = 0f;
|
|
|
|
// ─────────────────────────────────────────────────────────────────
|
|
void Update()
|
|
{
|
|
if (isSprinting && !isExhausted)
|
|
{
|
|
stamina -= staminaDrain * Time.deltaTime;
|
|
_regenTimer = 0f;
|
|
|
|
if (stamina <= 0f)
|
|
{
|
|
stamina = 0f;
|
|
isExhausted = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Count down regen delay
|
|
_regenTimer += Time.deltaTime;
|
|
|
|
if (_regenTimer >= regenDelay)
|
|
{
|
|
stamina = Mathf.MoveTowards(stamina, maxStamina, staminaRegen * Time.deltaTime);
|
|
|
|
// Clear exhaustion only once fully refilled
|
|
if (isExhausted && stamina >= maxStamina)
|
|
isExhausted = false;
|
|
}
|
|
}
|
|
|
|
health = Mathf.Clamp(health, 0f, maxHealth);
|
|
stamina = Mathf.Clamp(stamina, 0f, maxStamina);
|
|
}
|
|
|
|
// ─── Helpers other scripts can call ──────────────────────────────
|
|
public bool CanSprint() => !isExhausted && stamina > 0f;
|
|
|
|
public float HealthFraction => health / maxHealth;
|
|
public float StaminaFraction => stamina / maxStamina;
|
|
}
|