You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Babushka/scripts/CSharp/Common/SceneManagement/SceneTransitionThreaded.cs

87 lines
2.6 KiB

using Godot;
namespace Babushka.scripts.CSharp.Common.SceneManagement;
public partial class SceneTransitionThreaded : CanvasLayer
{
public static SceneTransitionThreaded Instance { get; private set; }
// set to true if don't wanna use it
public bool userCodeLoadCompleted = true;
private ThreadedLoadingStateEn threadedLoadingState = ThreadedLoadingStateEn._none;
public enum ThreadedLoadingStateEn
{
_none,
_fading_in,
_loading,
_loading_user_code
}
[Export] private AnimationPlayer animationPlayer;
private string scenePathThreaded = "";
public override void _EnterTree()
{
Instance = this;
}
public override void _Process(double delta)
{
CheckLoadThreaded();
}
private void CheckLoadThreaded()
{
if (scenePathThreaded == "")
{ return; }
if (threadedLoadingState == ThreadedLoadingStateEn._loading && ResourceLoader.LoadThreadedGetStatus(scenePathThreaded) == ResourceLoader.ThreadLoadStatus.Loaded)
{
OnResourceLoadThreadedComplete();
}
else if (threadedLoadingState == ThreadedLoadingStateEn._loading_user_code && userCodeLoadCompleted)
{
OnResourceLoadThreadedCompleteUserCode();
}
}
public async void ChangeSceneToFile(string scenePath)
{
animationPlayer.Play("fadeIn");
await ToSignal(animationPlayer, "animation_finished");
GetTree().ChangeSceneToFile(scenePath);
animationPlayer.Play("fadeOut");
}
public void OnFadeInCompletedThreaded()
{
if (scenePathThreaded == "")
{ return; }
ResourceLoader.LoadThreadedRequest(scenePathThreaded);
threadedLoadingState = ThreadedLoadingStateEn._loading;
}
// https://docs.godotengine.org/en/stable/tutorials/io/background_loading.html
public void ChangeSceneToFileThreaded(string scenePath)
{
scenePathThreaded = scenePath;
animationPlayer.Play("fadeIn");
threadedLoadingState = ThreadedLoadingStateEn._fading_in;
}
private void OnResourceLoadThreadedComplete()
{
PackedScene loadedScene = (PackedScene)ResourceLoader.LoadThreadedGet(scenePathThreaded);
//Node sceneInstance = loadedScene.Instantiate();
GetTree().ChangeSceneToPacked(loadedScene);
threadedLoadingState = ThreadedLoadingStateEn._loading_user_code;
}
private void OnResourceLoadThreadedCompleteUserCode()
{
animationPlayer.Play("fadeOut");
scenePathThreaded = "";
threadedLoadingState = ThreadedLoadingStateEn._none;
}
}