Files
dungeons/Assets/Scripts/Managers/SceneManager.cs

193 lines
5.3 KiB
C#

using HurricaneVR.Framework.Core.Player;
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
using Zenject;
public class SceneManager : NetworkBehaviour
{
#if UNITY_EDITOR
public UnityEditor.SceneAsset LobbyScene;
public UnityEditor.SceneAsset EntranceScene;
public UnityEditor.SceneAsset ForgeScene;
private void OnValidate()
{
lobbySceneName = LobbyScene?.name;
entranceSceneName = EntranceScene?.name;
forgeSceneName = ForgeScene?.name;
}
#endif
[SerializeField]
[ReadOnly]
private string lobbySceneName;
[SerializeField]
[ReadOnly]
private string entranceSceneName;
[SerializeField]
[ReadOnly]
private string forgeSceneName;
//enum Scene
//{
// Lobby = 0,
// Entrance = 1,
// Forge = 2
//}
[Inject]
[ReadOnly]
[SerializeField]
private GameManager gameManager;
[ReadOnly]
[SerializeField]
private float fadeDuration = 2f;
[ReadOnly]
[SerializeField]
private Scene loadedScene;
public override void OnNetworkSpawn()
{
NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
base.OnNetworkSpawn();
}
private void OnSceneEvent(SceneEvent sceneEvent)
{
var clientOrServer = sceneEvent.ClientId == NetworkManager.ServerClientId ? "server" : "client";
switch (sceneEvent.SceneEventType)
{
case SceneEventType.LoadComplete:
{
// We want to handle this for only the server-side
if (sceneEvent.ClientId == NetworkManager.ServerClientId)
{
// Keep track of the loaded scene, you need this to unload it
loadedScene = sceneEvent.Scene;
}
Debug.Log($"Loaded the {sceneEvent.SceneName} scene on " +
$"{clientOrServer}-({sceneEvent.ClientId}).");
break;
}
case SceneEventType.UnloadComplete:
{
Debug.Log($"Unloaded the {sceneEvent.SceneName} scene on " +
$"{clientOrServer}-({sceneEvent.ClientId}).");
break;
}
case SceneEventType.LoadEventCompleted:
case SceneEventType.UnloadEventCompleted:
{
var loadUnload = sceneEvent.SceneEventType == SceneEventType.LoadEventCompleted ? "Load" : "Unload";
Debug.Log($"{loadUnload} event completed for the following client " +
$"identifiers:({sceneEvent.ClientsThatCompleted})");
if (sceneEvent.ClientsThatTimedOut.Count > 0)
{
Debug.LogWarning($"{loadUnload} event timed out for the following client " +
$"identifiers:({sceneEvent.ClientsThatTimedOut})");
}
break;
}
}
}
private void CheckStatus(SceneEventProgressStatus status, bool isLoading = true)
{
var sceneEventAction = isLoading ? "load" : "unload";
if (status != SceneEventProgressStatus.Started)
{
Debug.LogWarning($"Failed to {sceneEventAction} with" +
$" a {nameof(SceneEventProgressStatus)}: {status}");
}
}
private void UnloadScene()
{
// Assure only the server calls this when the NetworkObject is
// spawned and the scene is loaded.
if (!IsServer || !IsSpawned || !loadedScene.IsValid() || !loadedScene.isLoaded)
{
return;
}
var status = NetworkManager.SceneManager.UnloadScene(loadedScene);
CheckStatus(status, false);
}
[Button]
public void SwitchToEntranceLevel()
{
StartCoroutine(SwitchToSceneMultiplayer(entranceSceneName));
}
private IEnumerator SwitchToSceneMultiplayer(string scene)
{
//gameManager.LocalPlayer.FadeScreen(1, fadeDuration);
NetworkManager.SceneManager.LoadScene(scene, LoadSceneMode.Single);
//var operation = UnityEngine.SceneManagement.SceneManager
// .LoadSceneAsync((int)scene);
//operation.allowSceneActivation = false;
//float timer = 0;
//while (timer <= fadeDuration && !operation.isDone)
//{
// timer += Time.deltaTime;
yield return null;
//}
//operation.allowSceneActivation = true;
//this.scene = scene;
//gameManager.LocalPlayer.FadeScreen(0, fadeDuration);
}
//private IEnumerator SwitchToSceneSolo(Scene scene)
//{
// gameManager.LocalPlayer.FadeScreen(1, fadeDuration);
// //NetworkManager.SceneManager.LoadScene()
// var operation = UnityEngine.SceneManagement.SceneManager
// .LoadSceneAsync((int)scene);
// operation.allowSceneActivation = false;
// float timer = 0;
// while (timer <= fadeDuration && !operation.isDone)
// {
// timer += Time.deltaTime;
// yield return null;
// }
// operation.allowSceneActivation = true;
// this.scene = scene;
// gameManager.LocalPlayer.FadeScreen(0, fadeDuration);
//}
}