Projext/Assets/5.TestScript/EnemyHP Ui.cs
2026-01-29 15:58:38 +09:00

42 lines
1.1 KiB
C#

using UnityEngine;
using System;
public class EnemyHealth : MonoBehaviour, IDamageable
{
[Header("--- 능력치 ---")]
[SerializeField] private float maxHealth = 50f;
private float _currentHealth;
public event Action<float, float> OnHealthChanged;
public bool IsDead { get; private set; }
private void Start()
{
_currentHealth = maxHealth;
// ⭐ 시작하자마자 UI에 현재 숫자가 뜨도록 신호 발송
OnHealthChanged?.Invoke(_currentHealth, maxHealth);
}
// IDamageable 인터페이스 구현
public void TakeDamage(float amount)
{
if (IsDead) return;
_currentHealth = Mathf.Max(0, _currentHealth - amount);
// ⭐ 피격 시 로그와 함께 UI 갱신 신호 발송
Debug.Log($"{gameObject.name} 피격! 체력: {{{_currentHealth}/{maxHealth}}}");
OnHealthChanged?.Invoke(_currentHealth, maxHealth);
if (_currentHealth <= 0) Die();
}
private void Die()
{
if (IsDead) return;
IsDead = true;
Debug.Log($"{gameObject.name} 사망!");
Destroy(gameObject, 1.5f);
}
}