diff --git a/scripts/UI/DeathScreen.cs b/scripts/UI/DeathScreen.cs index 31f0d2a..e8dfd05 100644 --- a/scripts/UI/DeathScreen.cs +++ b/scripts/UI/DeathScreen.cs @@ -2,6 +2,7 @@ using Godot; using Mr.BrickAdventures; using Mr.BrickAdventures.Autoloads; using Mr.BrickAdventures.scripts.Resources; +using Mr.BrickAdventures.scripts.State; namespace Mr.BrickAdventures.scripts.UI; @@ -20,18 +21,35 @@ public partial class DeathScreen : Control public override void _Ready() { _gameManager = GetNode(Constants.GameManagerPath); - SetLabels(); + + // Subscribe to lives changed event for reactive updates + EventBus.Instance.LivesChanged += OnLivesChanged; + } + + public override void _ExitTree() + { + if (EventBus.Instance != null) + { + EventBus.Instance.LivesChanged -= OnLivesChanged; + } + } + + private void OnLivesChanged(int lives) + { + // Update the label when lives change + LivesLeftLabel.Text = $" x {lives}"; } private void SetLabels() { - if (_gameManager == null) return; - if (CurrentLevel != null) { CurrentLevelLabel.Text = CurrentLevel.LevelName; } - LivesLeftLabel.Text = $" x {_gameManager.GetLives()}"; + + // Read current lives from store + var lives = GameStateStore.Instance?.Player.Lives ?? 0; + LivesLeftLabel.Text = $" x {lives}"; } private void SetupTimer() @@ -66,7 +84,8 @@ public partial class DeathScreen : Control private void OnTimeout() { - if (_gameManager == null || _gameManager.GetLives() == 0) return; + var lives = GameStateStore.Instance?.Player.Lives ?? 0; + if (lives == 0) return; GetTree().ReloadCurrentScene(); }