using System; using System.Globalization; using System.Linq; using Godot; namespace GodotspaceShooter.Scripts; public partial class Ui : Node2D { [ExportCategory("Stats")] [Export] private Label lblLevel; [Export] private Label lblPoints; [Export] private ProgressBar barHealth; [Export] private ProgressBar barLevel; [ExportCategory("GameOver")] [Export] private VBoxContainer vBoxGameOver; [Export] private Label lblGameOver; [Export] private Button butGameOverRestart; [Export] private Label lblReachedPoints; [Export] private Button butGameOverExit; [ExportCategory("Pause")] [Export] private VBoxContainer vBoxPause; [Export] private Button buttonResume; [Export] private Button buttonPauseRestart; [Export] private Button butPauseExit; [ExportCategory("Countdown")] [Export] private VBoxContainer vBoxCountdown; [Export] private Label lblCountdown; [Export] private TextureRect textAnyKey; [Export] private Label lblAnyKey; private const double countdownMaxTimer = 3; private double countdownTimer = countdownMaxTimer; private bool countdownRunning; public override void _Ready() { if (GameManager.Instance is null) GD.PrintErr("No GameManager found!"); else { GameManager.Instance.OnRefreshStats += GameManager_RefreshStats; GameManager.Instance.OnNewCountdown += GameManager_NewCountdown; GameManager.Instance.OnGameOver += GameManager_GameOver; InputManager.Instance.StartCountdown += InputManager_StartCountdown; lblGameOver.Visible = GameManager.Instance.GameOver; butGameOverRestart.Visible = GameManager.Instance.GameOver; lblReachedPoints.Visible = GameManager.Instance.GameOver; butGameOverRestart.Pressed += AllButtonRestartOnPressed; buttonResume.Pressed += ButtonResumeOnPressed; buttonPauseRestart.Pressed += AllButtonRestartOnPressed; butPauseExit.Pressed += AllButExitOnPressed; butGameOverExit.Pressed += AllButExitOnPressed; GameManager_RefreshStats(this, EventArgs.Empty); } } private void AllButExitOnPressed() => GetTree().Quit(); private void GameManager_GameOver(object sender, EventArgs e) { GD.Print($"Game Over! You have reached level {GameManager.Instance.Level} and {GameManager.Instance.Points} points!"); lblReachedPoints.Text = $"You have reached level {GameManager.Instance.Level} and {GameManager.Instance.Points} points!"; foreach (CanvasItem canvasItem in vBoxGameOver.GetChildren().Cast()) canvasItem.Visible = true; vBoxGameOver.Visible = true; } private void InputManager_StartCountdown(object sender, EventArgs e) { lblAnyKey.Visible = false; textAnyKey.Visible = false; lblCountdown.Visible = true; countdownRunning = true; } private void GameManager_NewCountdown(object sender, EventArgs e) { lblCountdown.Text = countdownTimer.ToString(CultureInfo.InvariantCulture); foreach (CanvasItem canvasItem in vBoxCountdown.GetChildren().Cast()) canvasItem.Visible = true; vBoxCountdown.Visible = true; lblCountdown.Visible = false; } public override void _Process(double delta) { if (GameManager.Instance.Countdown && countdownRunning) { countdownTimer -= delta; lblCountdown.Text = Mathf.RoundToInt(countdownTimer).ToString(); if (countdownTimer <= 0) { foreach (CanvasItem canvasItem in vBoxCountdown.GetChildren().Cast()) canvasItem.Visible = false; vBoxCountdown.Visible = false; countdownRunning = false; GameManager.Instance.Countdown = false; } } } private void GameManager_RefreshStats(object sender, EventArgs e) { lblLevel.Text = $"{GameManager.Instance.Level}"; lblPoints.Text = $"{GameManager.Instance.Points}"; barHealth.MaxValue = GameManager.Instance.MaxLives; barHealth.Value = GameManager.Instance.Lives; barHealth.TooltipText = $"{GameManager.Instance.Lives} / {GameManager.Instance.MaxLives}"; barLevel.MaxValue = GameManager.Instance.NextLevelPoints; barLevel.Value = GameManager.Instance.Points - GameManager.Instance.LastLevelPoints; barLevel.TooltipText = $"{GameManager.Instance.Points} / {GameManager.Instance.NextLevelPoints}"; vBoxPause.Visible = GameManager.Instance.GamePaused; vBoxGameOver.Visible = GameManager.Instance.GameOver; } private static void ButtonResumeOnPressed() => GameManager.Instance.GamePaused = false; private void AllButtonRestartOnPressed() { GameManager.Instance.GamePaused = false; GetTree().ReloadCurrentScene(); } public static void ShowMessage(string message) => GD.Print(message); }