using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class PlayerHealth : MonoBehaviour { [SerializeField] private HitFlash flashEffect; // Reference to the hit flash effect public int maxHealth = 100; // Maximum health of the player public int currentHealth; // Current health of the player public Animator myAnimator; // Animator for player animations public HealthBar healthBar; // Reference to the health bar UI public GameManagerScript gameManager; // Reference to the game manager [SerializeField] private int healthPotionAmount = 3; // Number of health potions the player has public int healAmount = 20; // Amount of health restored by a potion private bool isDead; // Flag to check if the player is dead public TextMeshProUGUI potionAmountText; // UI text to display the number of health potions private Vector3 checkpointPos; // Player's checkpoint position private int initialHealthPotionAmount; // Initial number of health potions public float deathCounter = 0; // Counter for the number of deaths public TextMeshProUGUI deathText; // UI text to display the death count AudioManager audioManager; // Reference to the audio manager void Awake() { // Initialize audio manager audioManager = GameObject.FindGameObjectWithTag("Audio").GetComponent(); } void Start() { // Initialize health and other variables myAnimator = GetComponent(); currentHealth = maxHealth; initialHealthPotionAmount = healthPotionAmount; checkpointPos = transform.position; healthBar.SetMaxHealth(maxHealth); // Set the maximum value for the health bar myAnimator.SetBool("isDead", false); // Ensure the player is not dead at the start UpdatePotionAmountUI(); // Update the potion amount display // Load the death count from PlayerPrefs if (PlayerPrefs.HasKey("DeathCount")) { deathCounter = PlayerPrefs.GetFloat("DeathCount"); } // Display the death count deathText.text = "Death Count: " + deathCounter; UpdateDeathText(); // Initialize death count text } void Update() { // Use health potion when 'H' key is pressed if (Input.GetKeyDown(KeyCode.H)) { StartCoroutine(HealthPotion()); } } // Detect collision with hazards private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.layer == LayerMask.NameToLayer("Hazards")) { TakeDamage(currentHealth); // Instantly kill the player } } // Method to handle taking damage public void TakeDamage(int damage) { currentHealth -= damage; // Decrease health by damage amount healthBar.SetHealth(currentHealth); // Update health bar // Check if the player is dead if (currentHealth <= 0 && !isDead) { audioManager.PlaySFX(audioManager.playerDeathSFX); // Play death sound isDead = true; // Set dead flag deathCounter++; // Increment death counter PlayerPrefs.SetFloat("DeathCount", deathCounter); // Save death count UpdateDeathText(); // Update death count display myAnimator.SetBool("isDead", true); // Trigger death animation gameManager.GameOver(); // Call game over method } else { flashEffect.Flash(); // Show hit flash effect } } // Coroutine to use a health potion private IEnumerator HealthPotion() { // Check if player is not dead and has potions if (!isDead && healthPotionAmount > 0) { flashEffect.Flash(); // Show hit flash effect audioManager.PlaySFX(audioManager.healPotionSFX); healthPotionAmount -= 1; // Decrease potion amount yield return new WaitForSeconds(1.5f); // Wait for potion effect duration currentHealth += healAmount; // Increase health by heal amount if (currentHealth > maxHealth) { currentHealth = maxHealth; // Cap health at maximum } healthBar.SetHealth(currentHealth); // Update health bar UpdatePotionAmountUI(); // Update potion amount display } } // Set a new checkpoint position and reset health public void SetNewCheckpoint(Vector3 newCheckpoint) { checkpointPos = newCheckpoint; // Update checkpoint position ResetHealth(); // Reset health and potions } // Reset health and potions to initial values public void ResetHealth() { currentHealth = maxHealth; // Reset health healthPotionAmount = initialHealthPotionAmount; // Reset potions healthBar.SetHealth(currentHealth); // Update health bar UpdatePotionAmountUI(); // Update potion amount display } // Update the UI text for potion amount private void UpdatePotionAmountUI() { potionAmountText.text = healthPotionAmount.ToString(); } // Update the UI text for death count private void UpdateDeathText() { deathText.text = "Death Count: " + deathCounter; } }