added race management
This commit is contained in:
1 parent
8e168efa3e
commit
586c476851
6 files changed
+1410
-9
No files matched your search
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Gameplay
|
||||
{
|
||||
public class FinishTrigger : MonoBehaviour
|
||||
{
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.CompareTag("Player"))
|
||||
{
|
||||
var raceManager = FindFirstObjectByType<RaceManager>();
|
||||
raceManager?.OnFinish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4cb191c0886a4eabb5d7a94ff5200e09
|
||||
timeCreated: 1769527096
|
||||
@@ -0,0 +1,125 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 951c89eb8c3a473591e74a48d9588855
|
||||
timeCreated: 1769525346
|
||||
@@ -154,6 +154,7 @@ namespace Code.UI
|
||||
private void OnDestroy()
|
||||
{
|
||||
_inputService?.DisableMap(ActionMapping.MainMenu);
|
||||
_inputService?.DisableMap(ActionMapping.Gameplay);
|
||||
startGameButton?.onClick.RemoveListener(OnStartGameClicked);
|
||||
showResultsButton?.onClick.RemoveListener(OnShowResultsClicked);
|
||||
exitButton?.onClick.RemoveListener(OnExitButtonClicked);
|
||||
|
||||
Reference in new issue
Block a user