67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// Enemy health handler.
|
|
/// Uses HealthComponent for health management.
|
|
/// </summary>
|
|
[RequireComponent(typeof(HealthComponent))]
|
|
public class EnemyHealth : MonoBehaviour
|
|
{
|
|
private HealthComponent _health;
|
|
|
|
/// <summary>
|
|
/// Event fired when this enemy dies.
|
|
/// </summary>
|
|
public event Action OnEnemyDeath;
|
|
|
|
void Awake()
|
|
{
|
|
_health = GetComponent<HealthComponent>();
|
|
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the health component for direct access if needed.
|
|
/// </summary>
|
|
public HealthComponent Health => _health;
|
|
|
|
/// <summary>
|
|
/// Get the current health (convenience property).
|
|
/// </summary>
|
|
public float CurrentHealth => _health != null ? _health.CurrentHealth : 0f;
|
|
|
|
/// <summary>
|
|
/// Get the max health (convenience property).
|
|
/// </summary>
|
|
public float MaxHealth => _health != null ? _health.MaxHealth : 0f;
|
|
}
|