twinStick/twinStickCrawler/Assets/Scripts/Creatures/Vitals.cs
2024-12-24 21:40:18 +01:00

55 lines
1.2 KiB
C#

using Creatures;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
public class Vitals : MonoBehaviour
{
[SerializeField]
private float initHealth = 100;
[SerializeField]
private GameObject canvas;
[SerializeField]
private GameObject damagePrefab;
private IVitalReaction _reaction;
private float health;
public float Health
{
get
{
return health;
}
set
{
GameObject damageInstance = Instantiate(damagePrefab, canvas.transform);
damageInstance.transform.localPosition = Vector3.zero;
damageInstance.GetComponent<TextMeshProUGUI>().SetText(Mathf.Abs((int)value).ToString());
if (value < 0)
{
_reaction.Hit();
}
else
{
_reaction.Heal();
}
health += value;
}
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
health = initHealth;
}
void OnEnable()
{
_reaction = GetComponent<IVitalReaction>();
}
}