38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UI
|
|
{
|
|
public class HUDService : MonoBehaviour
|
|
{
|
|
[SerializeField] private UIDocument uiDocument;
|
|
private VisualElement player1Hp;
|
|
private VisualElement player2Hp;
|
|
|
|
private void Awake()
|
|
{
|
|
player1Hp = uiDocument.rootVisualElement.Q<VisualElement>("player1HP");
|
|
player2Hp = uiDocument.rootVisualElement.Q<VisualElement>("player2HP");
|
|
}
|
|
|
|
public void Register(Player.Player player)
|
|
{
|
|
var progressBar = player.playerIndex == 0 ? player1Hp : player2Hp;
|
|
progressBar.Q<Label>().text = player.playerIndex == 0 ? "BLOOD JOE" : "BONE JACK";
|
|
progressBar.style.visibility = Visibility.Visible;
|
|
progressBar.Q<ProgressBar>().highValue = player.controlledPawn.MaxHP;
|
|
progressBar.Q<ProgressBar>().value = player.controlledPawn.CurrentHP;
|
|
player.controlledPawn.HPUpdated += (newHp) =>
|
|
{
|
|
HandleUpdated(player.playerIndex, newHp);
|
|
};
|
|
}
|
|
|
|
private void HandleUpdated(int playerIndex, int newHP)
|
|
{
|
|
var progressBar = playerIndex == 0 ? player1Hp : player2Hp;
|
|
progressBar.Q<ProgressBar>().value = newHP;
|
|
}
|
|
}
|
|
}
|