IdleGirl/Assets/2_Codes/MonsterHealth.cs

65 lines
1.3 KiB
C#
Raw Normal View History

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; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>̹<EFBFBD><CCB9><EFBFBD>
// IsDead <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƽ <20><><EFBFBD><EFBFBD>
public bool IsDead { get; private set; }
private void Start()
{
currentHealth = maxHealth;
UpdateHealthBar();
IsDead = false; // <20>ʱⰪ<CAB1><E2B0AA> <20><><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD> <20><><EFBFBD><EFBFBD>
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
UpdateHealthBar();
if (currentHealth <= 0)
{
Die(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> ó<><C3B3>
}
}
void UpdateHealthBar()
{
float healthRatio = (float)currentHealth / maxHealth;
healthBar.fillAmount = healthRatio;
MonsterHp.text = string.Format("HP {0}/{1}", currentHealth, maxHealth);
}
// <20><><EFBFBD><EFBFBD> ó<><C3B3> <20>޼<EFBFBD><DEBC><EFBFBD>
void Die()
{
IsDead = true;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>õ<EFBFBD> <20>߰<EFBFBD> ó<><C3B3><EFBFBD><EFBFBD> <20><><EFBFBD><20><><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>׸<EFBFBD><D7B8><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <20><><EFBFBD><EFBFBD>
if (monsterShdow != null)
{
Destroy(monsterShdow);
}
Destroy(gameObject);
}
}