using System.Collections; using System.Collections.Generic; using UnityEngine; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class PlayerStamina : MonoBehaviour { public int maxStamina = 100; // Maximum stamina value private int currentStamina; // Current stamina value public float staminaRechargeTime = 0.5f; // Time between each stamina increment during recharge private bool isRechargingStamina = false; // Flag to check if stamina is currently recharging private float rechargeDelay = 0.2f; // Delay before starting stamina recharge after use public StaminaBar staminaBar; // Reference to the stamina bar UI void Start() { currentStamina = maxStamina; // Initialize current stamina to maximum value staminaBar.SetMaxStamina(maxStamina); // Set the maximum stamina value on the stamina bar } // Method to use stamina public bool UseStamina(int amount) { // Check if enough stamina is available if (currentStamina >= amount) { currentStamina -= amount; // Deduct the specified amount of stamina staminaBar.SetStamina(currentStamina); // Update the stamina bar // Start recharging stamina if not already recharging if (!isRechargingStamina) { StartCoroutine(RechargeStamina()); } return true; // Successfully used stamina } return false; // Not enough stamina available } // Coroutine to handle stamina recharge private IEnumerator RechargeStamina() { isRechargingStamina = true; // Set the flag to indicate recharging has started yield return new WaitForSeconds(rechargeDelay); // Wait for the initial delay before starting recharge // Recharge stamina until it reaches the maximum value while (currentStamina < maxStamina) { currentStamina += 5; // Increase stamina by 5 units (adjust as needed) if (currentStamina > maxStamina) { currentStamina = maxStamina; // Ensure stamina does not exceed maximum value } staminaBar.SetStamina(currentStamina); // Update the stamina bar yield return new WaitForSeconds(staminaRechargeTime); // Wait before the next increment } isRechargingStamina = false; // Set the flag to indicate recharging has stopped } }