Files
przygody-pana-cegly/scripts/UI/DeathScreen.cs
Gabriel Kaszewski e8d1da8f89 feat: lives and death move to the new domain (commit C of the swap)
fixes the regression commit B introduced: coins and skills lived in the
Attempt ledger, but nothing discarded it, because LivesStateHandler still
called the old store and HandlePlayerDeath was never invoked. Dying kept
everything earned that level. Discard-on-death is the ledger's purpose,
so it could not be separated from it.

new AttemptFailed(remaining, runOver) event, raised after the life is
spent and the ledger discarded. DeathScreen and GameOverScreen react to
that instead of being wired to HealthComponent.Death by scene connections
which fired *before* the life was charged - on the last life game-over
saw one life left and showed nothing, then the death screen saw zero and
refused to reload. 18 scene connections deleted; the race is now
unrepresentable rather than fixed.

DeathScreen also had two read paths for lives - a signal and a direct
store read - because neither was reliable alone. Now one.

Hud reads both facts from signals; only health still polls, and that
belongs to the component rather than to progression. GameManager no
longer touches GameStateStore or SaveSystem at all.
2026-07-28 20:19:35 +02:00

82 lines
2.3 KiB
C#

using Godot;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.UI;
[GlobalClass]
public partial class DeathScreen : Control
{
[Export] public LevelResource CurrentLevel { get; set; }
[Export] public Label CurrentLevelLabel { get; set; }
[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 Timer _timer;
private bool _runOver;
public override void _Ready()
{
EventBus.Instance.AttemptFailed += OnAttemptFailed;
}
public override void _ExitTree()
{
if (EventBus.Instance != null)
EventBus.Instance.AttemptFailed -= OnAttemptFailed;
}
/// <summary>
/// Shows the death screen.
/// </summary>
/// <remarks>
/// Driven by the settled outcome rather than by a scene connection to
/// <c>HealthComponent.Death</c>, so the life is already spent and the count already correct. The old
/// arrangement read <c>Lives</c> from a handler racing the one that wrote it, then read it a second
/// time on timeout — two read paths for one fact, because neither was reliable alone.
/// </remarks>
private void OnAttemptFailed(int remainingLives, bool runOver)
{
_runOver = runOver;
LivesLeftLabel.Text = $" x {remainingLives}";
if (CurrentLevel != null)
CurrentLevelLabel.Text = CurrentLevel.LevelName;
ToggleNodes();
Show();
SetupTimer();
}
private void SetupTimer()
{
_timer = new Timer
{
WaitTime = TimeoutTime,
OneShot = true,
};
_timer.Timeout += OnTimeout;
AddChild(_timer);
_timer.Start();
}
private void ToggleNodes()
{
foreach (var node in NodesToDisable)
{
node.ProcessMode = node.ProcessMode == ProcessModeEnum.Disabled
? ProcessModeEnum.Inherit
: ProcessModeEnum.Disabled;
}
}
private void OnTimeout()
{
// Game over is the other screen's job; this one only retries.
if (_runOver) return;
GetTree().ReloadCurrentScene();
}
}