IdleGirl/Assets/2_Codes/MonsterHealth.cs

65 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using TMPro;
public class MonsterHealth : MonoBehaviour
{
public int maxHealth = 10;
public GameObject monsterShdow;
private int currentHealth;
public TextMeshProUGUI MonsterHp;
public int CurrentHealth
{
get { return currentHealth; }
}
public Image healthBar; // 게이지 이미지
// IsDead 프로퍼티 선언
public bool IsDead { get; private set; }
private void Start()
{
currentHealth = maxHealth;
UpdateHealthBar();
IsDead = false; // 초기값은 살아있는 상태
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
UpdateHealthBar();
if (currentHealth <= 0)
{
Die(); // 몬스터 사망 처리
}
}
void UpdateHealthBar()
{
float healthRatio = (float)currentHealth / maxHealth;
healthBar.fillAmount = healthRatio;
MonsterHp.text = string.Format("HP {0}/{1}", currentHealth, maxHealth);
}
// 사망 처리 메서드
void Die()
{
IsDead = true;
// 사망에 관련된 추가 처리를 여기에 구현
// 몬스터 그림자 오브젝트 제거
if (monsterShdow != null)
{
Destroy(monsterShdow);
}
Destroy(gameObject);
}
}