270 lines
8.1 KiB
C#
270 lines
8.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.VectorGraphics;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public static GameManager Instance { get; private set; }
|
|
public DialogueManager DialogueManager { get; private set; }
|
|
public TimeManager TimeManager { get; private set; }
|
|
public PlayerManager PlayerManager { get; private set; }
|
|
public Inventory Inventory { get; private set; }
|
|
public Storybools Storybools { get; private set; }
|
|
public PauseMenu PauseMenu { get; private set; }
|
|
|
|
// Are we currently in a scene transition?
|
|
private bool isTransitioningScenes = false;
|
|
private Image blackScreen;
|
|
private PlayerController playerController;
|
|
|
|
private void Awake()
|
|
{
|
|
// If there is an instance, and it's not me, delete myself.
|
|
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(this.gameObject);
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
ReloadReferences();
|
|
|
|
SaveSystem.Load();
|
|
SaveSystem.Save(); // save off any corruption fixes
|
|
}
|
|
|
|
/// <summary>
|
|
/// Grab everything the GameManager needs to keep track of
|
|
/// </summary>
|
|
private void ReloadReferences()
|
|
{
|
|
Instance.blackScreen = GameObject.FindWithTag("BlackScreen").GetComponent<Image>();
|
|
Instance.playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
|
|
Instance.DialogueManager = GetComponent<DialogueManager>();
|
|
Instance.DialogueManager.ReloadReferences();
|
|
|
|
Instance.PauseMenu = GameObject.FindWithTag("PauseMenu").GetComponent<PauseMenu>();
|
|
Instance.PauseMenu.ReloadReferences();
|
|
Instance.PauseMenu.gameObject.SetActive(false);
|
|
|
|
Instance.Inventory = GetComponent<Inventory>();
|
|
Instance.TimeManager = GetComponent<TimeManager>();
|
|
Instance.PlayerManager = GetComponent<PlayerManager>();
|
|
}
|
|
|
|
#region Map Load Handling
|
|
/// <summary>
|
|
/// Triggers transition to a map point
|
|
/// </summary>
|
|
public void GoToMapPoint(MapPoint mapPoint)
|
|
{
|
|
StartCoroutine(GoToMapPointCoroutine(mapPoint));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes transition to map point (scene and location)
|
|
/// </summary>
|
|
private IEnumerator GoToMapPointCoroutine(MapPoint mapPoint)
|
|
{
|
|
Instance.isTransitioningScenes = true;
|
|
Instance.playerController.SetCharacterControl(false);
|
|
|
|
// Fade to black
|
|
float fadeDuration = 0.2f; // how long to fade to/from black
|
|
float moveDuration = 0.4f; // how long to auto walk into room from the door
|
|
float fadeTime = 0;
|
|
Color blackScreenColor = Color.black;
|
|
|
|
while (fadeTime < fadeDuration)
|
|
{
|
|
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
|
|
blackScreen.color = blackScreenColor;
|
|
fadeTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
fadeTime = 0;
|
|
|
|
|
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(mapPoint.Scene);
|
|
while (!asyncLoad.isDone)
|
|
{
|
|
// Optional: Update a loading bar with asyncLoad.progress
|
|
yield return null;
|
|
}
|
|
|
|
ReloadReferences();
|
|
|
|
// Make sure screen is black in new scene
|
|
blackScreen.color = blackScreenColor;
|
|
|
|
if (Instance.playerController != null && GameSceneManager.Instance != null)
|
|
{
|
|
Instance.playerController.CharacterControllerMove(mapPoint.SpawnPosition - playerController.transform.position);
|
|
}
|
|
|
|
// Fade back in
|
|
while (fadeTime < fadeDuration)
|
|
{
|
|
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
|
|
Instance.blackScreen.color = blackScreenColor;
|
|
fadeTime += Time.deltaTime;
|
|
|
|
yield return null;
|
|
}
|
|
|
|
// move character a little more
|
|
while (fadeTime < moveDuration)
|
|
{
|
|
fadeTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
fadeTime = 0;
|
|
|
|
Instance.isTransitioningScenes = false;
|
|
Instance.playerController.SetCharacterControl(true);
|
|
}
|
|
#endregion
|
|
|
|
#region Scene Door Handling
|
|
/// <summary>
|
|
/// Triggers transition to specified scene at the door with the specified ID
|
|
/// </summary>
|
|
public void EnterSceneDoor(string scene, int door)
|
|
{
|
|
StartCoroutine(EnterSceneDoorCoroutine(scene, door));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes transition to specified scene at the door with the specified ID
|
|
/// </summary>
|
|
private IEnumerator EnterSceneDoorCoroutine(string scene, int doorId)
|
|
{
|
|
Instance.isTransitioningScenes = true;
|
|
Instance.playerController.SetCharacterControl(false);
|
|
|
|
// Fade to black
|
|
float fadeDuration = 0.2f; // how long to fade to/from black
|
|
float moveDuration = 0.4f; // how long to auto walk into room from the door
|
|
float fadeTime = 0;
|
|
Color blackScreenColor = Color.black;
|
|
|
|
while (fadeTime < fadeDuration)
|
|
{
|
|
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
|
|
blackScreen.color = blackScreenColor;
|
|
fadeTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
fadeTime = 0;
|
|
|
|
|
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(scene);
|
|
while (!asyncLoad.isDone)
|
|
{
|
|
// Optional: Update a loading bar with asyncLoad.progress
|
|
yield return null;
|
|
}
|
|
|
|
ReloadReferences();
|
|
|
|
// Make sure screen is black in new scene
|
|
blackScreen.color = blackScreenColor;
|
|
|
|
SceneDoor door = GameSceneManager.Instance.GetDoorWithId(doorId);
|
|
|
|
if (Instance.playerController != null && GameSceneManager.Instance != null)
|
|
{
|
|
if (door != null)
|
|
{
|
|
Instance.playerController.CharacterControllerMove(door.gameObject.transform.position - playerController.transform.position);
|
|
Instance.playerController.DougBody.transform.rotation = door.gameObject.transform.rotation;
|
|
}
|
|
}
|
|
|
|
// Fade back in
|
|
while (fadeTime < fadeDuration)
|
|
{
|
|
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
|
|
Instance.blackScreen.color = blackScreenColor;
|
|
fadeTime += Time.deltaTime;
|
|
|
|
Instance.playerController.WalkInDirection((door.WalkDirection.position - playerController.transform.position).normalized);
|
|
yield return null;
|
|
}
|
|
|
|
// move character a little more
|
|
while (fadeTime < moveDuration)
|
|
{
|
|
Instance.playerController.WalkInDirection((door.WalkDirection.position - playerController.transform.position).normalized);
|
|
fadeTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
fadeTime = 0;
|
|
|
|
Instance.isTransitioningScenes = false;
|
|
Instance.playerController.SetCharacterControl(true);
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Are we currently in the middle of a scene transition?
|
|
/// </summary>
|
|
public bool InSceneTransition()
|
|
{
|
|
return Instance.isTransitioningScenes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the blackscreen object reference
|
|
/// </summary>
|
|
public Image GetBlackScreen()
|
|
{
|
|
return Instance.blackScreen;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the playerContoller object reference
|
|
/// </summary>
|
|
public PlayerController GetPlayerController()
|
|
{
|
|
return Instance.playerController;
|
|
}
|
|
|
|
#region Storybool Save/Load
|
|
public void SaveStoryBools(ref StoryboolSaveData data)
|
|
{
|
|
data.Storybools = Instance.Storybools;
|
|
}
|
|
|
|
public void LoadStoryBools(StoryboolSaveData data)
|
|
{
|
|
if (data.Storybools != null)
|
|
{
|
|
Instance.Storybools = data.Storybools;
|
|
}
|
|
else
|
|
{
|
|
Instance.Storybools = new Storybools();
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct StoryboolSaveData
|
|
{
|
|
public Storybools Storybools;
|
|
}
|