using System; using Data; using Events; using TMPro; using UnityEngine; using UnityEngine.UI; namespace UI { public class HealthBar : MonoBehaviour { [Header("Events")] [SerializeField] private EventBroker eventBroker; [Header("Values")] [SerializeField] private FloatValue maxHealth; [SerializeField] private FloatValue health; [Header("References")] [SerializeField] private Image fillingImage; [SerializeField] private TextMeshProUGUI deadText; private void Awake() { fillingImage.type = Image.Type.Filled; fillingImage.fillMethod = Image.FillMethod.Horizontal; deadText.gameObject.SetActive(false); } private void OnEnable() { eventBroker.PlayerDied += OnPlayerDied; } private void OnDisable() { eventBroker.PlayerDied -= OnPlayerDied; } private void Update() { fillingImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Clamp01(health.Value / maxHealth.Value) * 200.0f); } private void OnPlayerDied() { fillingImage.fillAmount = 0.0f; fillingImage.color = Color.red; deadText.gameObject.SetActive(true); } } }