2025-03-25 22:21:21 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
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;
|
2025-03-28 17:39:07 -04:00
|
|
|
[SerializeField] private GameObject leaderboardIconPrefab;
|
2025-03-25 22:21:21 -04:00
|
|
|
|
|
|
|
|
private Dictionary<GameObject, GameObject> playerIcons = new Dictionary<GameObject, GameObject>();
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private void InitializeLeaderboard() // Creates the leaderboard icons for each player
|
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;
|
2025-03-28 17:39:07 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
public void UpdateLeaderboard() // Sorts the leaderboard based on player hold times
|
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));
|
2025-03-28 13:00:37 -04:00
|
|
|
foreach (var player in sortedList)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(player.Key.name + " : " + player.Value);
|
|
|
|
|
}
|
2025-03-25 22:21:21 -04:00
|
|
|
foreach (var player in sortedList)
|
|
|
|
|
{
|
|
|
|
|
playerIcons[player.Key].transform.SetSiblingIndex(sortedList.IndexOf(player));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|