Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class HealthManager : MonoBehaviour
- {
- public int maxPlayerHealth; // How much health the player can have
- public static int currentPlayerHealth; // How much health the player has
- public Slider healthBar;
- //[SerializeField] private Text text; // [SerializeField] to make it visible in the inspector window given the advice by JimsGamez on GamesPlusJanes Discord Server
- [SerializeField] private LevelManager levelManager; // [SerializeField] to make it visible in the inspector window given the advice by JimsGamez on GamesPlusJanes Discord Server
- public bool isDead; // Boolean to check if the player is dead
- [SerializeField] private LifeManager lifeSystem; // life stystem for adding and taking life
- [SerializeField] private TimeManager theTimeManager; // TimeManager for adding and taking time
- // Start is called before the first frame update
- void Start() //Start()
- {
- /*text = GetComponent<Text>(); // Get the health text component attached to this GameObject
- if (text == null)
- {
- // Debug.LogError("HealthManager: Text component not found!");
- return;
- }*/
- healthBar = GetComponent<Slider>(); // Get the health bar component attached to this GameObject
- if (healthBar == null)
- {
- //Debug.LogError("HealthManager: Slider component not found!");
- return;
- }
- currentPlayerHealth = PlayerPrefs.GetInt("PlayerCurrentHealth"); // Get the player's health from the PlayerPrefs
- //maxPlayerHealth = PlayerPrefs.GetInt("PlayerMaxHealth"); // Get the player's max health from the PlayerPrefs
- //currentPlayerHealth = maxPlayerHealth; // Set the player's health
- theTimeManager = GameObject.FindObjectOfType<TimeManager>(); // Find the TimeManager
- levelManager = GameObject.FindObjectOfType<LevelManager>(); // Find the LevelManager
- lifeSystem = FindObjectOfType<LifeManager>(); // Find the LifeManager
- isDead = false;// Set isDead to false
- if (levelManager == null)
- {
- Debug.LogError("HealthManager: LevelManager not found!");
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (currentPlayerHealth <= 0 && !isDead) // If the player's health is less than or equal to 0
- {
- isDead = true; // Set isDead to true
- currentPlayerHealth = 0; // Set the player's health to 0
- levelManager.RespawnPlayer(); // Respawn the player not being called
- lifeSystem.TakeLife();// Take a life from the life system when dead
- Debug.Log("TakeLife called"); // Add this line to check if TakeLife is being called
- Debug.Log("RespawnPlayer called"); // Add this line to check if RespawnPlayer is being called
- theTimeManager.ResetTime(); // Add time when the player dies
- }
- /*if (text != null) // If the health text component exists
- {
- text.text = "" + currentPlayerHealth; // Update the health text
- }*/
- // healthBar.value = currentPlayerHealth; // Update the health bar
- if (healthBar != null)
- {
- healthBar.maxValue = maxPlayerHealth; // Update the max health bar
- healthBar.value = currentPlayerHealth; // Update the health bar
- //Debug.LogError("HealthManager: Slider component not found!");
- return;
- }
- }
- public void TakeDamage(int damageToGive) // player Takes damage
- {
- Debug.Log("TakeDamage called"); // Add this line to check if TakeDamage is being called
- currentPlayerHealth -= damageToGive; // Subtract the damage from the player's health
- PlayerPrefs.SetInt("PlayerCurrentHealth", currentPlayerHealth); // Set the player's health in the PlayerPrefs
- Debug.Log("currentPlayerHealth -= damageToGive is called"); // Add this line to check if currentPlayerHealth -= damageToGive is being called
- }
- public void FullHealth() // Full heal
- {
- currentPlayerHealth = PlayerPrefs.GetInt("PlayerMaxHealth"); // Set the player's health to max
- PlayerPrefs.SetInt("PlayerCurrentHealth", currentPlayerHealth); // Set the player's health in the PlayerPrefs
- Debug.Log("FullHealth called"); // Add this line to check if FullHealth is being called
- }
- public void KillPlayer()
- {
- currentPlayerHealth = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement