using Godot; using Mr.BrickAdventures; using Mr.BrickAdventures.Autoloads; using Mr.BrickAdventures.scripts.components; namespace Mr.BrickAdventures.scripts.UI; public partial class Hud : Control { [Export] public HealthComponent Health { get; set; } [Export] public Label CoinsLabel { get; set; } [Export] public ProgressBar HealthBar { get; set; } [Export] public Label LivesLabel { get; set; } public override void _Ready() { if (EventBus.Instance != null) { EventBus.Instance.CoinsChanged += OnCoinsChanged; EventBus.Instance.LivesChanged += OnLivesChanged; } var progression = ProgressionRoot.Instance; SetCoinsLabel(progression?.TotalCoins.Value ?? 0); SetLivesLabel(progression?.Lives.Value ?? 0); } public override void _ExitTree() { if (EventBus.Instance != null) { EventBus.Instance.CoinsChanged -= OnCoinsChanged; EventBus.Instance.LivesChanged -= OnLivesChanged; } } // Health belongs to the component on the player, not to progression, so it stays a direct read. public override void _Process(double delta) { SetHealthBar(); } private void OnLivesChanged(int lives) => SetLivesLabel(lives); private void OnCoinsChanged(int totalCoins) => SetCoinsLabel(totalCoins); private void SetCoinsLabel(int totalCoins) { CoinsLabel.Text = Tr("COINS_LABEL") + ": " + totalCoins; } private void SetLivesLabel(int lives) { LivesLabel.Text = Tr("LIVES_LABEL") + ": " + lives; } private void SetHealthBar() { HealthBar.Value = Health.Health; HealthBar.MaxValue = Health.MaxHealth; } }