基于Godot引擎的视觉小说系统包含以下核心组件:
[
{"cmd": "show_bg", "res": "背景图片路径"},
{"cmd": "show_char_sprite2d", "position": "center", "char_name": "角色资源名称"},
"欢迎来到游戏世界!",
"这是对话系统演示。",
{"cmd": "hide_char_sprite2d", "position": "center"}
]
1. 文本段落:直接使用字符串格式
2. 命令段落:使用JSON对象格式
{"cmd": "命令名称", "参数1": "值1", "参数2": "值2"}
# resource_loader.gd 示例
RESOURCE_PATHS = {
"backgrounds": {
"main_menu": "res://背景/main_menu.png",
"forest": "res://背景/forest.png"
},
"characters": {
"protagonist": "res://角色/主角.png"
}
}
通过add_resource_path()方法动态添加资源路径
# 在 dialogue_full.gd 中添加
func _handle_command(command: Dictionary):
match command["cmd"]:
"custom_command":
print("执行自定义命令")
# 添加自定义逻辑
命令开发流程:
系统新增了自定义函数队列机制,允许在对话序列中插入游戏逻辑函数调用。
# 在对话序列中插入自定义函数
VisualNovelSystem.queue_custom_function(
func(): day_2_logic() # 匿名函数封装方法调用
)
# GameLogic.gd 示例
class_name GameLogic
extends Node
func day_2_logic():
print("执行第二天的逻辑")
# 这里可以有复杂的游戏逻辑
func start_sequence():
# 启动对话序列
VisualNovelSystem.start_full_system("res://Data/text/day1_intro.json")
# 添加自定义逻辑到队列
VisualNovelSystem.queue_custom_function(
func(): day_2_logic()
)
# 添加更多对话
VisualNovelSystem.start_empty_system("res://Data/text/day1_end.json")
# 添加完成回调
VisualNovelSystem.add_completion_callback(
func(): print("所有任务完成!")
)
# 带参数的方法
func activate_character(character_name: String, location: Vector2):
print("激活角色: ", character_name, " 在位置: ", location)
# 在队列中添加的两种方式
VisualNovelSystem.queue_custom_function(
func(): activate_character("Alice", Vector2(100, 200))
)
# 或者使用参数数组形式
VisualNovelSystem.queue_custom_function(
activate_character,
["Alice", Vector2(100, 200)]
)