diff options
Diffstat (limited to 'scripts/GameManager.cs')
| -rw-r--r-- | scripts/GameManager.cs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/scripts/GameManager.cs b/scripts/GameManager.cs new file mode 100644 index 0000000..8bc8b6b --- /dev/null +++ b/scripts/GameManager.cs @@ -0,0 +1,59 @@ +using Godot; +using GodotPlugins.Game; +using System; +using TheLegendOfGustav.GUI; +using TheLegendOfGustav.Utils; + +namespace TheLegendOfGustav; + +public partial class GameManager : Node +{ + private PackedScene mainMenuScene = GD.Load<PackedScene>("res://scenes/GUI/main_menu.tscn"); + private PackedScene gameScene = GD.Load<PackedScene>("res://scenes/Game.tscn"); + + private Node currentScene; + + public override void _Ready() + { + base._Ready(); + LoadMainMenu(); + } + + private Node SwitchToScene(PackedScene scene) + { + if (currentScene != null && currentScene is Game gaimu) + { + gaimu.MainMenuRequested -= LoadMainMenu; + gaimu.QueueFree(); + gaimu = null; + } else if (currentScene != null && currentScene is MainMenu menu) + { + menu.QueueFree(); + menu.GameRequest -= OnGameRequest; + menu = null; + } + + currentScene?.QueueFree(); + currentScene = scene.Instantiate(); + AddChild(currentScene); + return currentScene; + } + + private void LoadMainMenu() + { + MainMenu menu = (MainMenu)SwitchToScene(mainMenuScene); + menu.GameRequest += OnGameRequest; + } + + private void LoadGame() + { + MessageLogData.Instance.ClearMessages(); + Game game = (Game)SwitchToScene(gameScene); + game.MainMenuRequested += LoadMainMenu; + } + + private void OnGameRequest(bool load) + { + LoadGame(); + } +} |
