Advertisement
johnnygoodguy2000

HealthManager.cs

Apr 28th, 2024 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class HealthManager : MonoBehaviour
  7. {
  8.     public int maxPlayerHealth; // How much health the player can have
  9.     public static int currentPlayerHealth; // How much health the player has
  10.  
  11.     public Slider healthBar;
  12.     //[SerializeField] private Text text; // [SerializeField] to make it visible in the inspector window given the advice by JimsGamez on GamesPlusJanes Discord Server
  13.     [SerializeField] private LevelManager levelManager; // [SerializeField] to make it visible in the inspector window given the advice by JimsGamez on GamesPlusJanes Discord Server
  14.    
  15.     public bool isDead; // Boolean to check if the player is dead
  16.     [SerializeField] private LifeManager lifeSystem; // life stystem for adding and taking life
  17.  
  18.     [SerializeField] private TimeManager theTimeManager; // TimeManager for adding and taking time
  19.  
  20.      
  21.  
  22.     // Start is called before the first frame update
  23.     void Start()  //Start()
  24.     {
  25.         /*text = GetComponent<Text>(); // Get the health text component attached to this GameObject
  26.         if (text == null)
  27.         {
  28.            // Debug.LogError("HealthManager: Text component not found!");
  29.             return;
  30.         }*/
  31.  
  32.         healthBar = GetComponent<Slider>(); // Get the health bar component attached to this GameObject
  33.         if (healthBar == null)
  34.         {
  35.             //Debug.LogError("HealthManager: Slider component not found!");
  36.             return;
  37.         }
  38.  
  39.  
  40.  
  41.         currentPlayerHealth = PlayerPrefs.GetInt("PlayerCurrentHealth"); // Get the player's health from the PlayerPrefs
  42.         //maxPlayerHealth = PlayerPrefs.GetInt("PlayerMaxHealth"); // Get the player's max health from the PlayerPrefs
  43.  
  44.         //currentPlayerHealth = maxPlayerHealth; // Set the player's health
  45.  
  46.         theTimeManager = GameObject.FindObjectOfType<TimeManager>(); // Find the TimeManager
  47.         levelManager = GameObject.FindObjectOfType<LevelManager>(); // Find the LevelManager
  48.  
  49.         lifeSystem = FindObjectOfType<LifeManager>(); // Find the LifeManager
  50.  
  51.  
  52.         isDead = false;// Set isDead to false
  53.  
  54.  
  55.         if (levelManager == null)
  56.         {
  57.             Debug.LogError("HealthManager: LevelManager not found!");
  58.         }
  59.     }
  60.  
  61.     // Update is called once per frame
  62.     void Update()
  63.     {
  64.         if (currentPlayerHealth <= 0 && !isDead) // If the player's health is less than or equal to 0
  65.         {
  66.             isDead = true; // Set isDead to true  
  67.  
  68.             currentPlayerHealth = 0; // Set the player's health to 0
  69.             levelManager.RespawnPlayer();   // Respawn the player not being called
  70.  
  71.               lifeSystem.TakeLife();// Take a life from the life system when dead
  72.             Debug.Log("TakeLife called"); // Add this line to check if TakeLife is being called
  73.             Debug.Log("RespawnPlayer called"); // Add this line to check if RespawnPlayer is being called
  74.  
  75.             theTimeManager.ResetTime(); // Add time when the player dies
  76.          
  77.  
  78.  
  79.  
  80.            
  81.  
  82.  
  83.         }
  84.         /*if (text != null) // If the health text component exists
  85.         {
  86.             text.text = "" + currentPlayerHealth; // Update the health text
  87.         }*/
  88.  
  89.      //   healthBar.value = currentPlayerHealth; // Update the health bar
  90.         if (healthBar != null)
  91.         {
  92.             healthBar.maxValue = maxPlayerHealth; // Update the max health bar
  93.  
  94.             healthBar.value = currentPlayerHealth; // Update the health bar
  95.        
  96.             //Debug.LogError("HealthManager: Slider component not found!");
  97.             return;
  98.         }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.     }
  105.  
  106.     public void TakeDamage(int damageToGive) //  player Takes damage
  107.    
  108.     {
  109.         Debug.Log("TakeDamage called"); // Add this line to check if TakeDamage is being called
  110.         currentPlayerHealth -= damageToGive;  // Subtract the damage from the player's health
  111.  
  112.         PlayerPrefs.SetInt("PlayerCurrentHealth", currentPlayerHealth); // Set the player's health in the PlayerPrefs
  113.  
  114.  
  115.         Debug.Log("currentPlayerHealth -= damageToGive is called"); // Add this line to check if currentPlayerHealth -= damageToGive is being called
  116.     }
  117.  
  118.     public void FullHealth() // Full heal
  119.     {
  120.         currentPlayerHealth = PlayerPrefs.GetInt("PlayerMaxHealth"); // Set the player's health to max
  121.  
  122.         PlayerPrefs.SetInt("PlayerCurrentHealth", currentPlayerHealth); // Set the player's health in the PlayerPrefs
  123.  
  124.        
  125.         Debug.Log("FullHealth called"); // Add this line to check if FullHealth is being called
  126.     }
  127.  
  128.     public void KillPlayer()
  129.     {
  130.         currentPlayerHealth = 0;
  131.     }
  132. }
  133.  
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement