fix: add null guards for status_label, hand_area, and label throughout UI

This commit is contained in:
xiaji
2026-05-29 21:39:06 +08:00
parent 08f0a9f4f8
commit c6e806a155
3 changed files with 37 additions and 34 deletions

View File

@@ -26,18 +26,20 @@ func start_training() -> void:
func _on_turn_ready(player_idx: int, is_human: bool) -> void:
if is_human:
hand_area.enable_input()
status_label.text = "你的回合"
if status_label: status_label.text = "你的回合"
else:
hand_area.disable_input()
status_label.text = "%s 思考中..." % controller.game_state.player_names[player_idx]
if status_label and controller and controller.game_state:
status_label.text = "%s 思考中..." % controller.game_state.player_names[player_idx]
func _on_play_pressed() -> void:
if not hand_area: return
var selected := hand_area.selected_cards
if selected.is_empty():
return
var result := controller.handle_human_play(selected)
if not result.ok:
status_label.text = "无效出牌"
if status_label: status_label.text = "无效出牌"
return
hand_area.clear_selection()
_refresh_ui()
@@ -45,27 +47,28 @@ func _on_play_pressed() -> void:
func _on_pass_pressed() -> void:
var result := controller.handle_human_pass()
if not result.ok:
status_label.text = "不能过牌"
if status_label: status_label.text = "不能过牌"
return
_refresh_ui()
func _on_hint_pressed() -> void:
var hint := controller.get_hint()
if hint == null or hint.type == -1:
status_label.text = "建议:过牌"
if status_label: status_label.text = "建议:过牌"
return
if not hand_area: return
hand_area.clear_selection()
for card in hint.cards:
for cn in hand_area.card_nodes:
if cn.card_data != null and cn.card_data.card_id == card.card_id:
cn.set_selected(true)
hand_area.selected_cards.append(card)
status_label.text = "建议牌型: %s (rank=%d)" % [hint.type, hint.primary_rank]
if status_label: status_label.text = "建议牌型: %s (rank=%d)" % [hint.type, hint.primary_rank]
func _on_game_ended(winner_team: int, reason: String) -> void:
status_label.text = "游戏结束! 队伍 %d 获胜" % winner_team
hand_area.disable_input()
if status_label: status_label.text = "游戏结束! 队伍 %d 获胜" % winner_team
if hand_area: hand_area.disable_input()
func _refresh_ui() -> void:
if controller and controller.game_state:
if controller and controller.game_state and hand_area:
hand_area.update_hand(controller.game_state.get_hand(0))