Implemented health bar and diamond count in the new gui

This commit is contained in:
Max 2025-02-28 13:20:53 +01:00
parent 105055b307
commit c42406e615
14 changed files with 668 additions and 505 deletions

View file

@ -5,10 +5,11 @@ using UnityEngine.Serialization;
public class Player : MonoBehaviour
{
public event Action<int> DiamondCountUpdated;
public event Action<int> HealthUpdated;
[SerializeField] private int maxHealth;
[SerializeField] private int diamonds;
[FormerlySerializedAs("healthGameObject")] [SerializeField] private GameObject healthGO;
[FormerlySerializedAs("DiamondsGO")] [SerializeField] private GameObject diamondsGO;
[SerializeField] private GameObject rooms;
private int _healthIndex = 0;
private readonly int[] _healthBar = {0, 0, -1, -2, -4, -6, -9, -12, -16, -20};
@ -56,8 +57,8 @@ public class Player : MonoBehaviour
private void UpdateGUI()
{
healthGO.GetComponent<TextMeshProUGUI>().text = _healthBar[_healthIndex].ToString();
diamondsGO.GetComponent<TextMeshProUGUI>().text = diamonds.ToString();
DiamondCountUpdated?.Invoke(diamonds);
HealthUpdated?.Invoke(_healthIndex);
}
private void HandleDiamondAndLifeAbilitySelected()

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
@ -46,6 +47,10 @@ namespace UI
private List<Toggle> _armorUses = new List<Toggle>();
private int _armorUsesUsed = 0;
private List<VisualElement> _healthAreas;
private Label diamondCount;
public void OnEnable()
@ -58,7 +63,10 @@ namespace UI
GroupBox startingBlackDieGroupBox = root.Q<GroupBox>("StartingBlackDieUses");
startingBlackDieGroupBox.AddManipulator(new Clickable(evt => HandleStartingBlackDieAbilityClicked()));
GroupBox diceGroupBox = root.Q<GroupBox>("Dice");
VisualElement healthArea = root.Q<VisualElement>("HealthArea");
_healthAreas = healthArea.Children().ToList();
diamondCount = root.Q<Label>("DiamondCount");
foreach (Button diceButton in diceGroupBox.Children())
{
if (diceButton.name == "BlackDie")
@ -113,6 +121,26 @@ namespace UI
_rollOrPassButton = root.Q<Button>("RollPass");
_rollOrPassButton.clicked += HandleRollOrPassClicked;
}
public void SetDiamondCount(int count)
{
diamondCount.text = count.ToString();
}
public void SetHealth(int healthIndex)
{
for (int i = 0; i < _healthAreas.Count; i++)
{
if (i == healthIndex)
{
_healthAreas[i].SetEnabled(true);
}
else
{
_healthAreas[i].SetEnabled(false);
}
}
}
public void SetBlackDieAbilityEnabled()
{

View file

@ -20,6 +20,7 @@ namespace UI
[SerializeField] private BlackDieAbility blackDieAbility;
[SerializeField] private ChestRewardSelection chestRewardSelection;
[SerializeField] private TorchAbility torchAbility;
[SerializeField] private Player player;
public static UIManager Instance { get; private set; }
@ -69,6 +70,9 @@ namespace UI
torchAbility.AbilityGained += hudController.SetKeyAbilityEnabled;
TorchAbility.TorchAbilityUsed += hudController.MarkKeyAbilityUsed;
hudController.KeyAbilityClicked += torchAbility.UseClicked;
player.DiamondCountUpdated += hudController.SetDiamondCount;
player.HealthUpdated += hudController.SetHealth;
}
private void OnDisable()