Leaderboard holdtime updates properly now

This commit is contained in:
djkellerman
2025-03-28 20:34:09 -04:00
parent a098e8c053
commit 0bf6ab0da7
4 changed files with 57 additions and 9 deletions

View File

@@ -165,7 +165,7 @@ public class GameManager : MonoBehaviour
return alivePlayers;
}
public void UpdatePlayerHoldTime(GameObject player, float holdTime) // Finds each players hold time and updates the leaderboard
public void UpdatePlayerHoldTime(GameObject player, float holdTime)
{
if (playerHoldTimes.ContainsKey(player))
{

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class LeaderboardManager : MonoBehaviour
@@ -31,6 +32,9 @@ public class LeaderboardManager : MonoBehaviour
private void InitializeLeaderboard() // Creates the leaderboard icons for each player
{
RectTransform parentRectTransform = playersParent.GetComponent<RectTransform>();
parentRectTransform.anchoredPosition = new Vector2(-20f, 10f);
foreach (GameObject player in GameManager.players)
{
Transform parent = Instantiate(playerPrefab, playersParent.transform).transform;
@@ -45,12 +49,24 @@ public class LeaderboardManager : MonoBehaviour
List<KeyValuePair<GameObject, float>> sortedList = new List<KeyValuePair<GameObject, float>>(GameManager.playerHoldTimes);
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
foreach (var player in sortedList)
{
Debug.Log(player.Key.name + " : " + player.Value);
}
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
{
if (playerIcons.ContainsKey(player))
{
TextMeshProUGUI holdTimeText = playerIcons[player].GetComponentInChildren<TextMeshProUGUI>();
if (holdTimeText != null)
{
int minutes = Mathf.FloorToInt(holdTime / 60F);
int seconds = Mathf.FloorToInt(holdTime % 60F);
holdTimeText.text = string.Format("{0:0}:{1:00}", minutes, seconds);
}
}
}
}