创建新工程
This commit is contained in:
174
scripts/SimpleDialogueTest.gd
Normal file
174
scripts/SimpleDialogueTest.gd
Normal file
@@ -0,0 +1,174 @@
|
||||
extends Node
|
||||
## 简单对话测试脚本
|
||||
## 直接在Main场景中使用,无需复杂的类型声明
|
||||
|
||||
# 测试NPC数据
|
||||
var test_npcs = []
|
||||
var spawned_npcs = {}
|
||||
|
||||
# 引用
|
||||
var world_manager = null
|
||||
var dialogue_system = null
|
||||
|
||||
## 初始化测试
|
||||
func setup_test(world_mgr, dialogue_sys):
|
||||
"""设置测试环境"""
|
||||
world_manager = world_mgr
|
||||
dialogue_system = dialogue_sys
|
||||
print("Simple dialogue test setup complete")
|
||||
|
||||
## 生成测试NPC
|
||||
func spawn_test_npcs(player_position: Vector2 = Vector2(640, 360)):
|
||||
"""生成测试NPC"""
|
||||
if not world_manager:
|
||||
print("WorldManager not available")
|
||||
return
|
||||
|
||||
# 检查WorldManager是否已正确设置
|
||||
if not world_manager.character_container:
|
||||
print("WorldManager character container not set, waiting...")
|
||||
# 延迟一秒后重试
|
||||
await get_tree().create_timer(1.0).timeout
|
||||
if not world_manager.character_container:
|
||||
print("Error: WorldManager character container still not set")
|
||||
return
|
||||
|
||||
# 清除已存在的NPC
|
||||
clear_test_npcs()
|
||||
|
||||
# 创建测试NPC数据
|
||||
var npc_data = [
|
||||
{
|
||||
"id": "test_npc_1",
|
||||
"name": "测试小助手",
|
||||
"responses": ["你好!我是测试小助手!", "有什么可以帮助你的吗?", "今天天气真不错呢!"]
|
||||
},
|
||||
{
|
||||
"id": "test_npc_2",
|
||||
"name": "友好机器人",
|
||||
"responses": ["哔哔!我是友好的机器人!", "正在运行对话测试程序...", "系统状态:一切正常!"]
|
||||
},
|
||||
{
|
||||
"id": "test_npc_3",
|
||||
"name": "聊天达人",
|
||||
"responses": ["嗨!想聊什么呢?", "我最喜欢和大家聊天了!", "你知道吗,聊天是最好的交流方式!"]
|
||||
}
|
||||
]
|
||||
|
||||
# 生成NPC
|
||||
for i in range(npc_data.size()):
|
||||
var npc = npc_data[i]
|
||||
|
||||
# 计算位置
|
||||
var angle = (2.0 * PI * i) / npc_data.size()
|
||||
var offset = Vector2(cos(angle), sin(angle)) * 200.0
|
||||
var npc_position = player_position + offset
|
||||
|
||||
# 创建角色数据
|
||||
var character_data = CharacterData.create(npc["name"], "system", npc_position)
|
||||
character_data[CharacterData.FIELD_ID] = npc["id"]
|
||||
|
||||
# 生成角色
|
||||
var character = world_manager.spawn_character(character_data, false)
|
||||
if character:
|
||||
spawned_npcs[npc["id"]] = {
|
||||
"character": character,
|
||||
"responses": npc["responses"],
|
||||
"response_index": 0
|
||||
}
|
||||
print("Test NPC spawned: ", npc["name"], " at ", npc_position)
|
||||
|
||||
print("Generated ", spawned_npcs.size(), " test NPCs")
|
||||
show_help()
|
||||
|
||||
## 清除测试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")
|
||||
|
||||
## 开始对话
|
||||
func start_dialogue_with_npc(npc_id: String):
|
||||
"""开始与NPC对话"""
|
||||
if not dialogue_system or not spawned_npcs.has(npc_id):
|
||||
print("Cannot start dialogue with: ", npc_id)
|
||||
return
|
||||
|
||||
dialogue_system.start_dialogue(npc_id)
|
||||
|
||||
# 发送欢迎消息
|
||||
send_npc_response(npc_id)
|
||||
|
||||
## 发送NPC响应
|
||||
func send_npc_response(npc_id: String):
|
||||
"""发送NPC响应"""
|
||||
if not spawned_npcs.has(npc_id):
|
||||
return
|
||||
|
||||
var npc_data = spawned_npcs[npc_id]
|
||||
var responses = npc_data["responses"]
|
||||
var index = npc_data["response_index"]
|
||||
|
||||
var message = responses[index]
|
||||
npc_data["response_index"] = (index + 1) % responses.size()
|
||||
|
||||
# 延迟发送
|
||||
await get_tree().create_timer(1.0).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):
|
||||
"""获取附近的NPC"""
|
||||
var nearby = []
|
||||
|
||||
for npc_id in spawned_npcs.keys():
|
||||
var npc_data = spawned_npcs[npc_id]
|
||||
var character = npc_data["character"]
|
||||
if character and character.global_position.distance_to(position) <= radius:
|
||||
nearby.append({
|
||||
"id": npc_id,
|
||||
"name": character.name,
|
||||
"distance": character.global_position.distance_to(position)
|
||||
})
|
||||
|
||||
return nearby
|
||||
|
||||
## 显示帮助
|
||||
func show_help():
|
||||
"""显示测试帮助"""
|
||||
print("=== 对话系统测试帮助 ===")
|
||||
print("1. 已生成 ", spawned_npcs.size(), " 个测试NPC")
|
||||
print("2. 走近NPC按E键开始对话")
|
||||
print("3. 在对话框中输入消息测试")
|
||||
print("4. NPC会自动回复")
|
||||
print("5. 支持表情符号::smile: :happy: :laugh:")
|
||||
print("========================")
|
||||
|
||||
## 测试表情符号
|
||||
func test_emoji():
|
||||
"""测试表情符号转换"""
|
||||
print("=== 表情符号测试 ===")
|
||||
var test_messages = [
|
||||
":smile: 你好!",
|
||||
"今天心情很好 :happy:",
|
||||
":laugh: 哈哈哈"
|
||||
]
|
||||
|
||||
for msg in test_messages:
|
||||
var converted = EmojiManager.convert_text_to_emoji(msg)
|
||||
print("原文: ", msg, " -> 转换: ", converted)
|
||||
|
||||
## 处理玩家消息
|
||||
func handle_player_message(sender: String, _message: String):
|
||||
"""处理玩家发送的消息,让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)
|
||||
Reference in New Issue
Block a user