DNF-timer + next car button when finished

This commit is contained in:
magn9775
2024-04-26 18:21:02 +02:00
parent 69c51ec879
commit ef5c8e25a6
12 changed files with 736 additions and 239 deletions

View File

@@ -17,58 +17,55 @@ public class GameController :MonoBehaviour
public static bool RaceIsStarted { get { return true; } }
public static bool RaceIsEnded { get { return false; } }
//PlayerController m_PlayerCar;
//List<PlayerController> Cars = new List<PlayerController>();
public TextMeshPro speedometer;
public List<GameObject> cars;
int CurrentCarIndex = 0;
protected virtual void Awake ()
private List<string> finished;
void Start()
{
Instance = this;
finished=gameObject.GetComponent<GameManager>().playersFinished;
// foreach (var car in cars)
// {
// var userControl = car.GetComponent<PlayerController>();
// var audioListener = car.GetComponent<AudioListener>();
// if (userControl == null)
// {
// userControl = car.gameObject.AddComponent<PlayerController> ();
// }
// if (audioListener == null)
// {
// audioListener = car.gameObject.AddComponent<AudioListener> ();
// }
// userControl.enabled = false;
// audioListener.enabled = false;
// }
// cars[CurrentCarIndex].GetComponent<PlayerController>().enabled = true;
// cars[CurrentCarIndex].GetComponent<AudioListener>().enabled = true;
if (speedometer == null)
{
speedometer = new TextMeshPro();
}
if (carRB == null)
{
carRB = GetComponent<Rigidbody>();
}
}
void Update ()
public void Update ()
{
if (Input.GetKeyDown (NextCarKey))
{
NextCar ();
}
carVel = (math.abs(carRB.velocity.x) + math.abs(carRB.velocity.y) + math.abs(carRB.velocity.z))*5;
//print(carVel);
speedometer.SetText(carVel.ToString());
}
private void NextCar ()
public void NextCar()
{
// cars[CurrentCarIndex].GetComponent<PlayerController> ().enabled = false;
// cars[CurrentCarIndex].GetComponent<AudioListener> ().enabled = false;
for (int i = 0; i < cars.Count; i++)
{
if (finished.Contains(cars[i].name))
{
cars.Remove(cars[i]);
}
}
CurrentCarIndex = LoopClamp (CurrentCarIndex + 1, 0, cars.Count);
// cars[CurrentCarIndex].GetComponent<PlayerController>().enabled = true;
// cars[CurrentCarIndex].GetComponent<AudioListener>().enabled = true;
PlayerCar = cars[CurrentCarIndex];
@@ -90,4 +87,6 @@ public class GameController :MonoBehaviour
}
return value;
}
}

View File

@@ -8,6 +8,7 @@ using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public int lapAmount;
@@ -20,6 +21,9 @@ public class GameManager : MonoBehaviour
public TextMeshProUGUI lapCounter;
public TextMeshProUGUI timeCounter;
public TextMeshProUGUI startTime;
public TextMeshProUGUI DNFTime;
public TextMeshProUGUI speedometer;
public GameObject nextCarbtn;
float DNFTimer = 0;
private bool hasCountedDown = false;
void Start()
@@ -51,6 +55,10 @@ public class GameManager : MonoBehaviour
StartGame();
}
for (int i = 0; i < players.Count(); i++)
{
GameObject player = players[i];
@@ -86,6 +94,10 @@ public class GameManager : MonoBehaviour
{
int checkpointsCollected = player.GetComponent<PlayerController>().checkpointsCollected;
carRB = player.GetComponent<RigidBody>();
carVel = (math.abs(carRB.velocity.x) + math.abs(carRB.velocity.y) + math.abs(carRB.velocity.z))*5;
speedometer.SetText(carVel.ToString());
if (checkpointsCollected == checkpoints.Count())
{
player.GetComponent<PlayerController>().checkpointsCollected = 0;
@@ -96,18 +108,7 @@ public class GameManager : MonoBehaviour
lapCounter.text = "Lap count: " + playerLaps[i] + "/" + lapAmount;
}
string strTimes =playerTimes[i].ToString();
int seconds = (int)MathF.Floor(playerTimes[i]);
int seperator = strTimes.IndexOf(",");
string miliseconds;
if (strTimes.Length < seperator + 4)
miliseconds = ",000";
else
miliseconds = strTimes.Substring(seperator, 4);
timeCounter.text = "Time: " + seconds + miliseconds;
timeCounter.text = "Time: " + (Mathf.Round(playerTimes[i]*1000)/1000).ToString();
}
if (playerLaps[i] > lapAmount)
@@ -120,6 +121,7 @@ public class GameManager : MonoBehaviour
else
playersFinished.Add(player.name + " (player)");
player.GetComponent<PlayerController>().enabled = false;
nextCarbtn.SetActive(true);
playerTimesStr[i] = playerTimes[i].ToString();
}
@@ -131,6 +133,7 @@ public class GameManager : MonoBehaviour
if (playersFinished.Count() > 0)
{
DNFTimer += Time.deltaTime;
DNFTime.text = "DNF timer: "+(Mathf.Round(60-DNFTimer)).ToString();
}
// race finished