You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Driftology/Assets/Scripts/RaceManager.cs

88 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class RaceManager : MonoBehaviour
{
[SerializeField] private GameObject leaderboardUI;
[SerializeField] private Transform leaderboardContent;
[SerializeField] private GameObject leaderboardEntryPrefab;
[SerializeField] private GameObject waitingText;
[SerializeField] private int totalCars = 4;
private List<(VehicleController car, string name)> finishOrder = new();
private HashSet<VehicleController> finishedCars = new();
private bool leaderboardShown = false;
public void RegisterFinish(VehicleController car)
{
if (finishedCars.Contains(car)) return;
string displayName = car.isAIControlled
? car.GetComponentInChildren<TextMeshProUGUI>().text
: GameConstants.PlayerName;
finishOrder.Add((car, displayName));
finishedCars.Add(car);
if (!leaderboardShown && !car.isAIControlled)
{
leaderboardShown = true;
ShowLeaderboard(); // Show first time (player triggered)
waitingText?.SetActive(true);
}
else if (leaderboardShown)
{
UpdateLeaderboard(); // Add AI progressively
}
if (finishedCars.Count >= totalCars)
{
waitingText?.SetActive(false);
}
}
public bool HasFinished(VehicleController car)
{
return finishedCars.Contains(car);
}
private void ShowLeaderboard()
{
leaderboardUI.SetActive(true);
UpdateLeaderboard();
}
private void UpdateLeaderboard()
{
foreach (Transform child in leaderboardContent)
{
Destroy(child.gameObject);
}
string playerName = GameConstants.PlayerName;
for (int i = 0; i < finishOrder.Count; i++)
{
GameObject entry = Instantiate(leaderboardEntryPrefab, leaderboardContent);
TextMeshProUGUI text = entry.GetComponentInChildren<TextMeshProUGUI>();
if (text != null)
{
string name = finishOrder[i].name;
if (name == playerName)
{
text.text = $"<b>{i + 1}. {name}</b>";
text.color = Color.yellow;
}
else
{
text.text = $"{i + 1}. {name}";
text.color = Color.white;
}
}
}
}
}