Files
monde-usage-unity/Assets/Scripts/GameManager.cs

183 lines
4.1 KiBLFS
C#

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
public class GameManager : MonoBehaviour
{
#region Singleton
public static GameManager singleton;
private void Awake()
{
if (singleton == null)
{
singleton = this;
}
else if (singleton != this)
{
Destroy(gameObject);
}
}
#endregion
public bool skipIntro = false;
public VideoAsset introClip;
public VideoAsset fullMovie;
public VideoAsset endingClip;
public bool IsPlayingIntro { get; private set; }
public FloatingLabel floatingLabelPrefab;
public bool CanPlay => CanvasManager.singleton.CanPlay && !(IsPlayingIntro || CameraManager.singleton.isPlayingVideo);
public SoundManager soundManager;
public bool AllUnlocked { get; set; }
public VideoDownloader VideoDownloader { get; private set; }
public GameObject endingItem;
public int endingThreshold = 7;
public SchoolManager mainScene;
void Start()
{
soundManager = GetComponent<SoundManager>();
VideoDownloader = GetComponent<VideoDownloader>();
#if !UNITY_EDITOR
skipIntro = false;
#endif
}
// Called by UI Button
public void OnPressPlay()
{
if (skipIntro)
{
StartCoroutine(StartGame());
}
else
{
Intro();
}
}
private IEnumerator StartGame()
{
CanvasManager.singleton.StartGame();
CameraManager.singleton.ShowDefaultTexture();
IsPlayingIntro = false;
if (!AllUnlocked) CameraManager.singleton.UpdateArrows();
if (!skipIntro)
{
CameraManager.singleton.MoveRight();
yield return new WaitForSeconds(1f);
CameraManager.singleton.UpdateArrows();
}
}
private void Intro()
{
Fader.Fade(() =>
{
CanvasManager.singleton.titleScreen.SetActive(false);
CameraManager.singleton.PlayVideo(introClip, () =>
{
StartCoroutine(StartGame());
});
}, null, false);
}
public void PlayFullMovie()
{
CanvasManager.singleton.Warning("Le Film Dure 21 minutes Continuer ?",
() =>
{
CameraManager.singleton.PlayVideo(fullMovie, () =>
{
CameraManager.singleton.MoveToMainScene();
});
});
}
public void RestartGameWithWarning()
{
CanvasManager.singleton.Warning("êtes vous sûr de quitter ?", RestartGame);
}
public void RestartGame()
{
VideoPlayManager.Reset();
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void UnlockAll()
{
if (AllUnlocked) return;
AllUnlocked = true;
CameraManager.singleton.UnlockAll();
}
public void OnEndVideo()
{
if (VideoPlayManager.TotalVideoPlays >= endingThreshold)
{
if (CameraManager.singleton.CurrentSubScene == null)
{
CameraManager.singleton.GoToEnding();
}
}
}
public void PlayEndVideo()
{
CanvasManager.singleton.Warning("Terminer la révolution ?",
() =>
{
CameraManager.singleton.ToggleEnding();
CameraManager.singleton.PlayVideo(endingClip, () =>
{
RestartGame();
});
});
}
public void PlayCredits()
{
CameraManager.singleton.PlayVideo(endingClip, () =>
{
RestartGame();
}, 2 * 60); // Credits start at 2 minutes of endingClip
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Application.Quit();
}
else if (Input.GetKeyDown(KeyCode.R))
{
RestartGame();
}
else if (Input.GetKeyDown(KeyCode.U))
{
UnlockAll();
}
else if (Input.GetKeyDown(KeyCode.E))
{
PlayEndVideo();
}
}
}