final (i hope)
This commit is contained in:
1 parent
3c6679ab04
commit
db18247040
6 files changed
+816
-59
No files matched your search
@@ -4,12 +4,14 @@ namespace Code.Gameplay
|
||||
{
|
||||
public class AICarController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float speed = 15f;
|
||||
[SerializeField] public float speed = 15f;
|
||||
[SerializeField] private float stoppingDistance = 2f;
|
||||
[SerializeField] private float turnSpeed = 3f;
|
||||
|
||||
private Vector3[] _waypoints;
|
||||
private int _currentWaypointIndex;
|
||||
|
||||
private bool _isStopped;
|
||||
|
||||
public void SetRoute(Vector3[] waypoints)
|
||||
{
|
||||
@@ -19,7 +21,14 @@ namespace Code.Gameplay
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_waypoints == null || _waypoints.Length == 0) return;
|
||||
if (_waypoints == null || _waypoints.Length == 0 || _isStopped) return;
|
||||
|
||||
if (_currentWaypointIndex >= _waypoints.Length - 1)
|
||||
{
|
||||
var raceManager = FindFirstObjectByType<RaceManager>();
|
||||
raceManager?.OnLoose();
|
||||
_isStopped = true;
|
||||
}
|
||||
|
||||
var target = _waypoints[_currentWaypointIndex];
|
||||
var direction = target - transform.position;
|
||||
|
||||
@@ -11,6 +11,11 @@ namespace Code.Gameplay
|
||||
var raceManager = FindFirstObjectByType<RaceManager>();
|
||||
raceManager?.OnFinish();
|
||||
}
|
||||
else if (other.CompareTag("AI"))
|
||||
{
|
||||
var raceManager = FindFirstObjectByType<RaceManager>();
|
||||
raceManager?.OnLoose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Code.DI;
|
||||
using Code.Input;
|
||||
using Code.Records;
|
||||
using Code.UI;
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
@@ -11,14 +12,15 @@ namespace Code.Gameplay
|
||||
{
|
||||
public class RaceManager : MonoBehaviour
|
||||
{
|
||||
[Header("UI")]
|
||||
[SerializeField] private TMP_Text countdownText;
|
||||
[SerializeField] private TMP_Text timerText;
|
||||
[SerializeField] private GameObject resultsWrapper;
|
||||
[SerializeField] private GameObject youLoseWrapper;
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private float countdownDuration = 3f;
|
||||
[SerializeField] private float startDelayAfterGo = 0.5f;
|
||||
|
||||
[SerializeField] private AICarController aiCar;
|
||||
|
||||
private float _startTime;
|
||||
private float _elapsedTime;
|
||||
@@ -28,6 +30,9 @@ namespace Code.Gameplay
|
||||
private TMP_InputField _nameField;
|
||||
private Button _restartButton;
|
||||
private Button _toMenuButton;
|
||||
|
||||
private Button _restartButtonLose;
|
||||
private Button _toMenuButtonLose;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -36,9 +41,12 @@ namespace Code.Gameplay
|
||||
|
||||
_nameField = resultsWrapper.transform.Find("EnterNameField")?.GetComponent<TMP_InputField>();
|
||||
_restartButton = resultsWrapper.transform.Find("RestartButton")?.GetComponent<Button>();
|
||||
_restartButtonLose = youLoseWrapper.transform.Find("RestartButton")?.GetComponent<Button>();
|
||||
_toMenuButton = resultsWrapper.transform.Find("ToMenuButton")?.GetComponent<Button>();
|
||||
_toMenuButtonLose = youLoseWrapper.transform.Find("ToMenuButton")?.GetComponent<Button>();
|
||||
|
||||
resultsWrapper.SetActive(false);
|
||||
youLoseWrapper.SetActive(false);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -88,6 +96,10 @@ namespace Code.Gameplay
|
||||
_isRaceActive = true;
|
||||
_startTime = Time.time;
|
||||
countdownText.gameObject.SetActive(false);
|
||||
|
||||
var trackData = FindFirstObjectByType<GameplayController>().currentTrack;
|
||||
aiCar.transform.position = trackData.Waypoints[0] + Random.insideUnitSphere * 2f;
|
||||
aiCar.GetComponent<AICarController>().SetRoute(trackData.Waypoints.ToArray());
|
||||
}
|
||||
|
||||
private void UpdateTimer()
|
||||
@@ -106,6 +118,8 @@ namespace Code.Gameplay
|
||||
countdownText.gameObject.SetActive(false);
|
||||
|
||||
resultsWrapper.SetActive(true);
|
||||
aiCar.speed = 0;
|
||||
|
||||
var rect = resultsWrapper.GetComponent<RectTransform>();
|
||||
rect.localScale = Vector3.zero;
|
||||
rect.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack);
|
||||
@@ -115,6 +129,24 @@ namespace Code.Gameplay
|
||||
_restartButton.onClick.AddListener(RestartRace);
|
||||
_toMenuButton.onClick.AddListener(LoadMainMenu);
|
||||
}
|
||||
|
||||
public void OnLoose()
|
||||
{
|
||||
_isRaceActive = false;
|
||||
countdownText.gameObject.SetActive(false);
|
||||
|
||||
youLoseWrapper.SetActive(true);
|
||||
aiCar.speed = 0;
|
||||
|
||||
var rect = youLoseWrapper.GetComponent<RectTransform>();
|
||||
rect.localScale = Vector3.zero;
|
||||
rect.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack);
|
||||
|
||||
_inputService.DisableMap(ActionMapping.Gameplay);
|
||||
|
||||
_restartButtonLose.onClick.AddListener(RestartRaceLose);
|
||||
_toMenuButtonLose.onClick.AddListener(LoadMainMenuLose);
|
||||
}
|
||||
|
||||
private void SaveResult()
|
||||
{
|
||||
@@ -130,10 +162,20 @@ namespace Code.Gameplay
|
||||
SaveResult();
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
|
||||
private void RestartRaceLose()
|
||||
{
|
||||
SaveResult();
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
|
||||
private void LoadMainMenu()
|
||||
{
|
||||
SaveResult();
|
||||
SceneManager.LoadScene("MainMenu");
|
||||
}
|
||||
|
||||
private void LoadMainMenuLose()
|
||||
{
|
||||
SceneManager.LoadScene("MainMenu");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Code.Gameplay;
|
||||
using Code.Track;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -9,28 +8,21 @@ namespace Code.UI
|
||||
[SerializeField] private TrackBuilder trackBuilder;
|
||||
[SerializeField] private Transform playerSpawn;
|
||||
[SerializeField] private Transform finishTrigger;
|
||||
[SerializeField] private AICarController[] aiCars;
|
||||
|
||||
private TrackData _currentTrack;
|
||||
public TrackData currentTrack { get; private set; }
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var seed = Random.Range(0, 10000);
|
||||
_currentTrack = TrackGenerator.Generate(seed, 500, segmentCount: 30);
|
||||
currentTrack = TrackGenerator.Generate(seed, 500, segmentCount: 30);
|
||||
|
||||
trackBuilder.Build(_currentTrack);
|
||||
trackBuilder.Build(currentTrack);
|
||||
|
||||
playerSpawn.position = _currentTrack.StartPosition;
|
||||
playerSpawn.rotation = _currentTrack.StartRotation;
|
||||
playerSpawn.position = currentTrack.StartPosition;
|
||||
playerSpawn.rotation = currentTrack.StartRotation;
|
||||
|
||||
finishTrigger.position = _currentTrack.FinishPosition;
|
||||
finishTrigger.rotation = _currentTrack.FinishRotation;
|
||||
|
||||
foreach (var aiCar in aiCars)
|
||||
{
|
||||
aiCar.transform.position = _currentTrack.Waypoints[0] + Random.insideUnitSphere * 2f;
|
||||
aiCar.GetComponent<AICarController>().SetRoute(_currentTrack.Waypoints.ToArray());
|
||||
}
|
||||
finishTrigger.position = currentTrack.FinishPosition;
|
||||
finishTrigger.rotation = currentTrack.FinishRotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user