Files
ggj2026/Assets/Scripts/HealthGUIController.cs
T
2026-02-01 13:21:59 -06:00

85 lines
3.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
using static UnityEditor.Experimental.GraphView.GraphView;
public class HealthGUIController : MonoBehaviour
{
private List<PlayerStressController> players;
public List<Sprite> images;
private UIDocument uiDocument;
private List<Image> playerLifeList;
private List<ProgressBar> playerStressBarList;
void Awake()
{
players = Object.FindObjectsByType<PlayerStressController>(FindObjectsSortMode.InstanceID).ToList();
uiDocument ??= GetComponent<UIDocument>();
if (uiDocument == null)
{
return;
}
var root = uiDocument.rootVisualElement;
if (root == null)
{
return;
}
playerLifeList = new List<Image>() { new Image(), new Image(), new Image(), new Image() };
playerStressBarList = new List<ProgressBar>() { new ProgressBar(), new ProgressBar(), new ProgressBar(), new ProgressBar() };
playerLifeList[0] = root.Q<Image>("Player1Life");
playerLifeList[1] = root.Q<Image>("Player2Life");
playerLifeList[2] = root.Q<Image>("Player3Life");
playerLifeList[3] = root.Q<Image>("Player4Life");
playerLifeList[0].tintColor = players[0].gameObject.GetComponent<SpriteRenderer>() ? players[0].gameObject.GetComponent<SpriteRenderer>().color : Color.white;
playerLifeList[1].tintColor = players[1].gameObject.GetComponent<SpriteRenderer>() ? players[1].gameObject.GetComponent<SpriteRenderer>().color : Color.white;
playerLifeList[2].tintColor = players[2].gameObject.GetComponent<SpriteRenderer>() ? players[2].gameObject.GetComponent<SpriteRenderer>().color : Color.white;
playerLifeList[3].tintColor = players[3].gameObject.GetComponent<SpriteRenderer>() ? players[3].gameObject.GetComponent<SpriteRenderer>().color : Color.white;
playerStressBarList[0] = root.Q<ProgressBar>("Player1StressBar");
playerStressBarList[1] = root.Q<ProgressBar>("Player2StressBar");
playerStressBarList[2] = root.Q<ProgressBar>("Player3StressBar");
playerStressBarList[3] = root.Q<ProgressBar>("Player4StressBar");
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < players.Count; i++)
{
//update stress bar
float ratio = Mathf.Clamp01(players[i].currentStress / players[i].maxStress);
// Update the bar's value (0-100)
playerStressBarList[i].value = ratio * 100f;
//update sprite
playerLifeList[i].sprite = players[i].currentStress switch
{
>= 0 and <= 33 => images[0],
> 33 and <= 66 => images[1],
> 66 and <= 99 => images[2],
_ => images[0]
};
if (players[i].isParalyzed)
{
playerLifeList[i].sprite = images[3];
}
else if (players[i].isPanicking)
{
playerLifeList[i].sprite = images[4];
}
}
}
}