Advertisement
evelynshilosky

PlayerState - Part 31

Feb 2nd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class PlayerState : MonoBehaviour
  7. {    
  8.     public static PlayerState Instance { get; set; }
  9.  
  10.     // Inventory Item placeholders
  11.     public void setHealth(float health)
  12.     {
  13.         currentHealth = health;
  14.     }
  15.     public void setCalories(float calories)
  16.     {
  17.         currentCalories = calories;
  18.     }
  19.     public void setHydration(float hydration)
  20.     {
  21.         currentHydrationPercent = hydration;
  22.     }
  23.  
  24.     // ---------------------------------------------------------------------------------------------------------------------------------------------------------\\
  25.  
  26.  
  27.     // --- Player Health --- \\
  28.     public float currentHealth;
  29.     public float maxHealth;
  30.  
  31.  
  32.     // --- Player Calories --- \\
  33.     public float currentCalories;
  34.     public float maxCalories;
  35.  
  36.     float distanceTraveled = 0;
  37.     Vector3 lastPosition;
  38.  
  39.     public GameObject playerBody;
  40.  
  41.  
  42.     // --- Player Hydration --- \\
  43.     public float currentHydrationPercent;
  44.     public float maxHydrationPercent;
  45.  
  46.     public bool isHydrationActive;
  47.  
  48.  
  49.     private void Awake()
  50.     {
  51.         if (Instance != null && Instance != this)
  52.         {
  53.             Destroy(gameObject);
  54.         }
  55.         else
  56.         {
  57.             Instance = this;
  58.         }
  59.     }
  60.  
  61.     private void Start()
  62.     {
  63.         currentHealth = maxHealth;
  64.         currentCalories = maxCalories +10;
  65.         currentHydrationPercent = maxHydrationPercent +1;
  66.  
  67.         StartCoroutine(decreaseHydration());
  68.     }
  69.  
  70.     IEnumerator decreaseHydration()
  71.     {
  72.         while (true)
  73.         {
  74.             currentHydrationPercent -= 1;
  75.             yield return new WaitForSeconds(10);
  76.         }
  77.     }
  78.  
  79.  
  80.  
  81.     // Update is called once per frame
  82.     void Update()
  83.     {
  84.         // Calories Bar
  85.         distanceTraveled += Vector3.Distance(playerBody.transform.position, lastPosition);
  86.         lastPosition = playerBody.transform.position;
  87.  
  88.         if (distanceTraveled >=5) //I changed the amount from the video
  89.         {
  90.             distanceTraveled = 0;
  91.             currentCalories -= 10; //I changed the amount from the video (I also added 10 in the start method to keep the calories correct at the start)
  92.         }
  93.  
  94.  
  95.         // Testing the health bar
  96.         if (Input.GetKeyDown(KeyCode.N))
  97.         {
  98.             currentHealth -= 10;
  99.         }
  100.     }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement