Last active
October 5, 2021 09:16
-
-
Save MichelBartz/20df99af21bc34009b8f8430fa64c00e to your computer and use it in GitHub Desktop.
LoadingController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Assets.Scripts.Game.Main; | |
using Assets.Scripts.Game.Main.Event; | |
using Assets.Scripts.Systems.Audio; | |
using Assets.Scripts.Systems.State; | |
using Sirenix.OdinInspector; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.SceneManagement; | |
namespace Assets.Scripts.Game.LoadingScreen | |
{ | |
public class LoadingController: MonoBehaviour | |
{ | |
[BoxGroup("Dependencies")] | |
public UnityEvent<SceneChangeEvent> GameLoaded; | |
[BoxGroup("Debug")] | |
public bool SkipAnimation = false; | |
public Canvas LoadingAnimation; | |
public Camera LoadingScreenCamera; | |
private StateFileManager _stateFileManager; | |
private GameDataManager GDM; | |
private Dictionary<int, bool> _sceneLoadState; | |
public void Start() | |
{ | |
GDM = GameDataManager.Instance; | |
_stateFileManager = new StateFileManager(); | |
_sceneLoadState = new Dictionary<int, bool>(); | |
SceneStateManager.Instance.LoadingAnimation = LoadingAnimation; | |
SceneStateManager.Instance.LoadingCamera = LoadingScreenCamera; | |
GDM.GameLoaded.AddListener(LoadObjectPooler); | |
NotificationSoundSystem.Instance.OnNotification(new NotificationSoundEvent | |
{ | |
InstanceID = GetHashCode(), | |
Type = NotificationType.LoadingScreen | |
}); | |
Debug.Log("Loading game"); | |
LoadGame(); | |
} | |
private void OnDestroy() | |
{ | |
SceneManager.UnloadSceneAsync((int)Scenes.ObjectPooler); | |
} | |
private void LoadGame() | |
{ | |
if (GDM.SelectedSave != null) | |
{ | |
LoadFromSaveEntry(); | |
} | |
else | |
{ | |
GDM.StartRun(); | |
} | |
} | |
private async void LoadFromSaveEntry() | |
{ | |
Galaxy g = await _stateFileManager.LoadFromFile(GDM.SelectedSave.PathToSaveFile); | |
if (g != null) | |
{ | |
GDM.Initialize(g); | |
} | |
else | |
{ | |
throw new System.Exception("Not able to load save file"); | |
} | |
} | |
private void StartGame() | |
{ | |
QueueSceneLoad((int)Scenes.SurfaceView); | |
QueueSceneLoad((int)Scenes.TaskOverlay); | |
QueueSceneLoad((int)Scenes.SectorScreen); | |
StartCoroutine(WaitForScenesLoad(Scenes.SurfaceView)); | |
} | |
private void LoadObjectPooler() => StartCoroutine(LoadScene((int)Scenes.ObjectPooler, StartGame)); | |
private IEnumerator WaitForScenesLoad(Scenes activeScene) | |
{ | |
bool loaded = false; | |
while (!loaded) | |
{ | |
loaded = true; | |
foreach (var kv in _sceneLoadState) | |
{ | |
if (!kv.Value) loaded = false; | |
else Debug.Log($"Scene loaded: {kv.Key}"); | |
} | |
yield return null; | |
} | |
SceneStateManager.Instance.Load(activeScene); | |
LoadingScreenCamera.enabled = false; | |
} | |
private void QueueSceneLoad(int sceneBuildIndex) | |
{ | |
_sceneLoadState.Add(sceneBuildIndex, false); | |
StartCoroutine(LoadScene(sceneBuildIndex)); | |
} | |
private IEnumerator LoadScene(int scene) | |
{ | |
Debug.Log($"Loading Scene Index {scene}"); | |
var sceneAsync = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive); | |
while(!sceneAsync.isDone) | |
{ | |
yield return null; | |
} | |
Debug.Log($"Scene loaded {scene}"); | |
_sceneLoadState[scene] = true; | |
} | |
private IEnumerator LoadScene(int scene, Action callback) | |
{ | |
yield return LoadScene(scene); | |
callback(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment