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}";
}
}

View File

@@ -24,12 +24,13 @@ shader_parameter/corrupted_color = Color(0.3, 0.1, 0.2, 1)
shader_parameter/corruption_level = 0.0
shader_parameter/tolerance = 0.1
[node name="World" type="Node2D" node_paths=PackedStringArray("_faithLabel", "_followersLabel", "_corruptionLabel", "_miraclePanel", "_worldSprite")]
[node name="World" type="Node2D" node_paths=PackedStringArray("_faithLabel", "_followersLabel", "_corruptionLabel", "_productionLabel", "_miraclePanel", "_worldSprite")]
script = ExtResource("1_p8rbg")
_faithLabel = NodePath("UiLayer/Control/MarginContainer/Main/Top/FaithLabel")
_followersLabel = NodePath("UiLayer/Control/MarginContainer/Main/Top/FollowersLabel")
_corruptionLabel = NodePath("UiLayer/Control/MarginContainer/Main/Top/CorruptionLabel")
_miraclePanel = NodePath("UiLayer/Control/MarginContainer/Main/Bottom")
_faithLabel = NodePath("UiLayer/Control/Main/Top/FaithLabel")
_followersLabel = NodePath("UiLayer/Control/Main/Top/FollowersLabel")
_corruptionLabel = NodePath("UiLayer/Control/Main/Top/CorruptionLabel")
_productionLabel = NodePath("UiLayer/Control/Main/Top/ProductionLabel")
_miraclePanel = NodePath("UiLayer/Control/Main/ScrollContainer/Bottom")
_worldSprite = NodePath("World Sprite")
[node name="UiLayer" type="CanvasLayer" parent="."]
@@ -44,45 +45,121 @@ grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="MarginContainer" type="MarginContainer" parent="UiLayer/Control"]
[node name="Main" type="MarginContainer" parent="UiLayer/Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 0
theme_override_constants/margin_left = 8
theme_override_constants/margin_top = 8
theme_override_constants/margin_right = 8
theme_override_constants/margin_bottom = 8
[node name="Main" type="VBoxContainer" parent="UiLayer/Control/MarginContainer"]
[node name="Top" type="HBoxContainer" parent="UiLayer/Control/Main"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 0
alignment = 1
[node name="Top" type="HBoxContainer" parent="UiLayer/Control/MarginContainer/Main"]
layout_mode = 2
[node name="FaithLabel" type="Label" parent="UiLayer/Control/MarginContainer/Main/Top"]
[node name="FaithLabel" type="Label" parent="UiLayer/Control/Main/Top"]
layout_mode = 2
text = "Faith Label"
[node name="FollowersLabel" type="Label" parent="UiLayer/Control/MarginContainer/Main/Top"]
[node name="FollowersLabel" type="Label" parent="UiLayer/Control/Main/Top"]
layout_mode = 2
text = "FollowersLabel"
[node name="CorruptionLabel" type="Label" parent="UiLayer/Control/MarginContainer/Main/Top"]
[node name="ProductionLabel" type="Label" parent="UiLayer/Control/Main/Top"]
layout_mode = 2
text = "ProductionLabel"
[node name="CorruptionLabel" type="Label" parent="UiLayer/Control/Main/Top"]
layout_mode = 2
text = "CorruptionLabel"
[node name="Bottom" type="GridContainer" parent="UiLayer/Control/MarginContainer/Main"]
[node name="ScrollContainer" type="ScrollContainer" parent="UiLayer/Control/Main"]
custom_minimum_size = Vector2(430, 200)
layout_mode = 2
size_flags_vertical = 6
size_flags_horizontal = 8
size_flags_vertical = 10
horizontal_scroll_mode = 0
[node name="Bottom" type="GridContainer" parent="UiLayer/Control/Main/ScrollContainer"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
theme_override_constants/h_separation = 4
theme_override_constants/v_separation = 4
columns = 3
script = ExtResource("2_hcu3t")
_miracleButtonScene = ExtResource("3_qdkat")
[node name="ScrollContainer2" type="ScrollContainer" parent="UiLayer/Control/Main"]
custom_minimum_size = Vector2(400, 100)
layout_mode = 2
size_flags_horizontal = 0
size_flags_vertical = 10
vertical_scroll_mode = 0
[node name="HBoxContainer" type="HBoxContainer" parent="UiLayer/Control/Main/ScrollContainer2"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="Button" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button2" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button3" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button4" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button5" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button6" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button7" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button8" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button9" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Button10" type="Button" parent="UiLayer/Control/Main/ScrollContainer2/HBoxContainer"]
layout_mode = 2
disabled = true
text = "Effect"
[node name="Camera2D" type="Camera2D" parent="."]
[node name="World Sprite" type="Sprite2D" parent="."]

View File

@@ -17,7 +17,8 @@ public partial class MiracleButton : Button
return;
}
Text = $"{_miracle.Name}\nCost: {_miracle.FaithCost} Faith";
Text = BuildText();
TooltipText = BuildTooltipText();
Pressed += OnPressed;
}
@@ -35,8 +36,25 @@ public partial class MiracleButton : Button
public void SetMiracle(MiracleDefinition miracle)
{
_miracle = miracle;
Text = $"{_miracle.Name}\nCost: {_miracle.FaithCost} Faith";
Text = BuildText();
TooltipText = BuildTooltipText();
}
public MiracleDefinition GetMiracle() { return _miracle; }
private string BuildText()
{
return $"{_miracle.Name}\nCost: {_miracle.FaithCost} Faith";
}
private string BuildTooltipText()
{
var tooltip = $"Cost: {_miracle.FaithCost} Faith\nRequires: {_miracle.FollowersRequired} Followers\nEffects:\n";
foreach (var effect in _miracle.Effects)
{
if (effect.ToString() == string.Empty) continue;
tooltip += $"- {effect}\n";
}
return tooltip.TrimEnd();
}
}