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.
132 lines
3.1 KiB
C#
132 lines
3.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
using TMPro;
|
|
|
|
public class GameUIManager : MonoBehaviour
|
|
{
|
|
[Header("NOS UI")]
|
|
public Image boostBarFill;
|
|
public Button boostButton;
|
|
|
|
[Header("Pause UI")]
|
|
public Button pauseButton;
|
|
public GameObject pauseMenuPanel;
|
|
public Button resumeButton;
|
|
public Button restartButton;
|
|
public Button giveUpButton;
|
|
|
|
[Header("Speed UI")]
|
|
public TextMeshProUGUI speedText;
|
|
|
|
[Header("Position UI")]
|
|
public TextMeshProUGUI positionText;
|
|
|
|
private VehicleController playerCar;
|
|
private VehicleTracker playerTracker;
|
|
private RaceManager raceManager;
|
|
|
|
private bool isPaused = false;
|
|
|
|
public static GameUIManager Instance;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
raceManager = FindObjectOfType<RaceManager>();
|
|
|
|
if (pauseMenuPanel != null)
|
|
pauseMenuPanel.SetActive(false);
|
|
|
|
// Button listeners
|
|
if (pauseButton != null)
|
|
pauseButton.onClick.AddListener(TogglePause);
|
|
|
|
if (resumeButton != null)
|
|
resumeButton.onClick.AddListener(ResumeGame);
|
|
|
|
if (restartButton != null)
|
|
restartButton.onClick.AddListener(SceneRestart);
|
|
|
|
if (giveUpButton != null)
|
|
giveUpButton.onClick.AddListener(MainMenuSceneLoader);
|
|
|
|
// Wait for car spawn
|
|
Invoke(nameof(FindPlayerReferences), 0.2f);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateSpeedDisplay();
|
|
UpdatePositionDisplay();
|
|
}
|
|
|
|
private void FindPlayerReferences()
|
|
{
|
|
var controllers = FindObjectsOfType<VehicleController>();
|
|
foreach (var controller in controllers)
|
|
{
|
|
if (!controller.isAIControlled)
|
|
{
|
|
playerCar = controller;
|
|
playerTracker = controller.GetComponent<VehicleTracker>();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateSpeedDisplay()
|
|
{
|
|
if (playerCar == null || speedText == null)
|
|
return;
|
|
|
|
float speed = Mathf.Abs(playerCar.CurrentSpeed);
|
|
speedText.text = $"{Mathf.RoundToInt(speed)} km/h";
|
|
}
|
|
|
|
private void UpdatePositionDisplay()
|
|
{
|
|
if (playerTracker == null || raceManager == null || positionText == null)
|
|
return;
|
|
|
|
int pos = raceManager.GetCurrentPosition(playerTracker);
|
|
int total = raceManager.TotalCars;
|
|
|
|
positionText.text = $"{pos}/{total}";
|
|
}
|
|
|
|
public void TogglePause()
|
|
{
|
|
isPaused = !isPaused;
|
|
Time.timeScale = isPaused ? 0f : 1f;
|
|
|
|
if (pauseMenuPanel != null)
|
|
pauseMenuPanel.SetActive(isPaused);
|
|
}
|
|
|
|
public void ResumeGame()
|
|
{
|
|
isPaused = false;
|
|
Time.timeScale = 1f;
|
|
|
|
if (pauseMenuPanel != null)
|
|
pauseMenuPanel.SetActive(false);
|
|
}
|
|
|
|
public void SceneRestart()
|
|
{
|
|
Time.timeScale = 1f;
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
|
|
public void MainMenuSceneLoader()
|
|
{
|
|
Time.timeScale = 1f;
|
|
SceneManager.LoadScene("Menu");
|
|
}
|
|
}
|