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;
|
|
|
|
|
[SerializeField] private GameObject lifePrefab;
|
2025-03-25 22:21:21 -04:00
|
|
|
|
|
|
|
|
private Dictionary<GameObject, GameObject> playerIcons = new Dictionary<GameObject, GameObject>();
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
if (Instance == null)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
InitializeLeaderboard();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeLeaderboard()
|
|
|
|
|
{
|
|
|
|
|
foreach (GameObject player in GameManager.players)
|
|
|
|
|
{
|
2025-03-25 22:37:28 -04:00
|
|
|
Transform parent = Instantiate(playerPrefab, playersParent.transform).transform;
|
|
|
|
|
GameObject life = Instantiate(lifePrefab, parent);
|
|
|
|
|
life.GetComponentInChildren<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
|
|
|
|
|
playerIcons[player] = parent.gameObject;
|
2025-03-25 22:21:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateLeaderboard()
|
|
|
|
|
{
|
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);
|
|
|
|
|
}
|
|
|
|
|
// Less fancy sorting system
|
|
|
|
|
|
2025-03-25 22:21:21 -04:00
|
|
|
foreach (var player in sortedList)
|
|
|
|
|
{
|
|
|
|
|
playerIcons[player.Key].transform.SetSiblingIndex(sortedList.IndexOf(player));
|
|
|
|
|
}
|
2025-03-28 13:00:37 -04:00
|
|
|
|
|
|
|
|
//foreach (var key in GameManager.playerHoldTimes)
|
|
|
|
|
//{
|
|
|
|
|
// print(key.Key.name + " : " + key.Value);
|
|
|
|
|
// }
|
2025-03-25 22:21:21 -04:00
|
|
|
}
|
|
|
|
|
}
|