Files
Crash-Course/Assets/Scripts/Game/LeaderboardManager.cs

91 lines
3.0 KiB
C#
Raw Normal View History

2025-03-25 22:21:21 -04:00
using System.Collections.Generic;
2025-04-16 19:57:54 -04:00
using UnityEngine; using Game; using Music; using Player;
using TMPro;
2025-03-25 22:21:21 -04:00
using UnityEngine.UI;
2025-04-16 19:57:54 -04:00
namespace Game
{
2025-03-25 22:21:21 -04:00
public class LeaderboardManager : MonoBehaviour
{
public static LeaderboardManager Instance { get; private set; }
2025-03-25 22:37:28 -04:00
[SerializeField] private GameObject playersParent;
[SerializeField] private GameObject playerPrefab;
[SerializeField] private GameObject leaderboardIconPrefab;
2025-03-25 22:21:21 -04:00
private Dictionary<GameObject, GameObject> playerIcons = new Dictionary<GameObject, GameObject>();
private void Awake() // Ensures only one instance of LeaderboardManager exists
2025-03-25 22:21:21 -04:00
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
InitializeLeaderboard();
}
private void InitializeLeaderboard() // Creates the leaderboard icons for each player
2025-03-25 22:21:21 -04:00
{
RectTransform parentRectTransform = playersParent.GetComponent<RectTransform>();
parentRectTransform.anchoredPosition = new Vector2(-10f, 10f);
2025-03-25 22:21:21 -04:00
foreach (GameObject player in GameManager.players)
{
2025-03-25 22:37:28 -04:00
Transform parent = Instantiate(playerPrefab, playersParent.transform).transform;
GameObject leaderboardIcon = Instantiate(leaderboardIconPrefab, parent);
leaderboardIcon.GetComponentInChildren<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
2025-03-25 22:37:28 -04:00
playerIcons[player] = parent.gameObject;
2025-03-25 22:21:21 -04:00
}
}
public void UpdateLeaderboard()
2025-03-25 22:21:21 -04:00
{
2025-03-28 13:00:37 -04:00
List<KeyValuePair<GameObject, float>> sortedList = new List<KeyValuePair<GameObject, float>>(GameManager.playerHoldTimes);
2025-03-25 22:21:21 -04:00
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
for (int i = 0; i < sortedList.Count; i++)
2025-03-28 13:00:37 -04:00
{
var player = sortedList[i];
playerIcons[player.Key].transform.SetSiblingIndex(i);
// Update the number text
TextMeshProUGUI[] textComponents = playerIcons[player.Key].GetComponentsInChildren<TextMeshProUGUI>();
foreach (var textComponent in textComponents)
{
if (textComponent.name == "Position Text")
{
textComponent.text = "#" + (i + 1).ToString();
break;
}
}
2025-03-28 13:00:37 -04:00
}
}
public void UpdatePlayerHoldTimeText(GameObject player, float holdTime)
{
if (playerIcons.ContainsKey(player))
2025-03-25 22:21:21 -04:00
{
TextMeshProUGUI[] textComponents = playerIcons[player].GetComponentsInChildren<TextMeshProUGUI>();
foreach (var textComponent in textComponents)
{
if (textComponent.name == "Text (TMP)")
{
int minutes = Mathf.FloorToInt(holdTime / 60F);
int seconds = Mathf.FloorToInt(holdTime % 60F);
textComponent.text = string.Format("{0:0}:{1:00}", minutes, seconds);
break;
}
}
2025-03-25 22:21:21 -04:00
}
}
2025-04-16 19:57:54 -04:00
}}