add scene transition
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VectorGraphics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
@@ -8,13 +12,18 @@ public class GameManager : MonoBehaviour
|
||||
public Inventory Inventory { get; private set; }
|
||||
public Storybools Storybools { 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);
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -23,12 +32,97 @@ private void Awake()
|
||||
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
DialogueManager = GetComponent<DialogueManager>();
|
||||
Inventory = GetComponent<Inventory>();
|
||||
ReloadReferences();
|
||||
|
||||
SaveSystem.Load();
|
||||
SaveSystem.Save(); // save off any corruption fixes
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grab everything the GameManager needs to keep track of
|
||||
/// </summary>
|
||||
private void ReloadReferences()
|
||||
{
|
||||
blackScreen = GameObject.FindWithTag("BlackScreen").GetComponent<Image>();
|
||||
playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
|
||||
DialogueManager = GetComponent<DialogueManager>();
|
||||
Inventory = GetComponent<Inventory>();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
isTransitioningScenes = true;
|
||||
|
||||
// Fade to black
|
||||
float fadeDuration = 0.2f;
|
||||
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;
|
||||
|
||||
if (playerController != null && GameSceneManager.Instance != null)
|
||||
{
|
||||
SceneDoor door = GameSceneManager.Instance.GetDoorWithId(doorId);
|
||||
|
||||
if (door != null)
|
||||
{
|
||||
playerController.CharacterControllerMove(door.gameObject.transform.position - playerController.transform.position);
|
||||
playerController.DougBody.transform.rotation = door.gameObject.transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
// Fade back in
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
|
||||
blackScreen.color = blackScreenColor;
|
||||
fadeTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
fadeTime = 0;
|
||||
|
||||
isTransitioningScenes = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Are we currently in the middle of a scene transition?
|
||||
/// </summary>
|
||||
public bool InSceneTransition()
|
||||
{
|
||||
return isTransitioningScenes;
|
||||
}
|
||||
|
||||
#region Storybool Save/Load
|
||||
public void SaveStoryBools(ref StoryboolSaveData data)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// Manages scene-specific info, WILL be destroyed on load, and there is one of these
|
||||
// in each scene
|
||||
public class GameSceneManager : MonoBehaviour
|
||||
{
|
||||
public static GameSceneManager Instance { get; private set; }
|
||||
|
||||
// This is a list of doors that take us to different scenes. We'll use this
|
||||
// to tell the GameManager which door to put us at when we enter a new scene.
|
||||
public List<SceneDoor> SceneDoors;
|
||||
|
||||
public SceneDoor GetDoorWithId(int id)
|
||||
{
|
||||
return SceneDoors.Find(x => x.DoorId == id);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// If there is an instance, and it's not me, delete myself.
|
||||
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53b8d8c9181974149990f96ea419a6c1
|
||||
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SceneDoor : Interactable
|
||||
{
|
||||
// ID of THIS door
|
||||
public int DoorId = 0;
|
||||
|
||||
// What scene does this door take us to?
|
||||
public string SceneToLoad = "";
|
||||
|
||||
// Which door does this lead to in the scene this door takes us to?
|
||||
public int IdOfCorrespondingDoor = 0;
|
||||
|
||||
public override void Interact()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public override void MoveInsideRange()
|
||||
{
|
||||
if (!GameManager.Instance.InSceneTransition())
|
||||
GameManager.Instance.EnterSceneDoor(SceneToLoad, DoorId);
|
||||
}
|
||||
|
||||
public override void MoveOutsideRange()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e99e84716dcf704abb5593bc6c91a40
|
||||
@@ -41,7 +41,6 @@ IEnumerator EnterRoomCoroutine(RoomDoor door)
|
||||
if (door.requiresInteraction)
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
|
||||
blackScreenObject.gameObject.SetActive(true);
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
|
||||
@@ -63,8 +62,6 @@ IEnumerator EnterRoomCoroutine(RoomDoor door)
|
||||
}
|
||||
fadeTime = 0;
|
||||
|
||||
blackScreenObject.gameObject.SetActive(false);
|
||||
|
||||
// Play exit animation for linked door (if it exists)
|
||||
door.linkedDoor.CloseDoor();
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
|
||||
Reference in New Issue
Block a user