refactor: HUD 节点改用唯一名称引用,场景识别改用分组

- TopBarHUD 用 @onready + % 唯一名称替代硬编码路径
- TimeSystem 用 game_scene 分组替代场景名字符串比较
This commit is contained in:
2026-08-03 22:33:40 +08:00
parent 476834176c
commit b6c76d83d7
4 changed files with 19 additions and 13 deletions
+1
View File
@@ -4,6 +4,7 @@
[ext_resource type="PackedScene" uid="uid://dl0tb68vsw3c6" path="res://src/UI/HUD/TopBarHUD.tscn" id="1_g6xmk"]
[node name="MainGame" type="Node" unique_id=2053561265]
groups = ["game_scene"]
script = ExtResource("1_8bu23")
[node name="TopBarHud" parent="." unique_id=1115394694 instance=ExtResource("1_g6xmk")]
+2 -1
View File
@@ -51,9 +51,10 @@ func _process(delta: float) -> void:
## 判断当前场景是否为游戏世界场景(主菜单/存档界面不推进时间)。
## 通过 "game_scene" 分组识别,而非场景名。
func _is_in_game() -> bool:
var scene := get_tree().current_scene
return scene != null and scene.name == "MainGame"
return scene != null and scene.is_in_group("game_scene")
## 推进一日并广播对应信号,跨月/跨年时补发 month_passed / year_passed。
+5
View File
@@ -49,6 +49,7 @@ size_flags_horizontal = 3
size_flags_vertical = 8
size_flags_stretch_ratio = 2.0
label_settings = SubResource("LabelSettings_bn2o5")
unique_name_in_owner = true
[node name="Label_Reputation" type="Label" parent="HBoxContainer" unique_id=985110673]
layout_mode = 2
@@ -74,6 +75,7 @@ layout_mode = 2
theme_override_fonts/font = ExtResource("1_pa2rn")
theme_override_font_sizes/font_size = 32
text = "暂停"
unique_name_in_owner = true
[node name="Button_Speed1" type="Button" parent="HBoxContainer/SpeedGroup" unique_id=2144893128]
custom_minimum_size = Vector2(48, 0)
@@ -81,6 +83,7 @@ layout_mode = 2
theme_override_fonts/font = ExtResource("1_pa2rn")
theme_override_font_sizes/font_size = 32
text = "1x"
unique_name_in_owner = true
[node name="Button_Speed5" type="Button" parent="HBoxContainer/SpeedGroup" unique_id=2144893129]
custom_minimum_size = Vector2(48, 0)
@@ -88,6 +91,7 @@ layout_mode = 2
theme_override_fonts/font = ExtResource("1_pa2rn")
theme_override_font_sizes/font_size = 32
text = "5x"
unique_name_in_owner = true
[node name="Button_Speed20" type="Button" parent="HBoxContainer/SpeedGroup" unique_id=2144893130]
custom_minimum_size = Vector2(48, 0)
@@ -95,6 +99,7 @@ layout_mode = 2
theme_override_fonts/font = ExtResource("1_pa2rn")
theme_override_font_sizes/font_size = 32
text = "20x"
unique_name_in_owner = true
[node name="placeholder_1" type="Label" parent="HBoxContainer" unique_id=1042446504]
layout_mode = 2
+11 -12
View File
@@ -5,24 +5,25 @@ extends Control
## 请求返回主菜单。
signal sign_return_mainmenu
## 三个流速按钮的节点名(与 tscn 中的 SpeedGroup 子节点对应)。
const SPEED_BUTTONS := ["Button_Speed1", "Button_Speed5", "Button_Speed20"]
## 激活档位按钮颜色。
const ACTIVE_COLOR := Color(1.0, 1.0, 1.0)
## 未激活档位按钮颜色。
const INACTIVE_COLOR := Color(0.55, 0.55, 0.55)
@onready var time_label: Label = %Value_Time
@onready var pause_button: Button = %Button_Pause
@onready var speed_buttons: Array[Button] = [%Button_Speed1, %Button_Speed5, %Button_Speed20]
func _ready() -> void:
for i in SPEED_BUTTONS.size():
var btn: Button = $HBoxContainer/SpeedGroup.get_node(SPEED_BUTTONS[i])
btn.button_up.connect(_on_speed_button_up.bind(i))
for i in speed_buttons.size():
speed_buttons[i].button_up.connect(_on_speed_button_up.bind(i))
TimeSystem.day_passed.connect(_on_day_passed)
_update_speed_ui()
$HBoxContainer/Value_Time.text = TimeSystem.get_date_text()
time_label.text = TimeSystem.get_date_text()
## 每日推进时刷新日期显示。
func _on_day_passed(_year: int, _month: int, _day: int) -> void:
$HBoxContainer/Value_Time.text = TimeSystem.get_date_text()
time_label.text = TimeSystem.get_date_text()
## 点击"返回"按钮。
func _on_button_return_button_up() -> void:
@@ -41,9 +42,7 @@ func _on_speed_button_up(index: int) -> void:
## 刷新速度组 UI:暂停按钮文字与各档位高亮状态。
func _update_speed_ui() -> void:
var pause_btn: Button = $HBoxContainer/SpeedGroup/Button_Pause
pause_btn.text = "继续" if TimeSystem.paused else "暂停"
for i in SPEED_BUTTONS.size():
var btn: Button = $HBoxContainer/SpeedGroup.get_node(SPEED_BUTTONS[i])
pause_button.text = "继续" if TimeSystem.paused else "暂停"
for i in speed_buttons.size():
var active := (i == TimeSystem.speed_index) and not TimeSystem.paused
btn.modulate = ACTIVE_COLOR if active else INACTIVE_COLOR
speed_buttons[i].modulate = ACTIVE_COLOR if active else INACTIVE_COLOR