Files
racesm/Assets/Scripts/GameController.cs
2024-04-26 10:08:24 +02:00

94 lines
2.2 KiB
C#

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Base class game controller.
/// </summary>
public class GameController :MonoBehaviour
{
[SerializeField] KeyCode NextCarKey = KeyCode.N;
[SerializeField] UnityEngine.UI.Button NextCarButton;
public static GameController Instance;
public GameObject PlayerCar;
public GameObject cam;
public static bool RaceIsStarted { get { return true; } }
public static bool RaceIsEnded { get { return false; } }
//PlayerController m_PlayerCar;
//List<PlayerController> Cars = new List<PlayerController>();
public List<GameObject> cars;
int CurrentCarIndex = 0;
protected virtual void Awake ()
{
Instance = this;
// 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;
}
void Update ()
{
if (Input.GetKeyDown (NextCarKey))
{
NextCar ();
}
}
private void NextCar ()
{
// cars[CurrentCarIndex].GetComponent<PlayerController> ().enabled = false;
// cars[CurrentCarIndex].GetComponent<AudioListener> ().enabled = false;
CurrentCarIndex = LoopClamp (CurrentCarIndex + 1, 0, cars.Count);
// cars[CurrentCarIndex].GetComponent<PlayerController>().enabled = true;
// cars[CurrentCarIndex].GetComponent<AudioListener>().enabled = true;
PlayerCar = cars[CurrentCarIndex];
cam.GetComponent<CameraControl>().getCar(PlayerCar);
}
public static int LoopClamp (int value, int minValue, int maxValue)
{
while (value < minValue || value >= maxValue)
{
if (value < minValue)
{
value += maxValue - minValue;
}
else if (value >= maxValue)
{
value -= maxValue - minValue;
}
}
return value;
}
}