MaximilianPs

Read Stamina on GKC and update HUD

Aug 1st, 2021
1,454
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UIHealthAlchemy;
  5.  
  6. public class BottleStamina : MonoBehaviour
  7. {
  8.     private MaterialHealhBar guiBar;
  9.  
  10.     private staminaSystem staminaSys;               // Stamina component, used to use and refill Stamina (didn't have a MaxValue)
  11.     private playerStatsSystem statsSys;             // For that reason we need to gain access to the another component.
  12.     private playerStatsSystem.statInfo staminaInfo; // Which contain an array and we need to search for what we need from all StatsInfo that the array contains!
  13.     private float currentStamina;
  14.     private float maxStamina;   // This is used for UIHealthAlchemy which works between 0 and 1
  15.  
  16.     private float oldValue = 0;
  17.     // Start is called before the first frame update
  18.     void Start()
  19.     {
  20.         guiBar = GetComponent<MaterialHealhBar>();
  21.  
  22.         if (!transform.root.GetComponentInChildren<playerStatsSystem>())
  23.         {
  24.             Debug.LogError("*** MISSING PLAYER STATS SYSTEM ON CHARACTER ***");
  25.             return;
  26.         }
  27.         if (!transform.root.GetComponentInChildren<staminaSystem>())
  28.         {
  29.             Debug.LogError("*** MISSING STAMINA SYSTEM ON CHARACTER ***");
  30.             return;
  31.         }
  32.  
  33.         statsSys = transform.root.GetComponentInChildren<playerStatsSystem>();  // This is the Component that have all stats, it's an array
  34.         staminaSys = transform.root.GetComponentInChildren<staminaSystem>();
  35.         findStamina();  // this is used to find the Stamina Value from the array of StatInfo array inside the PlayerStatsSystem component
  36.         maxStamina = staminaInfo.currentValue;
  37.         currentStamina = staminaSys.staminaAmount;
  38.         oldValue = currentStamina;
  39.         UpdateUI();
  40.     }
  41.  
  42.     private void FixedUpdate()
  43.     {
  44.         if (oldValue != staminaSys.staminaAmount)
  45.             UpdateUI();
  46.     }
  47.  
  48.     public void UpdateUI()
  49.     {
  50.         // Debug.Log(currentStamina +"/"+ maxStamina);
  51.         currentStamina = staminaSys.staminaAmount;
  52.         guiBar.Value = currentStamina / maxStamina; // So we have value with a range 0 to 1
  53.  
  54.         oldValue = currentStamina;
  55.     }
  56.  
  57.  
  58.     private void findStamina()
  59.     {
  60.         for (int i = 0; i < statsSys.statInfoList.Count; i++)
  61.         {
  62.             if (statsSys.statInfoList[i].Name == "Stamina")
  63.                 staminaInfo = statsSys.statInfoList[i];
  64.         }
  65.     }
  66. }
RAW Paste Data