코드 리팩토링
재사용성 및 확장성을 고려하여 코드 전반을 리팩토링함
This commit is contained in:
@@ -1,33 +1,66 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class EnemyHealth : MonoBehaviour, IDamageable
|
||||
/// <summary>
|
||||
/// Enemy health handler.
|
||||
/// Uses HealthComponent for health management.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(HealthComponent))]
|
||||
public class EnemyHealth : MonoBehaviour
|
||||
{
|
||||
public float maxHealth = 50f;
|
||||
private float _currentHealth;
|
||||
private HealthComponent _health;
|
||||
|
||||
void Start()
|
||||
/// <summary>
|
||||
/// Event fired when this enemy dies.
|
||||
/// </summary>
|
||||
public event Action OnEnemyDeath;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_currentHealth = maxHealth;
|
||||
_health = GetComponent<HealthComponent>();
|
||||
|
||||
// Subscribe to health component events
|
||||
_health.OnDamaged += HandleDamaged;
|
||||
_health.OnDeath += HandleDeath;
|
||||
}
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
void OnDestroy()
|
||||
{
|
||||
_currentHealth -= damage;
|
||||
Debug.Log($"{gameObject.name} 남은 체력: {_currentHealth}");
|
||||
|
||||
// 데미지 입었을 때 반짝이는 효과를 여기서 호출해도 좋습니다.
|
||||
// 예: GetComponent<EnemyAttack>().StartFlash();
|
||||
|
||||
if (_currentHealth <= 0)
|
||||
if (_health != null)
|
||||
{
|
||||
Die();
|
||||
_health.OnDamaged -= HandleDamaged;
|
||||
_health.OnDeath -= HandleDeath;
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
private void HandleDamaged(DamageInfo info)
|
||||
{
|
||||
Debug.Log($"{gameObject.name} 사망!");
|
||||
// 여기서 파티클 생성이나 점수 추가 등을 처리합니다.
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user