using System.Collections; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class PlayerHealth : Singleton { // Public property to check if the player is dead public bool isDead { get; private set; } public GameManager gameManager; // Serialized fields visible in the Unity Inspector [SerializeField] private int maxHealth = 3; [SerializeField] private float knockBackThrustAmount = 10f; [SerializeField] private float damageRecoveryTime = 1f; // Private variables for health management private Slider healthSlider; private int currentHealth; private bool canTakeDamage = true; private Knockback knockback; private Flash flash; // Constants for scene and UI references const string HEALTH_SLIDER_TEXT = "Health Slider"; const string TOWN_TEXT = "Scene1"; readonly int DEATH_HASH = Animator.StringToHash("Death"); protected override void Awake() { base.Awake(); // Initialize references flash = GetComponent(); knockback = GetComponent(); } private void Start() { // Initialize health values isDead = false; currentHealth = maxHealth; // Update health slider UI UpdateHealthSlider(); } private void OnCollisionStay2D(Collision2D other) { // Damage player on collision with enemy EnemyAI enemy = other.gameObject.GetComponent(); if (enemy) { TakeDamage(1, other.transform); } } public void HealPlayer() { // Heal player if not at max health if (currentHealth < maxHealth) { currentHealth += 1; UpdateHealthSlider(); } } public void TakeDamage(int damageAmount, Transform hitTransform) { // Take damage if the player can take damage if (!canTakeDamage) { return; } // Perform damage effects ScreenShakeManager.Instance.ShakeScreen(); knockback.GetKnockedBack(hitTransform, knockBackThrustAmount); StartCoroutine(flash.FlashRoutine()); canTakeDamage = false; currentHealth -= damageAmount; StartCoroutine(DamageRecoveryRoutine()); UpdateHealthSlider(); CheckIfPlayerDeath(); } private void CheckIfPlayerDeath() { // Check if player health is zero or below and initiate death sequence if (currentHealth <= 0 && !isDead) { isDead = true; // Reset the player's gold through EconomyManager EconomyManager.Instance?.ResetGold(); if (gameManager != null) { gameManager.GameOver(); // Call GameOver } // Check if ActiveWeapon.Instance and its gameObject are valid if (ActiveWeapon.Instance != null && ActiveWeapon.Instance.gameObject != null) { Destroy(ActiveWeapon.Instance.gameObject); // Destroy weapon on death } currentHealth = 0; GetComponent().SetTrigger(DEATH_HASH); // Trigger death animation StartCoroutine(DeathLoadSceneRoutine()); // Load death scene after delay } } private IEnumerator DeathLoadSceneRoutine() { // Delay before loading town scene after player death yield return new WaitForSeconds(3f); // Wait for 3 seconds before restarting Destroy(gameObject); // Destroy player object SceneManager.LoadScene(TOWN_TEXT); // Load town scene } private IEnumerator DamageRecoveryRoutine() { // Delay before player can take damage again yield return new WaitForSeconds(damageRecoveryTime); canTakeDamage = true; } private void UpdateHealthSlider() { // Update the UI health slider with current health values if (healthSlider == null) { healthSlider = GameObject.Find(HEALTH_SLIDER_TEXT).GetComponent(); } if (healthSlider != null) // Check if healthSlider is found { healthSlider.maxValue = maxHealth; healthSlider.value = currentHealth; } } }