Screen fader on level transition

This commit is contained in:
Marco 2025-02-24 13:47:38 +01:00
commit 959701be08
6 changed files with 80 additions and 8 deletions

View file

@ -1,11 +1,16 @@
using System.Threading.Tasks;
using Cirno.Scripts.Resources;
using Godot;
using GTweens.Builders;
using GTweensGodot.Extensions;
public partial class GlobalState : Node
{
public static GlobalState Instance { get; private set; }
public Node CurrentScene { get; set; }
private ColorRect _fader { get; set; }
public override void _Ready()
{
@ -16,6 +21,8 @@ public partial class GlobalState : Node
Viewport root = GetTree().Root;
// Using a negative index counts from the end, so this gets the last child node of `root`.
CurrentScene = root.GetChild(-1);
_fader = CreateFader();
}
public void GotoScene(string path)
@ -28,16 +35,26 @@ public partial class GlobalState : Node
// The solution is to defer the load to a later time, when
// we can be sure that no code from the current scene is running:
CallDeferred(MethodName.DeferredGotoScene, path, new MapStartDataResource());
//CallDeferred(MethodName.DeferredGotoScene, path, new MapStartDataResource());
GoToScene(path, new MapStartDataResource());
}
public void GoToScene(string path, MapStartDataResource startData)
{
// TODO: Implement startdata usage
CallDeferred(MethodName.DeferredGotoScene, path, startData);
GTweenSequenceBuilder.New()
//.Append(_fader.TweenModulateAlpha(0, 0f))
.Append(_fader.TweenModulateAlpha(1, 0.5f))
.AppendCallback(() =>
{
CallDeferred(MethodName.DeferredGotoScene, path, startData);
})
.Build()
.PlayUnpausable();
//CallDeferred(MethodName.DeferredGotoScene, path, startData);
}
private void DeferredGotoScene(string path, MapStartDataResource startData = null)
{
// It is now safe to remove the current scene.
@ -63,10 +80,43 @@ public partial class GlobalState : Node
// Call deferred if it gives issues
DeferredAddStartDataToGameManager(startData);
}
FadeIn();
}
private void DeferredAddStartDataToGameManager(MapStartDataResource resource)
{
GameManager.Instance.ApplyMapStartData(resource);
}
private ColorRect CreateFader()
{
var canvas = new CanvasLayer();
canvas.ProcessMode = ProcessModeEnum.Always;
var rect = new ColorRect();
rect.SetAnchorsPreset(Control.LayoutPreset.FullRect);
rect.Color = new Color(Colors.Black, 1f);
rect.ProcessMode = ProcessModeEnum.Always;
rect.Modulate = new Color(0,0,0,0);
rect.MouseFilter = Control.MouseFilterEnum.Ignore;
canvas.CallDeferred("add_child", rect);
this.CallDeferred("add_child", canvas);
return rect;
}
public void FadeOut()
{
_fader.TweenModulateAlpha(1, 0.5f).PlayUnpausable();
}
public void FadeIn()
{
_fader.TweenModulateAlpha(0, 1f).PlayUnpausable();
}
}