Files
fgj-26/Scripts/Common/SceneManager.cs
T
2026-02-01 14:06:04 +02:00

85 lines
1.7 KiB
C#

using Godot;
using System;
public enum Scenes
{
Menu,
Game,
End
}
public partial class SceneManager : Node
{
public static SceneManager Instance;
[Export] private Node _root;
[Export] private PackedScene _gameScene;
[Export] private PackedScene _menuScene;
[Export] private PackedScene _endScene;
private Node _game;
private Node _menu;
private Node _end;
public override void _Ready()
{
Instance = this;
CallDeferred(nameof(EnterMenu));
}
public void ChangeScene(Scenes s)
{
GetTree().Paused = false;
switch (s)
{
case Scenes.Menu:
EnterMenu();
break;
case Scenes.Game:
EnterGame();
break;
case Scenes.End:
EnterEnd();
break;
default:
throw new ArgumentOutOfRangeException(nameof(s), s, null);
}
}
private void EnterMenu()
{
RemoveScene(ref _game);
RemoveScene(ref _end);
AddScene(ref _menuScene, ref _menu);
}
private void EnterGame()
{
RemoveScene(ref _menu);
RemoveScene(ref _end);
AddScene(ref _gameScene, ref _game);
}
private void EnterEnd() // Not the Minecraft
{
RemoveScene(ref _game);
RemoveScene(ref _menu);
AddScene(ref _endScene, ref _end);
}
private void RemoveScene(ref Node n)
{
if (n == null) return;
n.GetParent()?.RemoveChild(n);
n.QueueFree();
n = null;
}
private void AddScene(ref PackedScene s, ref Node n)
{
n = s.Instantiate();
_root.AddChild(n);
}
}