257 lines
6.1 KiB
C#
257 lines
6.1 KiB
C#
using System;
|
|
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 List<Sprite> Transports
|
|
{
|
|
get
|
|
{
|
|
return new List<Sprite> {
|
|
Resources.Load<Sprite>("Images/metro_80x80"),
|
|
Resources.Load<Sprite>("Images/bus_80x80"),
|
|
Resources.Load<Sprite>("Images/deambulateur_80x80"),
|
|
Resources.Load<Sprite>("Images/voiture_80x80"),
|
|
Resources.Load<Sprite>("Images/lapetiterennes_80x80"),
|
|
Resources.Load<Sprite>("Images/toutenvelo_80x80"),
|
|
Resources.Load<Sprite>("Images/monocycle_80x80"),
|
|
Resources.Load<Sprite>("Images/velopiste_80x80")
|
|
};
|
|
}
|
|
}
|
|
public string GameSceneName = "GameScene";
|
|
public string MenuSceneName = "Menus";
|
|
|
|
public string XAxisP1 = "HorizontalP1";
|
|
public string YAxisP1 = "VerticalP1";
|
|
public string SubmitP1 = "SubmitP1";
|
|
public string CancelP1 = "CancelP1";
|
|
public string XAxisP2 = "HorizontalP2";
|
|
public string YAxisP2 = "VerticalP2";
|
|
public string SubmitP2 = "SubmitP2";
|
|
public string CancelP2 = "CancelP2";
|
|
|
|
//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; } }
|
|
|
|
public HighscoreEntry EntryP1 = new HighscoreEntry { time = new TimeSpan(0, 0, 0, 28, 20) };
|
|
public HighscoreEntry EntryP2 = new HighscoreEntry { time = new TimeSpan(0, 0, 0, 40, 20) };
|
|
|
|
// 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());
|
|
}
|
|
|
|
public void DisplayMenu()
|
|
{
|
|
_status = States.Menu;
|
|
StartCoroutine(LoadMenuScene());
|
|
}
|
|
|
|
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()
|
|
{
|
|
return LoadScene(GameSceneName);
|
|
}
|
|
|
|
IEnumerator LoadMenuScene()
|
|
{
|
|
return LoadScene(MenuSceneName);
|
|
}
|
|
|
|
IEnumerator LoadScene(string sceneName)
|
|
{
|
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
|
|
while (!asyncLoad.isDone)
|
|
yield return null;
|
|
}
|
|
|
|
|
|
}
|