using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyHealth : MonoBehaviour { [SerializeField] private int startingHealth = 3; // The initial health of the enemy [SerializeField] private GameObject deathVFXPrefab; // Prefab for the visual effects when the enemy dies [SerializeField] private float knockBackThrust = 15f; // Force applied to the enemy when knocked back private int currentHealth; // The enemy's current health private Knockback knockback; // Reference to the knockback component private Flash flash; // Reference to the flash component for damage feedback private void Awake() { flash = GetComponent(); // Initialize the flash component knockback = GetComponent(); // Initialize the knockback component } private void Start() { currentHealth = startingHealth; // Set the enemy's health to its starting value } // Method to handle damage taken by the enemy public void TakeDamage(int damage) { currentHealth -= damage; // Reduce the enemy's health by the damage amount knockback.GetKnockedBack(PlayerController.Instance.transform, knockBackThrust); // Apply knockback effect StartCoroutine(flash.FlashRoutine()); // Trigger the flash effect to indicate damage StartCoroutine(CheckDetectDeathRoutine()); // Check if the enemy has died after damage } // Coroutine to wait for the flash effect before checking for death private IEnumerator CheckDetectDeathRoutine() { yield return new WaitForSeconds(flash.GetRestoreMatTime()); // Wait for the flash duration DetectDeath(); // Check if the enemy is dead and handle it } // Method to handle the enemy's death public void DetectDeath() { if (currentHealth <= 0) { // If the enemy's health is zero or below Instantiate(deathVFXPrefab, transform.position, Quaternion.identity); // Instantiate the death visual effects GetComponent().DropItems(); // Spawn any items the enemy should drop Destroy(gameObject); // Destroy the enemy game object } } }