using UnityEngine;
using System;
///
/// Enemy health handler.
/// Uses HealthComponent for health management.
///
[RequireComponent(typeof(HealthComponent))]
public class EnemyHealth : MonoBehaviour
{
private HealthComponent _health;
///
/// Event fired when this enemy dies.
///
public event Action OnEnemyDeath;
void Awake()
{
_health = GetComponent();
// Subscribe to health component events
_health.OnDamaged += HandleDamaged;
_health.OnDeath += HandleDeath;
}
void OnDestroy()
{
if (_health != null)
{
_health.OnDamaged -= HandleDamaged;
_health.OnDeath -= HandleDeath;
}
}
private void HandleDamaged(DamageInfo info)
{
Debug.Log($"{gameObject.name} took {info.Amount} damage. Remaining: {_health.CurrentHealth}");
}
private void HandleDeath()
{
Debug.Log($"{gameObject.name} died!");
// Fire event for external listeners (score system, spawner, etc.)
OnEnemyDeath?.Invoke();
// Destroy the enemy
Destroy(gameObject);
}
///
/// Get the health component for direct access if needed.
///
public HealthComponent Health => _health;
///
/// Get the current health (convenience property).
///
public float CurrentHealth => _health != null ? _health.CurrentHealth : 0f;
///
/// Get the max health (convenience property).
///
public float MaxHealth => _health != null ? _health.MaxHealth : 0f;
}