125 lines
3.6 KiB
C#
125 lines
3.6 KiB
C#
using Code.DI;
|
|
using Code.Input;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Code.Gameplay
|
|
{
|
|
public class RaceManager : MonoBehaviour
|
|
{
|
|
[Header("UI")]
|
|
[SerializeField] private TMP_Text countdownText;
|
|
[SerializeField] private TMP_Text timerText;
|
|
[SerializeField] private GameObject resultsWrapper;
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private float countdownDuration = 3f;
|
|
[SerializeField] private float startDelayAfterGo = 0.5f;
|
|
|
|
private float _startTime;
|
|
private float _elapsedTime;
|
|
private bool _isRaceActive = false;
|
|
private IInputService _inputService;
|
|
|
|
private TMP_Text _resultText;
|
|
private TMP_InputField _nameField;
|
|
private Button _restartButton;
|
|
private Button _toMenuButton;
|
|
|
|
private void Awake()
|
|
{
|
|
var container = FindFirstObjectByType<DIContainer>();
|
|
_inputService = container.Resolve<IInputService>();
|
|
|
|
_resultText = resultsWrapper.transform.Find("Text Area")?.Find("Text")?.GetComponent<TMP_Text>();
|
|
_nameField = resultsWrapper.transform.Find("EnterNameField")?.GetComponent<TMP_InputField>();
|
|
_restartButton = resultsWrapper.transform.Find("RestartButton")?.GetComponent<Button>();
|
|
_toMenuButton = resultsWrapper.transform.Find("ToMenuButton")?.GetComponent<Button>();
|
|
|
|
resultsWrapper.SetActive(false);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
StartCountdown();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_isRaceActive)
|
|
{
|
|
_elapsedTime = Time.time - _startTime;
|
|
UpdateTimer();
|
|
}
|
|
}
|
|
|
|
private void StartCountdown()
|
|
{
|
|
_isRaceActive = false;
|
|
_startTime = Time.time + countdownDuration + startDelayAfterGo;
|
|
|
|
countdownText.text = "3";
|
|
Invoke(nameof(Show2), 1f);
|
|
}
|
|
|
|
private void Show2()
|
|
{
|
|
countdownText.text = "2";
|
|
Invoke(nameof(Show1), 1f);
|
|
}
|
|
|
|
private void Show1()
|
|
{
|
|
countdownText.text = "1";
|
|
Invoke(nameof(ShowGo), 1f);
|
|
}
|
|
|
|
private void ShowGo()
|
|
{
|
|
countdownText.text = "GO!";
|
|
Invoke(nameof(StartRace), startDelayAfterGo);
|
|
}
|
|
|
|
private void StartRace()
|
|
{
|
|
_inputService.EnableMap(ActionMapping.Gameplay);
|
|
_isRaceActive = true;
|
|
_startTime = Time.time;
|
|
countdownText.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void UpdateTimer()
|
|
{
|
|
var totalMs = (int)(_elapsedTime * 1000f);
|
|
var minutes = totalMs / 60000;
|
|
var seconds = (totalMs % 60000) / 1000;
|
|
var milliseconds = totalMs % 1000;
|
|
|
|
timerText.text = $"{minutes:D2}:{seconds:D2}:{milliseconds:D2}";
|
|
}
|
|
|
|
public void OnFinish()
|
|
{
|
|
_isRaceActive = false;
|
|
countdownText.gameObject.SetActive(false);
|
|
|
|
resultsWrapper.SetActive(true);
|
|
_inputService.DisableMap(ActionMapping.Gameplay);
|
|
|
|
_restartButton.onClick.AddListener(RestartRace);
|
|
_toMenuButton.onClick.AddListener(LoadMainMenu);
|
|
}
|
|
|
|
private void RestartRace()
|
|
{
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
|
|
private void LoadMainMenu()
|
|
{
|
|
SceneManager.LoadScene("MainMenu");
|
|
}
|
|
}
|
|
} |