Files
ProjectMD/Assets/Scripts/Enemy/EnemyHealth.cs
Dal4segno db5db4b106 코드 리팩토링
재사용성 및 확장성을 고려하여 코드 전반을 리팩토링함
2026-01-21 01:45:15 +09:00

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;
}