Advertisement
evelynshilosky

PlayerState - Part 11

Jun 3rd, 2024
82
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.     private void Awake()
  49.     {
  50.         if (Instance != null && Instance != this)
  51.         {
  52.             Destroy(gameObject);
  53.         }
  54.         else
  55.         {
  56.             Instance = this;
  57.         }
  58.     }
  59.  
  60.     private void Start()
  61.     {
  62.         currentHealth = maxHealth;
  63.         currentCalories = maxCalories +10;
  64.         currentHydrationPercent = maxHydrationPercent +1;
  65.  
  66.         StartCoroutine(decreaseHydration());
  67.     }
  68.  
  69.     IEnumerator decreaseHydration()
  70.     {
  71.         while (true)
  72.         {
  73.             currentHydrationPercent -= 1;
  74.             yield return new WaitForSeconds(10);
  75.         }
  76.     }
  77.  
  78.  
  79.  
  80.     // Update is called once per frame
  81.     void Update()
  82.     {
  83.         // Calories Bar
  84.         distanceTraveled += Vector3.Distance(playerBody.transform.position, lastPosition);
  85.         lastPosition = playerBody.transform.position;
  86.  
  87.         if (distanceTraveled >=5) //I changed the amount from the video
  88.         {
  89.             distanceTraveled = 0;
  90.             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)
  91.         }
  92.  
  93.  
  94.         // Testing the health bar
  95.         if (Input.GetKeyDown(KeyCode.N))
  96.         {
  97.             currentHealth -= 10;
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement