Files
max-effort/Code/Systems/GameManager.cs

122 lines
3.1 KiB
C#

using Godot;
using MaxEffort.Code.Core;
using MaxEffort.Code.Data;
namespace MaxEffort.Code.Systems;
[GlobalClass]
public partial class GameManager : Node
{
[Export] private Godot.Collections.Array<DayConfig> _days;
[Export] private HazardSystem _hazardSystem;
[Export] private Node _minigameContainer;
[Export] private Control _winScreen;
[Export] private Control _loseScreen;
[Export] private Control _gameCompleteScreen;
[Export] private Label _dayLabel;
[Export] private PackedScene _mainMenuScene;
private int _currentDayIndex = 0;
private Node _currentMiniGame;
public override void _Ready()
{
EventBus.OnLiftCompleted += HandleLiftResult;
CallDeferred(nameof(StartDay), _currentDayIndex);
}
public override void _ExitTree()
{
EventBus.OnLiftCompleted -= HandleLiftResult;
}
private void StartDay(int index)
{
if (index >= _days.Count)
{
GD.Print("YOU BEAT THE GYM! ALL DAYS COMPLETE.");
_currentMiniGame?.QueueFree();
_hazardSystem.ClearHazards();
if (_winScreen != null) _winScreen.Visible = false;
if (_loseScreen != null) _loseScreen.Visible = false;
if (_gameCompleteScreen != null)
{
_gameCompleteScreen.Visible = true;
}
return;
}
var config = _days[index];
_currentMiniGame?.QueueFree();
_hazardSystem.ClearHazards();
if (config.MiniGameScene != null)
{
_currentMiniGame = config.MiniGameScene.Instantiate();
_minigameContainer.AddChild(_currentMiniGame);
var liftSystem = _currentMiniGame.GetNode<BaseLiftSystem>("System");
liftSystem?.Initialize(config.TargetWeight, config.Gravity);
}
_hazardSystem.SetAvailableHazards(config.AvailableHazards);
if (_dayLabel != null) _dayLabel.Text = config.DayTitle;
if (_winScreen != null) _winScreen.Visible = false;
if (_loseScreen != null) _loseScreen.Visible = false;
GD.Print($"Started {config.DayTitle}");
}
private void HandleLiftResult(bool success)
{
if (success)
{
GD.Print("Day Complete! Next day loading...");
if (_winScreen != null) _winScreen.Visible = true;
}
else
{
GD.Print("FAIL! Go home.");
if (_loseScreen != null) _loseScreen.Visible = true;
}
}
public void OnNextDayPressed()
{
NextDay();
}
public void OnRetryPressed()
{
RestartDay();
}
public void OnMenuPressed()
{
if (_mainMenuScene != null)
{
GetTree().ChangeSceneToPacked(_mainMenuScene);
}
else
{
GetTree().Quit();
}
}
private void NextDay()
{
_currentDayIndex++;
StartDay(_currentDayIndex);
}
private void RestartDay()
{
GetTree().ReloadCurrentScene();
}
}