using UnityEngine; using TMPro; // Import TextMeshPro namespace using System.Collections.Generic; public class RaceManager : MonoBehaviour { public static RaceManager Instance; [Header("Race Settings")] [Tooltip("Total number of laps needed to finish the race.")] public int totalLaps = 3; [Tooltip("Total number of checkpoints in the race.")] public int numberOfCheckpoints = 0; [Header("Finish Line Settings")] [Tooltip("Assign the finish line object (with a trigger collider) here.")] public Transform finishLine; [Header("UI Elements")] [Tooltip("Assign the Text UI component to display player's position.")] public TextMeshProUGUI playerPositionText; [Tooltip("Assign the Text UI component to display laps left.")] public TextMeshProUGUI lapsLeftText; [Tooltip("Reference to the player's vehicle.")] public GameObject playerVehicle; private List racers = new List(); public bool raceFinished = false; public string winnerName = ""; private RacerProgress playerProgress; void Awake() { if (Instance == null) Instance = this; else Destroy(gameObject); } void Start() { GameObject[] racerObjects = GameObject.FindGameObjectsWithTag("Racer"); foreach (GameObject racer in racerObjects) { RacerProgress progress = racer.GetComponent(); if (progress != null) { racers.Add(progress); } } if (playerVehicle != null) { playerProgress = playerVehicle.GetComponent(); } // Initialize UI if (playerProgress != null) UpdateLapsLeftUI(); } void Update() { if (playerProgress != null) { int playerPosition = GetPlayerPosition(); UpdatePositionUI(playerPosition); UpdateLapsLeftUI(); // Continuously update the lap counter } } /// /// Returns the player's current position among all racers. /// private int GetPlayerPosition() { racers.Sort((r1, r2) => GetRacerProgressValue(r2).CompareTo(GetRacerProgressValue(r1))); for (int i = 0; i < racers.Count; i++) { if (racers[i] == playerProgress) return i + 1; // Position is 1-based } return racers.Count; } /// /// Updates the UI element to show the player's current race position. /// private void UpdatePositionUI(int position) { playerPositionText.text = position + "/" + racers.Count; } /// /// Updates the laps left UI element based on the player's current progress. /// private void UpdateLapsLeftUI() { int lapsLeft = Mathf.Max(totalLaps - playerProgress.lapCount, 0); lapsLeftText.text = "Laps Left: " + lapsLeft; } /// /// Calculates the progress value for a racer based on laps and checkpoints. /// private float GetRacerProgressValue(RacerProgress rp) { return rp.lapCount * numberOfCheckpoints + rp.currentCheckpointIndex; } public void CheckFinish(RacerProgress rp) { if (rp.lapCount >= totalLaps && !raceFinished) { raceFinished = true; winnerName = rp.gameObject.name; Debug.Log("Race Finished! Winner: " + winnerName); } } }