175 lines
4.0 KiB
C#
175 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public enum States {
|
|
Menu = 0,
|
|
LoadingScene = 5,
|
|
LoadingVideo = 10,
|
|
RaceSetup = 15,
|
|
Race = 20
|
|
}
|
|
|
|
public class GameManager : MonoBehaviour {
|
|
private static GameManager _instance;
|
|
|
|
//public static GameManager Instance {get {return _instance; } }
|
|
public static GameManager Instance {get {return _instance ?? (_instance =
|
|
new GameObject("GameManager").AddComponent<GameManager>()); } }
|
|
|
|
private HUDManager hm;
|
|
private BikeManager bm1;
|
|
private BikeManager bm2;
|
|
|
|
private States _status;
|
|
public States Status {get {return _status;} }
|
|
|
|
// Skins of the two players
|
|
private int[] _skins = new int[2];
|
|
|
|
private ConfigurationLoader conf;
|
|
|
|
private void Awake(){
|
|
// Singleton
|
|
if (_instance != null && _instance != this) {
|
|
Debug.Log("WARNING: Other instance of GameManager found.");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
_instance = this;
|
|
|
|
// Don't destroy when loading new scene
|
|
DontDestroyOnLoad(transform.gameObject);
|
|
|
|
conf = new ConfigurationLoader();
|
|
|
|
_status = States.Menu;
|
|
|
|
// TODO: Load Menu
|
|
}
|
|
|
|
private void Start() {
|
|
// Quick Check
|
|
if (hm == null)
|
|
Debug.Log("ERROR: HUDManager not registered.");
|
|
if (bm1 == null)
|
|
Debug.Log("ERROR: BikeManager P1 not registered.");
|
|
if (bm2 == null)
|
|
Debug.Log("ERROR: BikeManager P2 not registered.");
|
|
}
|
|
|
|
public void RegisterBike(BikeManager bike, int bikeID) {
|
|
if (bikeID == 0)
|
|
bm1 = bike;
|
|
if (bikeID == 1)
|
|
bm2 = bike;
|
|
}
|
|
|
|
public void RegisterHUD(HUDManager hud) {
|
|
hm = hud;
|
|
}
|
|
|
|
public BikeManager GetBikeManager(int bikeID) {
|
|
if (bikeID == 0)
|
|
return bm1;
|
|
if (bikeID == 1)
|
|
return bm2;
|
|
return null;
|
|
}
|
|
|
|
public void Hello() {
|
|
Debug.Log("Hello!");
|
|
}
|
|
|
|
public float GetRaceDistance() {
|
|
return conf.distance;
|
|
}
|
|
|
|
public float GetRaceNormalTime() {
|
|
return conf.normalTime;
|
|
}
|
|
|
|
public float GetBikeWheelDiameter(int bikeID) {
|
|
return conf.diameters[bikeID];
|
|
}
|
|
|
|
public int GetSensorsCount(int bikeID) {
|
|
return conf.sensors[bikeID];
|
|
}
|
|
|
|
public float GetVideoEnd(int videoID) {
|
|
return conf.videoEnds[videoID];
|
|
}
|
|
|
|
public void SetSkins(int player1, int player2) {
|
|
_skins[0] = player1;
|
|
_skins[1] = player2;
|
|
}
|
|
|
|
public int GetSkins(int playerID) {
|
|
return _skins[playerID];
|
|
}
|
|
|
|
// TODO: Remove once menus are ok
|
|
void Update() {
|
|
if (Input.GetButtonDown("SubmitP1")) {
|
|
SetSkins(0, 1);
|
|
LaunchRace();
|
|
}
|
|
|
|
if (_status == States.LoadingVideo) {
|
|
Debug.Log(bm1.LoadingComplete() + " " + bm2.LoadingComplete());
|
|
if (bm1.LoadingComplete() && bm2.LoadingComplete()) {
|
|
Debug.Log("Videos are set up and ready to go!");
|
|
_status = States.RaceSetup;
|
|
StartCoroutine(SetupRace());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void LaunchRace() {
|
|
_status = States.LoadingScene;
|
|
StartCoroutine(LoadGameScene());
|
|
}
|
|
|
|
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode) {
|
|
if (_status == States.LoadingScene) {
|
|
Debug.Log("Loading videos");
|
|
_status = States.LoadingVideo;
|
|
}
|
|
}
|
|
|
|
void OnEnable() {
|
|
SceneManager.sceneLoaded += OnLevelFinishedLoading;
|
|
}
|
|
|
|
void OnDisable() {
|
|
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
|
|
}
|
|
|
|
IEnumerator SetupRace() {
|
|
hm.loadingScreens.SetActive(false);
|
|
hm.SetMessageActive(false);
|
|
// Timer
|
|
//yield return new WaitForSeconds(3f);
|
|
hm.SetMessageActive(true);
|
|
hm.SetMessage("Prêts ?");
|
|
// Timer
|
|
yield return new WaitForSeconds(2f);
|
|
hm.SetMessage("Pédalez !");
|
|
_status = States.Race;
|
|
// Timer
|
|
yield return new WaitForSeconds(1f);
|
|
hm.SetMessageActive(false);
|
|
}
|
|
|
|
|
|
IEnumerator LoadGameScene() {
|
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("GameScene");
|
|
while (!asyncLoad.isDone)
|
|
yield return null;
|
|
}
|
|
}
|