added comments and organized project files
This commit is contained in:
56
Assets/Scripts/Game/LeaderboardManager.cs
Normal file
56
Assets/Scripts/Game/LeaderboardManager.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LeaderboardManager : MonoBehaviour
|
||||
{
|
||||
public static LeaderboardManager Instance { get; private set; }
|
||||
|
||||
[SerializeField] private GameObject playersParent;
|
||||
[SerializeField] private GameObject playerPrefab;
|
||||
[SerializeField] private GameObject leaderboardIconPrefab;
|
||||
|
||||
private Dictionary<GameObject, GameObject> playerIcons = new Dictionary<GameObject, GameObject>();
|
||||
|
||||
private void Awake() // Ensures only one instance of LeaderboardManager exists
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InitializeLeaderboard();
|
||||
}
|
||||
|
||||
private void InitializeLeaderboard() // Creates the leaderboard icons for each player
|
||||
{
|
||||
foreach (GameObject player in GameManager.players)
|
||||
{
|
||||
Transform parent = Instantiate(playerPrefab, playersParent.transform).transform;
|
||||
GameObject leaderboardIcon = Instantiate(leaderboardIconPrefab, parent);
|
||||
leaderboardIcon.GetComponentInChildren<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
|
||||
playerIcons[player] = parent.gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateLeaderboard() // Sorts the leaderboard based on player hold times
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user