fix: add null guards for status_label, hand_area, and label throughout UI
This commit is contained in:
@@ -11,28 +11,28 @@ var is_selected: bool = false
|
||||
@onready var label: Label = $Label
|
||||
|
||||
func setup(card: Card) -> void:
|
||||
card_data = card
|
||||
update_display()
|
||||
card_data = card
|
||||
update_display()
|
||||
|
||||
func update_display() -> void:
|
||||
if card_data == null:
|
||||
return
|
||||
var suits := ["S", "H", "C", "D", "SJ", "BJ"]
|
||||
var ranks := ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "SJ", "BJ"]
|
||||
var suit := card_data.suit()
|
||||
var rank := card_data.rank()
|
||||
if rank < ranks.size() and suit < suits.size():
|
||||
label.text = "%s %s" % [suits[suit], ranks[rank]]
|
||||
modulate = Color.WHITE if not is_selected else Color(1.2, 1.2, 0.8)
|
||||
if card_data == null:
|
||||
return
|
||||
var suits := ["S", "H", "C", "D", "SJ", "BJ"]
|
||||
var ranks := ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "SJ", "BJ"]
|
||||
var suit := card_data.suit()
|
||||
var rank := card_data.rank()
|
||||
if rank < ranks.size() and suit < suits.size() and label:
|
||||
label.text = "%s %s" % [suits[suit], ranks[rank]]
|
||||
modulate = Color.WHITE if not is_selected else Color(1.2, 1.2, 0.8)
|
||||
|
||||
func set_selected(sel: bool) -> void:
|
||||
is_selected = sel
|
||||
update_display()
|
||||
is_selected = sel
|
||||
update_display()
|
||||
|
||||
func _on_gui_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if event.double_click:
|
||||
card_double_clicked.emit(self)
|
||||
else:
|
||||
card_clicked.emit(self)
|
||||
if event is InputEventMouseButton:
|
||||
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if event.double_click:
|
||||
card_double_clicked.emit(self)
|
||||
else:
|
||||
card_clicked.emit(self)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
extends Control
|
||||
|
||||
func _ready() -> void:
|
||||
var start_btn := $VBoxContainer/StartButton as Button
|
||||
var quit_btn := $VBoxContainer/QuitButton as Button
|
||||
start_btn.pressed.connect(_on_start_pressed)
|
||||
quit_btn.pressed.connect(_on_quit_pressed)
|
||||
var start_btn := $VBoxContainer/StartButton as Button
|
||||
var quit_btn := $VBoxContainer/QuitButton as Button
|
||||
start_btn.pressed.connect(_on_start_pressed)
|
||||
quit_btn.pressed.connect(_on_quit_pressed)
|
||||
|
||||
func _on_start_pressed() -> void:
|
||||
get_tree().change_scene_to_file("res://src/ui/scenes/training_room.tscn")
|
||||
get_tree().change_scene_to_file("res://src/ui/scenes/training_room.tscn")
|
||||
|
||||
func _on_quit_pressed() -> void:
|
||||
get_tree().quit()
|
||||
get_tree().quit()
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user