50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using UnityEngine;
|
|
using Unity.Collections;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
//Thomas09
|
|
|
|
public class PlayerHealthBar : MonoBehaviour
|
|
{
|
|
[SerializeField] private Image healthImage;
|
|
[SerializeField] private Image damageEffectImage;
|
|
[SerializeField] private TMP_Text healthText;
|
|
private float maxHealth;
|
|
private float damageEffectSpeed = 0.25f;
|
|
|
|
public void ChangeMaxHealth(int newMaxhHealth, int currentHealth)
|
|
{
|
|
maxHealth = newMaxhHealth;
|
|
ChangeHealth(currentHealth);
|
|
}
|
|
|
|
public void ChangeHealth(int currentHealth)
|
|
{
|
|
//Default behavior so it won't break
|
|
maxHealth = Mathf.Clamp(maxHealth, 0f, 6000f);
|
|
|
|
float percent = Mathf.Clamp((float)currentHealth / maxHealth, 0f, 1f);
|
|
|
|
healthImage.fillAmount = percent;
|
|
|
|
if (healthImage.fillAmount > damageEffectImage.fillAmount)
|
|
{
|
|
damageEffectImage.fillAmount = healthImage.fillAmount;
|
|
}
|
|
|
|
healthText.SetText($"{currentHealth}HP");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (damageEffectImage.fillAmount > healthImage.fillAmount)
|
|
{
|
|
damageEffectImage.fillAmount -= damageEffectSpeed * Time.deltaTime;
|
|
}
|
|
else
|
|
{
|
|
damageEffectImage.fillAmount = healthImage.fillAmount;
|
|
}
|
|
}
|
|
} |