using System.Collections.Generic;
using UnityEngine;
using Game;
using Music;
using Player;
using TMPro;
using UnityEngine.UI;
namespace Game
{
///
/// This class manages the leaderboard, including initializing player icons,
/// updating player positions, and displaying hold times.
///
public class LeaderboardManager : MonoBehaviour
{
///
/// A single instance of this class that can be accessed from anywhere.
///
public static LeaderboardManager Instance { get; private set; }
///
/// The parent object that contains all player icons on the leaderboard.
///
[SerializeField] private GameObject playersParent;
///
/// The prefab used to represent a player on the leaderboard.
///
[SerializeField] private GameObject playerPrefab;
///
/// The prefab used for the leaderboard icon of each player.
///
[SerializeField] private GameObject leaderboardIconPrefab;
///
/// A dictionary mapping each player to their corresponding leaderboard icon.
///
private Dictionary playerIcons = new Dictionary();
///
/// Ensures only one instance of the LeaderboardManager exists.
///
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
///
/// Initializes the leaderboard when the game starts.
///
private void Start()
{
InitializeLeaderboard();
}
///
/// Creates the leaderboard icons for each player and positions them.
///
private void InitializeLeaderboard()
{
// Adjust the position of the parent container for player icons
RectTransform parentRectTransform = playersParent.GetComponent();
parentRectTransform.anchoredPosition = new Vector2(-10f, 10f);
// Create a leaderboard icon for each player
foreach (GameObject player in GameManager.players)
{
Transform parent = Instantiate(playerPrefab, playersParent.transform).transform;
GameObject leaderboardIcon = Instantiate(leaderboardIconPrefab, parent);
// Set the color of the leaderboard icon based on the player's color
leaderboardIcon.GetComponentInChildren().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
playerIcons[player] = parent.gameObject;
}
}
///
/// Updates the leaderboard by sorting players based on their hold times
/// and adjusting their positions.
///
public void UpdateLeaderboard()
{
// Sort players by their hold times in descending order
List> sortedList = new List>(GameManager.playerHoldTimes);
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
// Update the position and rank of each player on the leaderboard
for (int i = 0; i < sortedList.Count; i++)
{
var player = sortedList[i];
playerIcons[player.Key].transform.SetSiblingIndex(i);
// Update the rank text for the player
TextMeshProUGUI[] textComponents = playerIcons[player.Key].GetComponentsInChildren();
foreach (var textComponent in textComponents)
{
if (textComponent.name == "Position Text")
{
textComponent.text = "#" + (i + 1).ToString();
break;
}
}
}
}
///
/// Updates the hold time text for a specific player on the leaderboard.
///
/// The player whose hold time is being updated.
/// The new hold time to display.
public void UpdatePlayerHoldTimeText(GameObject player, float holdTime)
{
if (playerIcons.ContainsKey(player))
{
// Find and update the hold time text for the player
TextMeshProUGUI[] textComponents = playerIcons[player].GetComponentsInChildren();
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;
}
}
}
}
}
}