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