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

139 lines
5.2 KiB
C#
Raw Normal View History

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