126 lines
3.6 KiB
GDScript
126 lines
3.6 KiB
GDScript
extends Node2D
|
|
|
|
# 公寓管理器 - 处理房间、家具和交互
|
|
|
|
@onready var val: CharacterBody2D = $Val
|
|
@onready var rooms: Node2D = $Rooms
|
|
@onready var furniture: Node2D = $Furniture
|
|
|
|
signal room_changed(room_name: String)
|
|
signal furniture_clicked(furniture: Node)
|
|
|
|
var bridge: Node = null
|
|
|
|
func _ready():
|
|
# 连接输入事件
|
|
_connect_furniture_clicks()
|
|
_connect_room_detection()
|
|
|
|
# 获取 BridgeClient 引用(从父场景)
|
|
_find_bridge()
|
|
|
|
print("Apartment ready, furniture count: ", furniture.get_child_count())
|
|
|
|
func _find_bridge():
|
|
# 尝试多种路径找到 BridgeClient
|
|
bridge = get_node_or_null("../BridgeClient")
|
|
if not bridge:
|
|
bridge = get_node_or_null("/root/Main/BridgeClient")
|
|
if not bridge:
|
|
# 延迟查找
|
|
await get_tree().process_frame
|
|
bridge = get_node_or_null("../BridgeClient")
|
|
|
|
if bridge:
|
|
print("BridgeClient found")
|
|
else:
|
|
print("BridgeClient not found - running in standalone mode")
|
|
|
|
func _connect_furniture_clicks():
|
|
for item in furniture.get_children():
|
|
if item is Area2D:
|
|
item.input_event.connect(_on_furniture_clicked.bind(item))
|
|
print("Connected click for: ", item.get_meta("furniture_name", "未知"))
|
|
|
|
func _connect_room_detection():
|
|
for room in rooms.get_children():
|
|
if room is Area2D:
|
|
room.body_entered.connect(_on_room_entered.bind(room))
|
|
|
|
func _on_furniture_clicked(viewport: Node, event: InputEvent, shape_idx: int, furniture: Node):
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
var furniture_name = furniture.get_meta("furniture_name", "未知")
|
|
print("Clicked on furniture: ", furniture_name)
|
|
emit_signal("furniture_clicked", furniture)
|
|
|
|
# 让Val走过去交互
|
|
if val:
|
|
val.interact_with(furniture)
|
|
else:
|
|
print("Val not found!")
|
|
|
|
# 发送事件到Bridge
|
|
_send_to_bridge({
|
|
"type": "furniture_clicked",
|
|
"furniture": furniture_name,
|
|
"position": [furniture.global_position.x, furniture.global_position.y]
|
|
})
|
|
|
|
func _on_room_entered(body: Node2D, room: Area2D):
|
|
if body.name == "Val":
|
|
var room_name = room.get_meta("room_name", "未知")
|
|
print("Val entered room: ", room_name)
|
|
emit_signal("room_changed", room_name)
|
|
|
|
# 更新Val的当前房间
|
|
if val and "current_room" in val:
|
|
val.current_room = room_name
|
|
if val and "agent_state" in val:
|
|
val.agent_state["location"] = room_name
|
|
|
|
# 发送事件到Bridge
|
|
_send_to_bridge({
|
|
"type": "room_changed",
|
|
"room": room_name,
|
|
"agent_id": "val"
|
|
})
|
|
|
|
func get_furniture_list() -> Array:
|
|
var list = []
|
|
for item in furniture.get_children():
|
|
if item is Area2D:
|
|
list.append({
|
|
"name": item.get_meta("furniture_name", "未知"),
|
|
"interaction": item.get_meta("interaction", "use"),
|
|
"position": [item.global_position.x, item.global_position.y]
|
|
})
|
|
return list
|
|
|
|
func get_room_list() -> Array:
|
|
var list = []
|
|
for room in rooms.get_children():
|
|
if room is Area2D:
|
|
list.append(room.get_meta("room_name", "未知"))
|
|
return list
|
|
|
|
func _send_to_bridge(data: Dictionary):
|
|
if bridge and "send_command" in bridge:
|
|
bridge.send_command(data)
|
|
|
|
# 公共API:让Val移动到指定房间
|
|
func move_val_to_room(room_name: String):
|
|
for room in rooms.get_children():
|
|
if room is Area2D and room.get_meta("room_name", "") == room_name:
|
|
var target_pos = room.global_position + Vector2(0, 50)
|
|
if val:
|
|
val.move_to(target_pos)
|
|
return true
|
|
return false
|
|
|
|
# 公共API:获取指定家具
|
|
func get_furniture(furniture_name: String) -> Node:
|
|
for item in furniture.get_children():
|
|
if item is Area2D and item.get_meta("furniture_name", "") == furniture_name:
|
|
return item
|
|
return null
|