Refactor game state management to use a unified Stat system and update UI labels for production and corruption

This commit is contained in:
2025-08-23 02:44:54 +02:00
parent 2998e4c02a
commit 18312671d7
23 changed files with 298 additions and 109 deletions

View File

@@ -9,29 +9,40 @@ public partial class Main : Node
[Export] private Label _faithLabel;
[Export] private Label _followersLabel;
[Export] private Label _corruptionLabel;
[Export] private Label _productionLabel;
[Export] private MiraclePanel _miraclePanel;
[Export] private Sprite2D _worldSprite;
[Export] private Color _deadWorldColor = new Color("#581845");
[Export] private Color _deadWorldColor = new("#581845");
public override void _Ready()
{
GameBus.Instance.StateChanged += OnStateChanged;
GameBus.Instance.SubscribeToStat(Stat.Faith, UpdateFaithLabel);
GameBus.Instance.SubscribeToStat(Stat.Followers, UpdateFollowersLabel);
GameBus.Instance.SubscribeToStat(Stat.Corruption, UpdateCorruptionLabel);
GameBus.Instance.SubscribeToStat(Stat.Production, UpdateProductionLabel);
_miraclePanel.PopulateInitialButtons(GameBus.Instance.AllMiracles);
GameBus.Instance.StateChanged += OnStateChanged;
UpdateFaithLabel(GameBus.Instance.CurrentState.Get(Stat.Faith));
UpdateFollowersLabel(GameBus.Instance.CurrentState.Get(Stat.Followers));
UpdateCorruptionLabel(GameBus.Instance.CurrentState.Get(Stat.Corruption));
UpdateProductionLabel(GameBus.Instance.CurrentState.Get(Stat.Production));
}
public override void _ExitTree()
{
GameBus.Instance.UnsubscribeFromStat(Stat.Faith, UpdateFaithLabel);
GameBus.Instance.UnsubscribeFromStat(Stat.Followers, UpdateFollowersLabel);
GameBus.Instance.UnsubscribeFromStat(Stat.Corruption, UpdateCorruptionLabel);
GameBus.Instance.UnsubscribeFromStat(Stat.Production, UpdateProductionLabel);
GameBus.Instance.StateChanged -= OnStateChanged;
}
private void OnStateChanged(GameState newState)
{
_faithLabel.Text = $"Faith: {newState.Faith:F0} (+{newState.FaithPerSecond:F1}/s)";
_followersLabel.Text = $"Followers: {newState.Followers}";
_corruptionLabel.Text = $"Corruption: {newState.Corruption:F0}%";
UpdateWorldVisuals(newState.Corruption);
UpdateWorldVisuals(newState.Get(Stat.Corruption));
}
private void UpdateWorldVisuals(double corruption)
@@ -41,4 +52,24 @@ public partial class Main : Node
var ratio = (float)corruption / 100.0f;
shaderMaterial.SetShaderParameter("corruption_level", ratio);
}
private void UpdateFaithLabel(double newValue)
{
_faithLabel.Text = $"Faith: {newValue:F0}";
}
private void UpdateFollowersLabel(double newValue)
{
_followersLabel.Text = $"Followers: {newValue:F0}";
}
private void UpdateCorruptionLabel(double newValue)
{
_corruptionLabel.Text = $"Corruption: {newValue:F0}%";
}
private void UpdateProductionLabel(double production)
{
_productionLabel.Text = $"Production: {production:F0}";
}
}