342 lines
8.4 KiB
C#
342 lines
8.4 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/monocycle_80x80"),
|
|
Resources.Load<Sprite>("Images/voiture_80x80"),
|
|
Resources.Load<Sprite>("Images/lapetiterennes_80x80"),
|
|
Resources.Load<Sprite>("Images/toutenvelo_80x80"),
|
|
Resources.Load<Sprite>("Images/deambulateur_80x80"),
|
|
Resources.Load<Sprite>("Images/velopiste_80x80")
|
|
};
|
|
}
|
|
}
|
|
|
|
public List<Sprite> Transports320
|
|
{
|
|
get
|
|
{
|
|
return new List<Sprite> {
|
|
Resources.Load<Sprite>("Images/metro_320x320"),
|
|
Resources.Load<Sprite>("Images/bus_320x320"),
|
|
Resources.Load<Sprite>("Images/monocycle_320x320"),
|
|
Resources.Load<Sprite>("Images/voiture_320x320"),
|
|
Resources.Load<Sprite>("Images/lapetiterennes_320x320"),
|
|
Resources.Load<Sprite>("Images/toutenvelo_320x320"),
|
|
Resources.Load<Sprite>("Images/deambulateur_320x320"),
|
|
Resources.Load<Sprite>("Images/velopiste_320x320")
|
|
};
|
|
}
|
|
}
|
|
public string GameSceneName = "GameScene";
|
|
public string MenuSceneName = "Menus";
|
|
|
|
|
|
public string XAxis = "Horizontal";
|
|
public string YAxis = "Vertical";
|
|
public string Submit = "Submit";
|
|
public string Cancel = "Cancel";
|
|
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 string XAxis = KeyCode.UpArrow;
|
|
// public string YAxis = KeyCode.DownArrow;
|
|
// public string Submit = KeyCode.Return;
|
|
// public string Cancel = KeyCode.RightControl;
|
|
// public string XAxisP1 = KeyCode.UpArrow;
|
|
// public string YAxisP1 = KeyCode.DownArrow;
|
|
// public string SubmitP1 = KeyCode.Return;
|
|
// public string CancelP1 = KeyCode.RightControl;
|
|
// public string XAxisP2 = KeyCode.Z;
|
|
// public string YAxisP2 = KeyCode.S;
|
|
// public string SubmitP2 = KeyCode.Space;
|
|
// public string CancelP2 = KeyCode.LeftControl;
|
|
|
|
//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 float startTime;
|
|
|
|
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;
|
|
|
|
SceneManager.LoadScene ("Menus");
|
|
// TODO: Load Menu
|
|
foreach(var j in Input.GetJoystickNames()) Debug.Log(j);
|
|
}
|
|
|
|
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.");
|
|
var temp = HighscoresManager.Current;
|
|
}
|
|
|
|
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];
|
|
}
|
|
|
|
public int GetFirst()
|
|
{
|
|
if (bm1.GetDistance () > bm2.GetDistance ())
|
|
return 0;
|
|
if (bm1.GetDistance () < bm2.GetDistance ())
|
|
return 1;
|
|
return -1;
|
|
}
|
|
|
|
// TODO: Remove once menus are ok
|
|
void Update()
|
|
{
|
|
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 ());
|
|
}
|
|
} else if (_status == States.Race) {
|
|
if (bm1.GetProgress () >= 1 && ! bm1.finished) {
|
|
bm1.finished = true;
|
|
bm1.finishTime = GetChrono ();
|
|
StartCoroutine(PlayerFinished(0));
|
|
}
|
|
if (bm2.GetProgress () >= 1 && ! bm2.finished) {
|
|
bm2.finished = true;
|
|
bm2.finishTime = GetChrono ();
|
|
StartCoroutine(PlayerFinished(1));
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public float GetChrono() {
|
|
return Time.time - startTime;
|
|
}
|
|
|
|
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("PRETS ?");
|
|
// Timer
|
|
yield return new WaitForSeconds(3f);
|
|
hm.SetMessage("PEDALEZ !");
|
|
startTime = Time.time;
|
|
hm.SetActiveChrono (true);
|
|
_status = States.Race;
|
|
// Timer
|
|
yield return new WaitForSeconds(1f);
|
|
hm.SetMessageActive(false);
|
|
}
|
|
|
|
IEnumerator PlayerFinished(int playerID)
|
|
{
|
|
int winner = GetFirst ();
|
|
hm.SetMessageActive(true, playerID);
|
|
if (winner == playerID) {
|
|
hm.SetMessage ("Victoire !", playerID);
|
|
} else {
|
|
hm.SetMessage ("Fini !", playerID);
|
|
}
|
|
// Timer
|
|
yield return new WaitForSeconds(3f);
|
|
|
|
hm.SetMessageActive(false, playerID);
|
|
|
|
yield return new WaitForSeconds(2f);
|
|
|
|
if (playerID == 0) {
|
|
EntryP1.time = TimeSpan.FromSeconds (bm1.finishTime);
|
|
EntryP1.transport = GetSkins (0);
|
|
} else {
|
|
EntryP2.time = TimeSpan.FromSeconds(bm2.finishTime);
|
|
EntryP2.transport = GetSkins (1);
|
|
}
|
|
|
|
hm.SetHighScore (playerID);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
}
|