refactor (#6)
Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
@@ -12,27 +14,44 @@ public partial class DeathScreen : Control
|
||||
[Export] public Label LivesLeftLabel { get; set; }
|
||||
[Export] public float TimeoutTime { get; set; } = 2.0f;
|
||||
[Export] public Godot.Collections.Array<Node> NodesToDisable { get; set; } = new();
|
||||
|
||||
|
||||
private GameManager _gameManager;
|
||||
private Timer _timer;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
SetLabels();
|
||||
_gameManager = GameManager.Instance;
|
||||
|
||||
// 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()
|
||||
{
|
||||
_timer = new Timer();
|
||||
@@ -42,31 +61,32 @@ public partial class DeathScreen : Control
|
||||
AddChild(_timer);
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
|
||||
private void ToggleNodes()
|
||||
{
|
||||
foreach (var node in NodesToDisable)
|
||||
{
|
||||
node.ProcessMode = node.ProcessMode == ProcessModeEnum.Disabled
|
||||
? ProcessModeEnum.Inherit
|
||||
node.ProcessMode = node.ProcessMode == ProcessModeEnum.Disabled
|
||||
? ProcessModeEnum.Inherit
|
||||
: ProcessModeEnum.Disabled;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void OnPlayerDeath()
|
||||
{
|
||||
if (_gameManager == null) return;
|
||||
|
||||
|
||||
ToggleNodes();
|
||||
SetLabels();
|
||||
Show();
|
||||
SetupTimer();
|
||||
}
|
||||
|
||||
|
||||
private void OnTimeout()
|
||||
{
|
||||
if (_gameManager == null || _gameManager.GetLives() == 0) return;
|
||||
|
||||
var lives = GameStateStore.Instance?.Player.Lives ?? 0;
|
||||
if (lives == 0) return;
|
||||
|
||||
GetTree().ReloadCurrentScene();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user