summaryrefslogtreecommitdiff
path: root/scripts/GameManager.cs
blob: a17ae79ca6f4ab757551b19eb82247a6e7ebb402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
		if (!game.LoadGame())
		{
			SwitchToScene(mainMenuScene);
		}
	}

	private void NewGame()
	{
		MessageLogData.Instance.ClearMessages();
		Game game = (Game)SwitchToScene(gameScene);
		game.NewGame();
		game.MainMenuRequested += LoadMainMenu;
	}

	private void OnGameRequest(bool load)
	{
		if (!load)
		{
			NewGame();
		}
		else 
		{
			LoadGame();
		}
	}
}