Some Nice UI

This commit is contained in:
Walter
2026-02-01 13:21:59 -06:00
parent 095296d5c2
commit 2814b6c7a2
22 changed files with 433 additions and 157 deletions
+84
View File
@@ -0,0 +1,84 @@
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];
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 09397b12132be4248998c82e96a8b7c4
+37
View File
@@ -0,0 +1,37 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
public class LoseGuiController : MonoBehaviour
{
private UIDocument uiDocument;
private Button button;
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void OnEnable()
{
uiDocument ??= GetComponent<UIDocument>();
if (uiDocument == null)
{
return;
}
var root = uiDocument.rootVisualElement;
if (root == null)
{
return;
}
button = root.Q<Button>("Button");
button.clicked += () => HandleClick("MainMenu");
}
private void HandleClick(string sceneName)
{
if (!string.IsNullOrWhiteSpace(sceneName))
{
SceneManager.LoadScene(sceneName);
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9c38ae32eef77b54bb36b487e11c7422
+3 -5
View File
@@ -8,19 +8,18 @@ public class LoseScreenController : MonoBehaviour
[SerializeField]private const string LoseSceneName = "Lose";
private List<PlayerStressController> players;
private bool hasLost = false;
void Awake()
{
players = FindObjectsOfType<PlayerStressController>().ToList();
players = Object.FindObjectsByType<PlayerStressController>(FindObjectsSortMode.None).ToList();
}
void Update()
{
if (hasLost || players.Count == 0)
if (players.Count == 0)
return;
if (players.All(p => p.IsPanicking))
if (players.All(p => p.isPanicking))
{
TriggerLose();
}
@@ -28,7 +27,6 @@ public class LoseScreenController : MonoBehaviour
void TriggerLose()
{
hasLost = true;
SceneManager.LoadScene(LoseSceneName);
}
}
+16 -5
View File
@@ -23,14 +23,17 @@ public class PlayerPickupController : MonoBehaviour
public Transform holdPoint; // Empty GameObject above head
public float pickupRange = 2.0f;
private GameObject heldObject;
public GameObject heldObject;
private PlayerPlatformerController platformerController;
private PlayerStressController playerStressController;
private bool isChargingThrow;
private float throwChargeStartTime;
private void OnEnable()
{
platformerController = GetComponent<PlayerPlatformerController>();
playerStressController = GetComponent<PlayerStressController>();
if (inputActions == null)
{
Debug.LogError("PlayerPickupController missing InputActionAsset reference.", this);
@@ -76,10 +79,10 @@ public class PlayerPickupController : MonoBehaviour
private void OnInteractPerformed(InputAction.CallbackContext context)
{
Debug.Log("Interact action performed");
if(heldObject != null)
if (playerStressController.isPanicking) return;
if (heldObject != null)
{
Debug.Log("Dropping held object");
DropObject(false);
return;
}
@@ -94,6 +97,12 @@ public class PlayerPickupController : MonoBehaviour
return;
}
if(playerStressController.isPanicking)
{
DropObject(false);
return;
}
if (!heldObject.TryGetComponent<TrickController>(out _))
{
return;
@@ -118,13 +127,15 @@ public class PlayerPickupController : MonoBehaviour
{
DropObject(true);
}
}
void TryPickUp()
{
// Find all colliders within range
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, pickupRange);
Debug.Log(colliders.Length + " colliders found in range");
foreach (var col in colliders)
{
if (col.gameObject == gameObject)
@@ -17,6 +17,8 @@ public class PlayerPlatformerController : MonoBehaviour
[SerializeField] private LayerMask groundMask = ~0;
[SerializeField] private float groundCheckDistance = 0.05f;
private PlayerStressController playerStressController;
private Rigidbody2D body;
private BoxCollider2D bodyCollider;
private InputActionAsset runtimeActions;
@@ -29,6 +31,7 @@ public class PlayerPlatformerController : MonoBehaviour
{
body = GetComponent<Rigidbody2D>();
bodyCollider = GetComponent<BoxCollider2D>();
playerStressController = GetComponent<PlayerStressController>();
}
private void OnEnable()
@@ -86,6 +89,8 @@ public class PlayerPlatformerController : MonoBehaviour
return;
}
if (playerStressController.isPanicking) return;
var input = moveAction.ReadValue<Vector2>();
if (Mathf.Abs(input.x) > 0.01f)
{
@@ -98,6 +103,8 @@ public class PlayerPlatformerController : MonoBehaviour
private void OnJumpPerformed(InputAction.CallbackContext context)
{
if (playerStressController.isPanicking) return;
if (!IsGrounded())
{
return;
+53 -12
View File
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@@ -10,12 +11,17 @@ public class PlayerStressController : MonoBehaviour
[Header("Stress Settings")]
public float currentStress = 0f;
public float maxStress = 100f;
public bool isPanicking = false;
public bool isParalyzed = false;
// Define the event
public static event Action OnPlayerPanic;
public static event Action OnPlayerCalm;
[Header("Panic Settings")]
[SerializeField] private float panicDuration = 1.2f;
private float panicEndTime;
[SerializeField] private float paralyseDuration = 5f;
public bool IsPanicking => Time.time < panicEndTime;
void Start()
{
@@ -27,35 +33,59 @@ public class PlayerStressController : MonoBehaviour
{
while (true) // Runs for the lifetime of the object
{
ApplyStressLogic();
yield return ApplyStressLogic();
// Wait for exactly 1 second before running again
yield return new WaitForSeconds(1.0f);
}
}
void ApplyStressLogic()
IEnumerator ApplyStressLogic()
{
if (isParalyzed) yield break;
// Find all objects tagged as "Enemy"
List<GameObject> enemies = GameObject.FindGameObjectsWithTag("Interactable").Where(enemy => enemy.GetComponent<TrickController>() != null).ToList();
// Keep stress within 0 and Max
currentStress = Mathf.Clamp(currentStress, 0, maxStress);
currentStress += enemies.Count; // Increase stress based on number of enemies nearby
Debug.Log($"Current Stress: {currentStress}");
if (currentStress >= maxStress)
if (pickupController.heldObject != null && pickupController.heldObject.TryGetComponent<TrickController>(out _))
{
HandlePanic();
currentStress += 5; // Increase stress based on number of carried tricks
}
currentStress = Mathf.Clamp(currentStress, 0, maxStress);
if (!isPanicking && currentStress >= maxStress)
{
Debug.Log($"{currentStress}, {isPanicking}, {isParalyzed}");
yield return HandlePanic();
}
else if(isPanicking && currentStress < maxStress)
{
isPanicking = false;
HandleCalm();
Debug.Log("Player has calmed down.");
}
}
void HandlePanic()
IEnumerator HandlePanic()
{
panicEndTime = Time.time + panicDuration;
isPanicking = true;
Debug.Log("Player is freaking out man!");
// Add effects like screen shake or slowing down the player here
OnPlayerPanic?.Invoke(); // Trigger the panic event
yield return StartCoroutine(HandleParalyze());
}
void HandleCalm()
{
Debug.Log("Player is feeling better now!");
OnPlayerCalm?.Invoke(); // Trigger the calm event
}
public void ReduceStress(float amount)
@@ -64,4 +94,15 @@ public class PlayerStressController : MonoBehaviour
currentStress = Mathf.Clamp(currentStress, 0, maxStress);
Debug.Log($"Stress reduced by {amount}. Current Stress: {currentStress}");
}
IEnumerator HandleParalyze()
{
isParalyzed = true;
Debug.Log("Player is paralyzed.");
yield return new WaitForSecondsRealtime(paralyseDuration);
Debug.Log("Player has recovered from paralysis.");
isParalyzed = false;
}
}
+2 -2
View File
@@ -25,10 +25,10 @@ public class WinSceneController : MonoBehaviour
button = root.Q<Button>("Button");
button.clicked += () => HandleStartClicked("MainMenu");
button.clicked += () => HandleClick("MainMenu");
}
private void HandleStartClicked(string sceneName)
private void HandleClick(string sceneName)
{
if (!string.IsNullOrWhiteSpace(sceneName))
{