using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Sirenix.OdinInspector;
namespace KairoEngine.Core
{
// Todo: Refactor class to be a Singleton
// Todo: Create a public function to trigger the change scene action
// Todo: The OnChangeScene event should be fired from this script after the scene change starts
// Todo: Change functionality in TriggerChangeScene to use this singleton instead of the OnChangeScene event
// Todo: The game location should be query to the GlobalMap
/// Handles changing scenes in the game.
public class SceneManager : MonoBehaviour
{
/// Name of the current location from the Global Map.
[ReadOnly] public string currentLocation = "";
private SceneChangeData nextSceneData;
private float time;
void OnEnable()
{
TransitionEvents.OnChangeScene += OnChangeScene;
UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
TransitionEvents.OnLocationName += SetCurrentLocation;
}
void OnDisable()
{
TransitionEvents.OnChangeScene -= OnChangeScene;
UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
TransitionEvents.OnLocationName -= SetCurrentLocation;
}
private void OnChangeScene(SceneChangeData sceneChangeData, float time)
{
nextSceneData = sceneChangeData;
this.time = time;
TransitionEvents.FadeIn(time);
TransitionEvents.OnTransitionFinish += ChangeScene;
}
private void ChangeScene()
{
TransitionEvents.OnTransitionFinish -= ChangeScene;
SerializationEvents.SerializeData();
TransitionEvents.GetLocationName();
Debug.Log("Loading scene " + nextSceneData.sceneName);
UnityEngine.SceneManagement.SceneManager.LoadScene(nextSceneData.sceneName, LoadSceneMode.Single);
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("Finished loading scene " + scene.name);
TransitionEvents.FadeOut(time);
string link = "";
if(nextSceneData != null) link = nextSceneData.sceneLink;
TransitionEvents.SceneStart(link);
}
private void SetCurrentLocation(string locationName)
{
currentLocation = locationName;
}
}
}