WIP new SceneTransition system

This commit is contained in:
2025-07-10 19:07:46 +02:00
parent e8c24e18be
commit 16251db248
7 changed files with 271 additions and 69 deletions
@@ -0,0 +1,88 @@
using Godot;
namespace Babushka.scripts.CSharp.Common.SceneManagement;
public partial class SceneTransitionThreaded : Node2D
{
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");
//yield(animationPlayer, "animation_finished");
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;
}
}
@@ -0,0 +1 @@
uid://bo2jik2jtuqlw
+21
View File
@@ -1,3 +1,5 @@
using System.Threading.Tasks;
using Babushka.scripts.CSharp.Common.SceneManagement;
using Godot;
namespace Babushka.scripts.CSharp.Common;
@@ -5,9 +7,12 @@ namespace Babushka.scripts.CSharp.Common;
public partial class SceneTransition : Node
{
[Export] private PackedScene _sceneToLoad;
[Export] private string[] _sceneNamesToLoad;
[Export] private int _sceneIndex;
[Export] private Node? _sceneInstanceParent;
[Export] private bool _unloadSelf = true;
// todo: remove and replace with indexed system
public void LoadScene()
{
Node sceneInstance = _sceneToLoad.Instantiate();
@@ -24,6 +29,22 @@ public partial class SceneTransition : Node
}
}
public void LoadSceneAtIndex(int index)
{
string sceneName = _sceneNamesToLoad[index];
SceneTransitionThreaded.Instance.ChangeSceneToFileThreaded(sceneName);
UnloadAfterDelay();
}
private async void UnloadAfterDelay()
{
await ToSignal(GetTree().CreateTimer(1.0f), "timeout"); // 1.0f seconds
if (_unloadSelf)
{
QueueFree();
}
}
public void Quit()
{
GetTree().Quit();