extends Node2D # 主场景管理器 @onready var apartment: Node2D = $Apartment @onready var bridge: Node = $BridgeClient @onready var command_input: LineEdit = $UI/Panel/VBoxContainer/CommandInput @onready var send_button: Button = $UI/Panel/VBoxContainer/SendButton func _ready(): # 连接UI事件 send_button.pressed.connect(_on_send_command) command_input.text_submitted.connect(_on_command_submitted) # 连接公寓事件 apartment.room_changed.connect(_on_room_changed) apartment.furniture_clicked.connect(_on_furniture_clicked) # 连接Bridge事件 if bridge: bridge.connected_to_bridge.connect(_on_bridge_connected) bridge.disconnected_from_bridge.connect(_on_bridge_disconnected) print("AgentLiving started!") print("Click on furniture to make Val interact with it!") func _on_send_command(): var command = command_input.text.strip_edges() if command.is_empty(): return _send_command_to_val(command) command_input.clear() func _on_command_submitted(text: String): _on_send_command() func _send_command_to_val(command: String): print("User command: ", command) var val = apartment.get_node_or_null("Val") if val: val.receive_command(command) # 同时发送到Bridge if bridge and bridge.is_connected: bridge.send_command({ "type": "user_command", "command": command, "timestamp": Time.get_unix_time_from_system() }) func _on_room_changed(room_name: String): print("Val entered: ", room_name) func _on_furniture_clicked(furniture: Node): print("Furniture clicked: ", furniture.get_meta("furniture_name", "未知")) func _on_bridge_connected(): print("Bridge connected!") # 请求初始状态同步 if bridge: bridge.request_state_sync() func _on_bridge_disconnected(): print("Bridge disconnected!") func _input(event): # 按ESC退出 if event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE: get_tree().quit() # 按R重新连接Bridge if event is InputEventKey and event.pressed and event.keycode == KEY_R: if bridge: bridge._connect_to_bridge()