From e456608f06c548027b1b61ef455223d4e5cfef26 Mon Sep 17 00:00:00 2001 From: Sascha Date: Fri, 26 Jan 2024 11:24:00 +0100 Subject: [PATCH] Add Countdown --- Godot-space Shooter.csproj.old.5 | 3 ++ Images/keyboard_any.png | Bin 0 -> 405 bytes Images/keyboard_any.png.import | 34 ++++++++++++++++++++ Material/lblSettingsCountdown.tres | 8 +++++ Scenes/SpaceShooter.tscn | 49 +++++++++++++++++++++++++++-- Scripts/GameManager.cs | 19 +++++++---- Scripts/InputManager.cs | 15 +++++++-- Scripts/Ui.cs | 41 ++++++++++++++++++++++-- 8 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 Images/keyboard_any.png create mode 100644 Images/keyboard_any.png.import create mode 100644 Material/lblSettingsCountdown.tres diff --git a/Godot-space Shooter.csproj.old.5 b/Godot-space Shooter.csproj.old.5 index cc1116d..9d3fa14 100644 --- a/Godot-space Shooter.csproj.old.5 +++ b/Godot-space Shooter.csproj.old.5 @@ -4,4 +4,7 @@ true GodotspaceShooter + + + \ No newline at end of file diff --git a/Images/keyboard_any.png b/Images/keyboard_any.png new file mode 100644 index 0000000000000000000000000000000000000000..ef26da1b86f5377330a1a2c815581a4f54f362c4 GIT binary patch literal 405 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0J3?w7mbKU|emUKs7M+SzC{oH>NS%G|m0G|-o z|3m|z4xrk7T$erqS^OnIe!=ni@6Y?!^UuG(zkj{FJ%h!8TP#4SubwWBAs)xyUbW_8 zG8AA-c>hq8C4T|?2R^Q_uxi%+1l^8mX7Pi57p{5vKUn;sbxr*d_TBr8(sUQC5f0xV zcxV2EG&_%kLn@6OD*rfloYZO!%KbLqp>c+cT!L$klvGb;!PNz4nEji%_-lAiOpvJI zp7Wcl`I>>dkeTC^w333i1~(#Sq%VDCvYF49hie4TT2 z#a-C~7y=qN?#)VOQDt2hAe$d0!JK;yzh!H*nF8#%mMm&WXZpDM zA8*3vuZ!9E`2D1KDhhtqh asteroidNumber; @@ -84,7 +85,7 @@ public partial class GameManager : Node { } private bool gamePaused; - public bool GamePaused { + public bool GamePaused{ get => gamePaused; set { gamePaused = value; @@ -92,23 +93,29 @@ public partial class GameManager : Node { RefreshUi?.Invoke(this, EventArgs.Empty); } } + + public bool Countdown { get; set; } + public static GameManager Instance { get; private set; } [Export] public bool Debug { get; private set; } = true; - public override void _Ready() { Instance = this; Lives = MaxLives; spawnTimer = spawnRate; SoundManager.Instance.Play(SoundManager.Sound.BackgroundMusic); RefreshUi?.Invoke(this, EventArgs.Empty); + Countdown = true; + NewCountdown?.Invoke(this, EventArgs.Empty); } public override void _Process(double delta) { - spawnTimer -= (float)delta; - if (spawnTimer < 0) { - if (AsteroidNumber < asteroidMaxNumber) SpawnNewAsteroid(); - spawnTimer = spawnRate; + if (!GamePaused && !Countdown) { + spawnTimer -= (float)delta; + if (spawnTimer < 0) { + if (AsteroidNumber < asteroidMaxNumber) SpawnNewAsteroid(); + spawnTimer = spawnRate; + } } } diff --git a/Scripts/InputManager.cs b/Scripts/InputManager.cs index 3eefff9..be2a170 100644 --- a/Scripts/InputManager.cs +++ b/Scripts/InputManager.cs @@ -1,3 +1,4 @@ +using System; using Godot; namespace GodotspaceShooter.Scripts; @@ -13,11 +14,21 @@ public partial class InputManager : Node private Vector3 moveDirection; private Vector3 rotationDirection; - + + public event EventHandler StartCountdown; + + public static InputManager Instance { get; private set; } + + public override void _Ready() => Instance = this; + public override void _UnhandledInput(InputEvent @event) { + //Countdown + if (GameManager.Instance.Countdown && Input.IsAnythingPressed()) StartCountdown?.Invoke(this,EventArgs.Empty); + //Pause if (Input.IsActionJustReleased(GAME_PAUSE)) GameManager.Instance.GamePaused = GameManager.Instance.GamePaused switch { true => false, false => true }; - + + if (GameManager.Instance.GamePaused || GameManager.Instance.Countdown) return; //Movement float moveVelocity = PlayerShip.Instance.MoveVelocity; float rotationVelocity = PlayerShip.Instance.RotationVelocity; diff --git a/Scripts/Ui.cs b/Scripts/Ui.cs index b61dfc1..e8923b0 100644 --- a/Scripts/Ui.cs +++ b/Scripts/Ui.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using Godot; namespace GodotspaceShooter.Scripts; @@ -17,6 +18,14 @@ public partial class Ui : Node2D [Export] private VBoxContainer vBoxPause; [Export] private Button buttonResume; [Export] private Button buttonPauseRestart; + [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 static Ui Instance { get; private set; } @@ -25,19 +34,45 @@ public partial class Ui : Node2D if (GameManager.Instance is null) GD.PrintErr("No GameManager found!"); else { - GameManager.Instance.RefreshUi += RefreshUi; + GameManager.Instance.RefreshUi += GameManager_RefreshUi; + GameManager.Instance.NewCountdown += GameManager_NewCountdown; + InputManager.Instance.StartCountdown += InputManager_StartCountdown; lblGameOver.Visible = GameManager.Instance.GameOver; buttonRestart.Visible = GameManager.Instance.GameOver; lblReachedPoints.Visible = GameManager.Instance.GameOver; buttonRestart.Pressed += ButtonRestartOnPressed; buttonResume.Pressed += ButtonResumeOnPressed; buttonPauseRestart.Pressed += ButtonPauseRestartOnPressed; - RefreshUi(this, EventArgs.Empty); + GameManager_RefreshUi(this, EventArgs.Empty); } } + private void InputManager_StartCountdown(object sender, EventArgs e) { + lblAnyKey.Visible = false; + textAnyKey.Visible = false; + countdownRunning = true; + } + + private void GameManager_NewCountdown(object sender, EventArgs e) { + lblCountdown.Text = countdownTimer.ToString(CultureInfo.InvariantCulture); + vBoxCountdown.Visible = true; + lblAnyKey.Visible = true; + textAnyKey.Visible = true; + } + + public override void _Process(double delta) { + if (GameManager.Instance.Countdown && countdownRunning) { + countdownTimer -= delta; + lblCountdown.Text = Mathf.RoundToInt(countdownTimer).ToString(); + if (countdownTimer <= 0) { + vBoxCountdown.Visible = false; + countdownRunning = false; + GameManager.Instance.Countdown = false; + } + } + } - private void RefreshUi(object sender, EventArgs e) { + private void GameManager_RefreshUi(object sender, EventArgs e) { rtlLifes.Text = $"Lives: {GameManager.Instance.Lives}"; rtlAsteroids.Text = $"Asteroids: {GameManager.Instance.AsteroidNumber} / {GameManager.Instance.AsteroidMaxNumber}"; rtlPoints.Text = $"Points: {GameManager.Instance.Points}";