MIF_E31222586/Assets/Scripts/healthmanager.cs

59 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class healthmanager : MonoBehaviour
{
//public static int health = 3;
public int maxHealth = 3;
public int health;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
void Start()
{
health = maxHealth;
UpdateHealthUI();
}
public void TakeDamage(int amount)
{
health -= amount;
health = Mathf.Max(0, health);
UpdateHealthUI();
}
public void RestoreHealth(int amount) {
health += amount;
health = Mathf.Min(health, maxHealth);
UpdateHealthUI();
}
void UpdateHealthUI(){
if (hearts.Length == 0) return;
for (int i = 0; i < hearts.Length; i++){
if (i < health) {
hearts[i].sprite = fullHeart;
} else {
hearts[i].sprite = emptyHeart;
}
hearts[i].enabled = i < maxHealth;
}
}
public bool isAlive() {
return health > 0;
}
void Update()
{
/*foreach (Image img in hearts) {
img.sprite = emptyHeart;
} for (int i = 0; i < health; i++) {
hearts[i].sprite = fullHeart;
}*/
}
}