Files
whale-town/scripts/DialogueTestManager.gd
2025-12-05 19:00:14 +08:00

295 lines
8.1 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
extends Node
class_name DialogueTestManager
## 对话系统测试管理器
## 用于测试对话功能生成测试NPC和模拟对话
# 测试NPC数据
var test_npcs: Array[Dictionary] = []
var spawned_npcs: Dictionary = {}
# 引用
var world_manager: WorldManager
var dialogue_system: DialogueSystem
# 测试配置
const TEST_NPC_COUNT = 3
const NPC_SPAWN_RADIUS = 200.0
func _ready():
"""初始化测试管理器"""
print("DialogueTestManager initialized")
## 设置引用
func setup_references(world_mgr: WorldManager, dialogue_sys: DialogueSystem):
"""
设置必要的引用
@param world_mgr: 世界管理器
@param dialogue_sys: 对话系统
"""
world_manager = world_mgr
dialogue_system = dialogue_sys
# 连接对话系统信号
if dialogue_system:
dialogue_system.dialogue_started.connect(_on_dialogue_started)
dialogue_system.dialogue_ended.connect(_on_dialogue_ended)
dialogue_system.message_received.connect(_on_message_received)
## 生成测试NPC
func spawn_test_npcs(player_position: Vector2 = Vector2(640, 360)):
"""
在玩家周围生成测试NPC
@param player_position: 玩家位置
"""
if not world_manager:
print("WorldManager not set, cannot spawn NPCs")
return
# 清除已存在的测试NPC
clear_test_npcs()
# 生成测试NPC数据
_generate_test_npc_data()
# 在玩家周围生成NPC
for i in range(test_npcs.size()):
var npc_data = test_npcs[i]
# 计算NPC位置围绕玩家分布
var angle = (2.0 * PI * i) / test_npcs.size()
var offset = Vector2(cos(angle), sin(angle)) * NPC_SPAWN_RADIUS
var npc_position = player_position + offset
# 更新NPC位置
npc_data[CharacterData.FIELD_POSITION] = {
"x": npc_position.x,
"y": npc_position.y
}
# 生成NPC
var npc_character = world_manager.spawn_character(npc_data, false)
if npc_character:
spawned_npcs[npc_data[CharacterData.FIELD_ID]] = npc_character
print("Test NPC spawned: ", npc_data[CharacterData.FIELD_NAME], " at ", npc_position)
## 生成测试NPC数据
func _generate_test_npc_data():
"""生成测试NPC的数据"""
test_npcs.clear()
var npc_names = [
"测试小助手",
"友好的机器人",
"聊天达人",
"表情包大师",
"知识渊博者"
]
var npc_responses = [
["你好!我是测试小助手,很高兴见到你!", "有什么可以帮助你的吗?", "今天天气真不错呢!"],
["哔哔!我是友好的机器人!", "正在运行对话测试程序...", "系统状态:一切正常!"],
["嗨!想聊什么呢?", "我最喜欢和大家聊天了!", "你知道吗,聊天是最好的交流方式!"],
["😊 表情包来了!", "😂 哈哈哈,太有趣了!", "🎉 让我们用表情包交流吧!"],
["你知道吗?对话系统很复杂呢!", "我了解很多有趣的知识!", "想听听关于AI的故事吗"]
]
for i in range(min(TEST_NPC_COUNT, npc_names.size())):
var npc_id = "test_npc_" + str(i + 1)
var npc_data = CharacterData.create(npc_names[i], "system", Vector2.ZERO)
npc_data[CharacterData.FIELD_ID] = npc_id
# 添加测试专用数据
npc_data["test_responses"] = npc_responses[i]
npc_data["response_index"] = 0
test_npcs.append(npc_data)
## 清除测试NPC
func clear_test_npcs():
"""清除所有测试NPC"""
for npc_id in spawned_npcs.keys():
if world_manager:
world_manager.remove_character(npc_id)
spawned_npcs.clear()
print("Test NPCs cleared")
## 开始与NPC对话
func start_dialogue_with_npc(npc_id: String):
"""
开始与指定NPC对话
@param npc_id: NPC ID
"""
if not dialogue_system:
print("DialogueSystem not set")
return
if not spawned_npcs.has(npc_id):
print("NPC not found: ", npc_id)
return
dialogue_system.start_dialogue(npc_id)
# 发送NPC的欢迎消息
_send_npc_response(npc_id, true)
## 发送NPC响应
func _send_npc_response(npc_id: String, is_greeting: bool = false):
"""
发送NPC的自动响应
@param npc_id: NPC ID
@param is_greeting: 是否为问候语
"""
# 找到对应的NPC数据
var npc_data: Dictionary
for data in test_npcs:
if data[CharacterData.FIELD_ID] == npc_id:
npc_data = data
break
if npc_data.is_empty():
return
var responses = npc_data.get("test_responses", [])
if responses.is_empty():
return
var response_index = npc_data.get("response_index", 0)
var message = responses[response_index]
# 更新响应索引
npc_data["response_index"] = (response_index + 1) % responses.size()
# 延迟发送响应(模拟真实对话)
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
if dialogue_system:
dialogue_system.receive_message(npc_id, message)
dialogue_system.show_bubble(npc_id, message, 3.0)
## 获取附近的NPC
func get_nearby_npcs(position: Vector2, radius: float = 100.0) -> Array[Dictionary]:
"""
获取附近的测试NPC
@param position: 中心位置
@param radius: 搜索半径
@return: 附近NPC的信息数组
"""
var nearby_npcs: Array[Dictionary] = []
for npc_id in spawned_npcs.keys():
var npc_character = spawned_npcs[npc_id]
if npc_character and npc_character.global_position.distance_to(position) <= radius:
# 找到NPC数据
for data in test_npcs:
if data[CharacterData.FIELD_ID] == npc_id:
nearby_npcs.append({
"id": npc_id,
"name": data[CharacterData.FIELD_NAME],
"position": npc_character.global_position,
"distance": npc_character.global_position.distance_to(position)
})
break
# 按距离排序
nearby_npcs.sort_custom(func(a, b): return a.distance < b.distance)
return nearby_npcs
## 测试表情符号功能
func test_emoji_system():
"""测试表情符号系统"""
print("=== 测试表情符号系统 ===")
var test_messages = [
":smile: 你好!",
"今天心情很好 :happy:",
":laugh: 哈哈哈",
"加油! :thumbsup:",
":heart: 爱你哦"
]
for message in test_messages:
var converted = EmojiManager.convert_text_to_emoji(message)
print("原文: ", message)
print("转换后: ", converted)
print("---")
## 测试群组对话功能
func test_group_dialogue():
"""测试群组对话功能"""
print("=== 测试群组对话功能 ===")
if not dialogue_system:
print("DialogueSystem not available")
return
# 创建测试群组
var group_id = dialogue_system.create_group_dialogue("测试群组")
if group_id.is_empty():
print("Failed to create group")
return
print("Created group: ", group_id)
# 发送测试消息
var test_messages = [
"大家好!",
"这是群组对话测试",
":wave: 欢迎大家!"
]
for message in test_messages:
var success = dialogue_system.send_group_message(group_id, message)
print("Group message sent: ", message, " (success: ", success, ")")
## 显示测试帮助
func show_test_help():
"""显示测试帮助信息"""
print("=== 对话系统测试帮助 ===")
print("1. 使用 spawn_test_npcs() 生成测试NPC")
print("2. 使用 start_dialogue_with_npc(npc_id) 开始对话")
print("3. 使用 get_nearby_npcs(position) 查找附近NPC")
print("4. 使用 test_emoji_system() 测试表情符号")
print("5. 使用 test_group_dialogue() 测试群组对话")
print("6. 使用 clear_test_npcs() 清除测试NPC")
print("========================")
## 信号处理
func _on_dialogue_started(character_id: String):
"""对话开始时的处理"""
print("Dialogue started with: ", character_id)
func _on_dialogue_ended():
"""对话结束时的处理"""
print("Dialogue ended")
func _on_message_received(sender: String, message: String):
"""收到消息时的处理"""
print("Message from ", sender, ": ", message)
# 如果是玩家发送的消息让NPC自动回复
if sender == "player" and dialogue_system and dialogue_system.is_dialogue_active():
var target_id = dialogue_system.get_current_target()
if spawned_npcs.has(target_id):
_send_npc_response(target_id)
## 快速测试函数
func quick_test():
"""快速测试所有功能"""
print("=== 开始快速测试 ===")
# 生成NPC
spawn_test_npcs()
# 等待一秒
await get_tree().create_timer(1.0).timeout
# 测试表情符号
test_emoji_system()
# 测试群组对话
test_group_dialogue()
print("=== 快速测试完成 ===")
show_test_help()