GameOver now visible

solved #30
pull/32/head
Sascha 2024-01-26 11:44:24 +07:00
parent e456608f06
commit e84d8184a5
4 changed files with 30 additions and 28 deletions

@ -222,7 +222,7 @@ scroll_active = false
shortcut_keys_enabled = false
[node name="VBoxGameOver" type="VBoxContainer" parent="Ui"]
visible = false
process_mode = 3
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
@ -261,7 +261,6 @@ text = "Restart"
icon = ExtResource("17_1t6sc")
[node name="VBoxPause" type="VBoxContainer" parent="Ui"]
visible = false
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5

@ -13,10 +13,9 @@ public partial class GameManager : Node {
get => gameOver;
private set {
gameOver = value;
GetTree().Paused = gameOver;
SoundManager.Instance.Stop(SoundManager.Sound.BackgroundMusic);
SoundManager.Instance.Play(SoundManager.Sound.GameOver);
RefreshUi?.Invoke(this, EventArgs.Empty);
OnGameOver?.Invoke(this,EventArgs.Empty);
}
}
@ -31,14 +30,15 @@ public partial class GameManager : Node {
private float spawnRate = 1f;
private float spawnTimer;
public event EventHandler RefreshUi;
public event EventHandler NewCountdown;
public event EventHandler OnRefreshUi;
public event EventHandler OnNewCountdown;
public event EventHandler OnGameOver;
public int AsteroidNumber {
get => asteroidNumber;
set {
asteroidNumber = value;
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -47,7 +47,7 @@ public partial class GameManager : Node {
set {
lives = value;
if (lives <= 0) GameOver = true;
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -56,7 +56,7 @@ public partial class GameManager : Node {
set {
points = value;
if (points >= nextLevelPoints) NextLevel();
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -64,7 +64,7 @@ public partial class GameManager : Node {
get => level;
private set {
level = value;
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -72,7 +72,7 @@ public partial class GameManager : Node {
get => nextLevelPoints;
private set {
nextLevelPoints = value;
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -80,7 +80,7 @@ public partial class GameManager : Node {
get => asteroidMaxNumber;
private set {
asteroidMaxNumber = value;
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -90,7 +90,7 @@ public partial class GameManager : Node {
set {
gamePaused = value;
GetTree().Paused = gamePaused;
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
}
}
@ -104,9 +104,9 @@ public partial class GameManager : Node {
Lives = MaxLives;
spawnTimer = spawnRate;
SoundManager.Instance.Play(SoundManager.Sound.BackgroundMusic);
RefreshUi?.Invoke(this, EventArgs.Empty);
OnRefreshUi?.Invoke(this, EventArgs.Empty);
Countdown = true;
NewCountdown?.Invoke(this, EventArgs.Empty);
OnNewCountdown?.Invoke(this, EventArgs.Empty);
}
public override void _Process(double delta) {

@ -28,7 +28,7 @@ public partial class InputManager : Node
//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;
if (GameManager.Instance.GamePaused || GameManager.Instance.Countdown || GameManager.Instance.GameOver) return;
//Movement
float moveVelocity = PlayerShip.Instance.MoveVelocity;
float rotationVelocity = PlayerShip.Instance.RotationVelocity;

@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Linq;
using Godot;
namespace GodotspaceShooter.Scripts;
@ -34,8 +35,9 @@ public partial class Ui : Node2D
if (GameManager.Instance is null)
GD.PrintErr("No GameManager found!");
else {
GameManager.Instance.RefreshUi += GameManager_RefreshUi;
GameManager.Instance.NewCountdown += GameManager_NewCountdown;
GameManager.Instance.OnRefreshUi += GameManager_OnRefreshUi;
GameManager.Instance.OnNewCountdown += GameManagerOnNewCountdown;
GameManager.Instance.OnGameOver += GameManager_OnGameOver;
InputManager.Instance.StartCountdown += InputManager_StartCountdown;
lblGameOver.Visible = GameManager.Instance.GameOver;
buttonRestart.Visible = GameManager.Instance.GameOver;
@ -43,17 +45,24 @@ public partial class Ui : Node2D
buttonRestart.Pressed += ButtonRestartOnPressed;
buttonResume.Pressed += ButtonResumeOnPressed;
buttonPauseRestart.Pressed += ButtonPauseRestartOnPressed;
GameManager_RefreshUi(this, EventArgs.Empty);
GameManager_OnRefreshUi(this, EventArgs.Empty);
}
}
private void GameManager_OnGameOver(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>()) canvasItem.Visible = true;
vBoxGameOver.Visible = true;
}
private void InputManager_StartCountdown(object sender, EventArgs e) {
lblAnyKey.Visible = false;
textAnyKey.Visible = false;
countdownRunning = true;
}
private void GameManager_NewCountdown(object sender, EventArgs e) {
private void GameManagerOnNewCountdown(object sender, EventArgs e) {
lblCountdown.Text = countdownTimer.ToString(CultureInfo.InvariantCulture);
vBoxCountdown.Visible = true;
lblAnyKey.Visible = true;
@ -72,7 +81,7 @@ public partial class Ui : Node2D
}
}
private void GameManager_RefreshUi(object sender, EventArgs e) {
private void GameManager_OnRefreshUi(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}";
@ -80,14 +89,8 @@ public partial class Ui : Node2D
rtlLevel.Text = $"Actual Level: {GameManager.Instance.Level}";
healthBar.MaxValue = GameManager.Instance.MaxLives;
healthBar.Value = GameManager.Instance.Lives;
if (GameManager.Instance.GameOver) {
lblReachedPoints.Text = $"You have reached level {GameManager.Instance.Level} and {GameManager.Instance.Points} points!";
vBoxGameOver.Visible = true;
}
// vBoxGameOver.Visible = GameManager.Instance.GameOver;
vBoxPause.Visible = GameManager.Instance.GamePaused;
vBoxGameOver.Visible = GameManager.Instance.GameOver;
}
private void ButtonRestartOnPressed() => GetTree().ReloadCurrentScene();