126 lines
3.6 KiB
C#
126 lines
3.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEditor.SearchService;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public int lapAmount;
|
|
public GameObject[] checkpoints;
|
|
public GameObject[] players;
|
|
[HideInInspector] public List<string> playersFinished;
|
|
[HideInInspector] public float[] playerTimes;
|
|
[HideInInspector] public string[] playerTimesStr;
|
|
int[] playerLaps;
|
|
public TextMeshProUGUI lapCounter;
|
|
public TextMeshProUGUI timeCounter;
|
|
void Start()
|
|
{
|
|
// reset laps
|
|
playerLaps = new int[players.Count()];
|
|
playerTimes = new float[players.Count()];
|
|
playerTimesStr = new string[players.Count()];
|
|
|
|
for (int i = 0; i < playerLaps.Count(); i++)
|
|
{
|
|
playerLaps[i] = 1;
|
|
}
|
|
|
|
for (int i = 0; i < players.Count(); i++)
|
|
{
|
|
playerTimes[i] = 0.00000001f;
|
|
playerTimesStr[i] = "DNF";
|
|
}
|
|
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
print("playersfinished: " + playersFinished.Count());
|
|
|
|
for (int i = 0; i < players.Count(); i++)
|
|
{
|
|
GameObject player = players[i];
|
|
|
|
try
|
|
{
|
|
if (playersFinished.Contains(player.name) || playersFinished.Contains(player.name + " (player)"))
|
|
continue;
|
|
}
|
|
catch
|
|
{
|
|
return;
|
|
}
|
|
|
|
playerTimes[i] += Time.deltaTime;
|
|
|
|
bool isAI = true;
|
|
if (player.GetComponent<PlayerController>().enabled)
|
|
isAI = false;
|
|
|
|
if (isAI)
|
|
{
|
|
bool isFinished = player.GetComponent<AgentController>().isFinished;
|
|
|
|
if (isFinished)
|
|
{
|
|
player.GetComponent<AgentController>().isFinished = false;
|
|
playerLaps[i] += 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int checkpointsCollected = player.GetComponent<PlayerController>().checkpointsCollected;
|
|
|
|
if (checkpointsCollected == checkpoints.Count())
|
|
{
|
|
player.GetComponent<PlayerController>().checkpointsCollected = 0;
|
|
playerLaps[i] += 1;
|
|
lapCounter.text = "Lap count: " + playerLaps[i];
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
if (playerLaps[i] > lapAmount)
|
|
{
|
|
if (isAI)
|
|
{
|
|
playersFinished.Add(player.name);
|
|
player.GetComponent<AgentController>().enabled = false;
|
|
}
|
|
else
|
|
playersFinished.Add(player.name + " (player)");
|
|
player.GetComponent<PlayerController>().enabled = false;
|
|
|
|
playerTimesStr[i] = playerTimes[i].ToString();
|
|
}
|
|
}
|
|
|
|
|
|
// race finished
|
|
if (playersFinished.Count() == players.Count())
|
|
{
|
|
SceneManager.LoadScene("WinScreen");
|
|
}
|
|
|
|
}
|
|
}
|