82 lines
1.9 KiB
C#
82 lines
1.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class RaftHealth : MonoBehaviour
|
|
{
|
|
[Header("Health")]
|
|
[SerializeField] private int maxHealth = 100;
|
|
[SerializeField] private int currentHealth = 100;
|
|
|
|
[Header("Options")]
|
|
[SerializeField] private bool resetHealthOnStart = true;
|
|
|
|
[Header("Events")]
|
|
public UnityEvent<int, int> onHealthChanged;
|
|
public UnityEvent onDead;
|
|
|
|
public int MaxHealth => maxHealth;
|
|
public int CurrentHealth => currentHealth;
|
|
public bool IsDead => currentHealth <= 0;
|
|
|
|
private void Start()
|
|
{
|
|
if (resetHealthOnStart)
|
|
{
|
|
ResetHealth();
|
|
}
|
|
}
|
|
|
|
public void ResetHealth()
|
|
{
|
|
currentHealth = maxHealth;
|
|
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
|
|
|
|
Debug.Log($"[RaftHealth] 체력 초기화: {currentHealth}/{maxHealth}");
|
|
|
|
onHealthChanged?.Invoke(currentHealth, maxHealth);
|
|
}
|
|
|
|
public void TakeDamage(int damage)
|
|
{
|
|
if (damage <= 0)
|
|
return;
|
|
|
|
if (IsDead)
|
|
return;
|
|
|
|
currentHealth -= damage;
|
|
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
|
|
|
|
Debug.Log($"[RaftHealth] 데미지 {damage} 받음. 현재 체력: {currentHealth}/{maxHealth}");
|
|
|
|
onHealthChanged?.Invoke(currentHealth, maxHealth);
|
|
|
|
if (currentHealth <= 0)
|
|
{
|
|
Die();
|
|
}
|
|
}
|
|
|
|
public void Heal(int amount)
|
|
{
|
|
if (amount <= 0)
|
|
return;
|
|
|
|
if (IsDead)
|
|
return;
|
|
|
|
currentHealth += amount;
|
|
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
|
|
|
|
Debug.Log($"[RaftHealth] 회복 {amount}. 현재 체력: {currentHealth}/{maxHealth}");
|
|
|
|
onHealthChanged?.Invoke(currentHealth, maxHealth);
|
|
}
|
|
|
|
private void Die()
|
|
{
|
|
Debug.Log("[RaftHealth] 체력이 0이 되었습니다. 뗏목 실패 처리.");
|
|
|
|
onDead?.Invoke();
|
|
}
|
|
} |