Files
racesm/Assets/Scripts/GameManager.cs
2024-04-26 09:20:13 +02:00

72 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public int lapAmount;
public GameObject[] checkpoints;
public GameObject[] players;
[HideInInspector] public string[] playersFinished;
int[] playerLaps;
void Start()
{
// reset laps
playerLaps = new int[players.Count()];
for (int i = 0; i < playerLaps.Count(); i++)
{
playerLaps[i] = 1;
}
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < players.Count(); i++)
{
GameObject player = players[i];
if (playersFinished.Contains(player.name))
continue;
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;
}
}
if (playerLaps[i] > lapAmount)
{
player.GetComponent<Rigidbody>().isKinematic = true;
playersFinished.Append(player.name);
print("\nPlayers finished:");
foreach (string pp in playersFinished)
print(pp);
}
}
}
}