From 2913d4b927a1e2f16b4a9d1c9463cfb850a56830 Mon Sep 17 00:00:00 2001 From: djkellerman Date: Fri, 28 Mar 2025 21:27:09 -0400 Subject: [PATCH] Leaderboard now sorts icons properly --- Assets/Scripts/Game/GameManager.cs | 36 ++++++++++++++++++++++- Assets/Scripts/Game/LeaderboardManager.cs | 4 +-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index f8ce573..5be2fe4 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -46,6 +46,25 @@ public class GameManager : MonoBehaviour StartGame(); } + private void Update() // Continuously updates player hold times + { + foreach (var player in players) + { + float holdTime = GetPlayerHoldTime(player); + UpdatePlayerHoldTime(player, holdTime); + } + } + + private float GetPlayerHoldTime(GameObject player) + { + UseItem useItem = player.GetComponent(); + if (useItem != null) + { + return useItem.holdTime; + } + return 0f; + } + public void StartGame() // Sets up the proper gamemode { GameManager.playerHoldTimes.Clear(); @@ -167,14 +186,29 @@ public class GameManager : MonoBehaviour public void UpdatePlayerHoldTime(GameObject player, float holdTime) { + bool shouldSort = false; + if (playerHoldTimes.ContainsKey(player)) { + if (holdTime > playerHoldTimes[player]) + { + shouldSort = true; + } playerHoldTimes[player] = holdTime; } else { playerHoldTimes.Add(player, holdTime); + shouldSort = true; + } + + // Update the player's hold time text + LeaderboardManager.Instance.UpdatePlayerHoldTimeText(player, holdTime); + + // Sort the leaderboard if necessary + if (shouldSort) + { + LeaderboardManager.Instance.UpdateLeaderboard(); } - LeaderboardManager.Instance.UpdateLeaderboard(); } } diff --git a/Assets/Scripts/Game/LeaderboardManager.cs b/Assets/Scripts/Game/LeaderboardManager.cs index d831b8c..7274db8 100644 --- a/Assets/Scripts/Game/LeaderboardManager.cs +++ b/Assets/Scripts/Game/LeaderboardManager.cs @@ -51,11 +51,10 @@ public class LeaderboardManager : MonoBehaviour foreach (var player in sortedList) { playerIcons[player.Key].transform.SetSiblingIndex(sortedList.IndexOf(player)); - UpdatePlayerHoldTimeText(player.Key, player.Value); } } - private void UpdatePlayerHoldTimeText(GameObject player, float holdTime) // Updates the hold times of each player shown on the leaderboard + public void UpdatePlayerHoldTimeText(GameObject player, float holdTime) // Updates the hold times of each player shown on the leaderboard { if (playerIcons.ContainsKey(player)) { @@ -69,4 +68,3 @@ public class LeaderboardManager : MonoBehaviour } } } -