feat: port data-driven dialogues
This commit is contained in:
@@ -24,6 +24,8 @@ func evaluate(condition: Dictionary, context: Dictionary) -> bool:
|
||||
return player.flags.get(StringName(condition.get("flag", "")), false) == condition.get("equals", true)
|
||||
&"reputation":
|
||||
return player.reputation.get(StringName(condition.get("faction", "")), 0) >= int(condition.get("min", 0))
|
||||
&"gold":
|
||||
return int(_payload(context).get("gold", 0)) >= int(condition.get("min", 0))
|
||||
&"archetype":
|
||||
if condition.has("is_set"):
|
||||
return (not player.archetype.is_empty()) == bool(condition["is_set"])
|
||||
|
||||
@@ -13,6 +13,7 @@ var _entries := {
|
||||
"quest": {},
|
||||
"trap": {},
|
||||
"event": {},
|
||||
"dialogue": {},
|
||||
}
|
||||
var _diagnostics := PackedStringArray()
|
||||
|
||||
@@ -56,6 +57,9 @@ func get_trap(id: StringName) -> Dictionary:
|
||||
func get_event(id: StringName) -> Dictionary:
|
||||
return _definition_for("event", id)
|
||||
|
||||
func get_dialogue(id: StringName) -> Dictionary:
|
||||
return _definition_for("dialogue", id)
|
||||
|
||||
func get_diagnostics() -> PackedStringArray:
|
||||
return _diagnostics
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
class_name DialogueEngine
|
||||
extends RefCounted
|
||||
|
||||
const ConditionEngineType = preload("res://scripts/domain/combat/condition_engine.gd")
|
||||
const DialogueStateType = preload("res://scripts/domain/dialogue/dialogue_state.gd")
|
||||
|
||||
var condition_engine: ConditionEngine
|
||||
|
||||
func _init(new_condition_engine: ConditionEngine = null) -> void:
|
||||
condition_engine = new_condition_engine if new_condition_engine != null else ConditionEngineType.new()
|
||||
|
||||
func start(definition: Dictionary, context: Dictionary) -> DialogueState:
|
||||
return DialogueStateType.new(definition)
|
||||
|
||||
func visible_choices(state: DialogueState, context: Dictionary) -> Array[Dictionary]:
|
||||
if state == null or state.is_over():
|
||||
return []
|
||||
var choices: Array[Dictionary] = []
|
||||
for option: Variant in state.current_node().get("options", []):
|
||||
if option is Dictionary and _conditions_met(option, context):
|
||||
choices.append(option)
|
||||
return choices
|
||||
|
||||
func choose(state: DialogueState, visible_index: int, context: Dictionary) -> Dictionary:
|
||||
var choices := visible_choices(state, context)
|
||||
if state == null or visible_index < 0 or visible_index >= choices.size():
|
||||
return {"accepted": false, "reason": &"invalid_choice", "applications": [], "opens_shop": false}
|
||||
var choice := choices[visible_index]
|
||||
var applications := condition_engine.apply_effects(choice.get("effects", []), context)
|
||||
var next_node := StringName(choice.get("goto", ""))
|
||||
if bool(choice.get("end", false)):
|
||||
state.ended = true
|
||||
elif next_node == &"__SHOP__":
|
||||
state.node_id = next_node
|
||||
state.ended = true
|
||||
elif not next_node.is_empty() and state.definition.get("nodes", {}).has(String(next_node)):
|
||||
state.node_id = next_node
|
||||
applications.append_array(condition_engine.apply_effects(state.current_node().get("on_enter", []), context))
|
||||
else:
|
||||
state.ended = true
|
||||
return {
|
||||
"accepted": true,
|
||||
"choice": choice,
|
||||
"applications": applications,
|
||||
"opens_shop": next_node == &"__SHOP__",
|
||||
"ended": state.is_over(),
|
||||
"node_id": state.node_id,
|
||||
}
|
||||
|
||||
func _conditions_met(choice: Dictionary, context: Dictionary) -> bool:
|
||||
if choice.has("condition"):
|
||||
var condition: Variant = choice.condition
|
||||
if not condition is Dictionary or not condition_engine.evaluate(condition, context):
|
||||
return false
|
||||
var conditions: Variant = choice.get("conditions", [])
|
||||
return condition_engine.evaluate_all(conditions, context) if conditions is Array else false
|
||||
@@ -0,0 +1 @@
|
||||
uid://753twggy18s5
|
||||
@@ -0,0 +1,23 @@
|
||||
class_name DialogueState
|
||||
extends RefCounted
|
||||
|
||||
var definition: Dictionary = {}
|
||||
var dialogue_id: StringName = &""
|
||||
var node_id: StringName = &""
|
||||
var ended := false
|
||||
|
||||
func _init(dialogue_definition: Dictionary = {}) -> void:
|
||||
definition = dialogue_definition.duplicate(true)
|
||||
dialogue_id = StringName(definition.get("id", ""))
|
||||
node_id = StringName(definition.get("start", ""))
|
||||
ended = dialogue_id.is_empty() or node_id.is_empty() or current_node().is_empty()
|
||||
|
||||
func current_node() -> Dictionary:
|
||||
var nodes: Variant = definition.get("nodes", {})
|
||||
if not nodes is Dictionary:
|
||||
return {}
|
||||
var node: Variant = nodes.get(String(node_id), {})
|
||||
return node if node is Dictionary else {}
|
||||
|
||||
func is_over() -> bool:
|
||||
return ended or node_id == &"__SHOP__"
|
||||
@@ -0,0 +1 @@
|
||||
uid://cs0wtl5qfl5rj
|
||||
+77
-8
@@ -25,6 +25,8 @@ const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
||||
const InventoryPanelType = preload("res://scripts/presentation/inventory_panel.gd")
|
||||
const EconomyType = preload("res://scripts/domain/items/economy.gd")
|
||||
const CraftingEngineType = preload("res://scripts/domain/items/crafting_engine.gd")
|
||||
const DialogueEngineType = preload("res://scripts/domain/dialogue/dialogue_engine.gd")
|
||||
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
||||
@onready var event_presenter: EventPresenter = $EventPresenter
|
||||
@onready var map_renderer: MapRenderer = $World/MapRenderer
|
||||
@onready var player_view: ActorView = $World/Player
|
||||
@@ -37,6 +39,7 @@ const CraftingEngineType = preload("res://scripts/domain/items/crafting_engine.g
|
||||
@onready var inventory_panel: InventoryPanel = $HUD/InventoryPanel
|
||||
@onready var shop_panel: ShopPanel = $HUD/ShopPanel
|
||||
@onready var crafting_panel: CraftingPanel = $HUD/CraftingPanel
|
||||
@onready var dialogue_panel: DialoguePanel = $HUD/DialoguePanel
|
||||
var pending_loot: Dictionary = {}
|
||||
var active_shop_npc: Dictionary = {}
|
||||
@onready var audio_library = $AudioLibrary
|
||||
@@ -52,6 +55,9 @@ var floor_transition: FloorTransition
|
||||
var spell_catalog: ContentCatalog
|
||||
var spell_state: SpellState
|
||||
var crafting_engine: CraftingEngine
|
||||
var dialogue_engine: DialogueEngine
|
||||
var dialogue_player: PlayerState
|
||||
var active_dialogue_npc: Dictionary = {}
|
||||
|
||||
func configure_new_run(settings: RunSettings, player_name: String, base_archetype: StringName) -> void:
|
||||
pending_run_settings = settings
|
||||
@@ -93,6 +99,8 @@ func _initialize_run_state() -> void:
|
||||
run_state.base_archetype = pending_base_archetype
|
||||
spell_catalog = ContentCatalogType.load_from_directory("res://data/ascii")
|
||||
crafting_engine = CraftingEngineType.new(spell_catalog)
|
||||
dialogue_engine = DialogueEngineType.new()
|
||||
dialogue_player = PlayerStateType.new({"id": game_state.player.id})
|
||||
game_state.content_catalog = spell_catalog
|
||||
spell_state = SpellStateType.new([&"spell_epines_empoisonnees"])
|
||||
floor_transition = FloorTransitionType.new(spell_catalog)
|
||||
@@ -151,6 +159,20 @@ func _prepare_exit_capture_state() -> void:
|
||||
_sync_camera_to_player()
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if dialogue_panel.visible and event is InputEventKey and event.is_pressed():
|
||||
if event.keycode == KEY_UP:
|
||||
dialogue_panel.select_next(-1)
|
||||
elif event.keycode == KEY_DOWN:
|
||||
dialogue_panel.select_next(1)
|
||||
elif event.keycode == KEY_ENTER or event.keycode == KEY_SPACE:
|
||||
_complete_dialogue_choice()
|
||||
elif event.keycode == KEY_ESCAPE:
|
||||
_close_dialogue()
|
||||
else:
|
||||
get_viewport().set_input_as_handled()
|
||||
return
|
||||
get_viewport().set_input_as_handled()
|
||||
return
|
||||
if crafting_panel.visible and event is InputEventKey and event.is_pressed():
|
||||
if event.keycode == KEY_UP:
|
||||
crafting_panel.select_next(-1)
|
||||
@@ -409,6 +431,9 @@ func get_game_state() -> GameState:
|
||||
func get_run_state() -> RunState:
|
||||
return run_state
|
||||
|
||||
func get_dialogue_player() -> PlayerState:
|
||||
return dialogue_player
|
||||
|
||||
func get_actor_cell_rect(actor_id: StringName) -> Rect2:
|
||||
if actor_id == &"player":
|
||||
return player_view.get_cell_rect()
|
||||
@@ -417,9 +442,9 @@ func get_actor_cell_rect(actor_id: StringName) -> Rect2:
|
||||
return Rect2()
|
||||
|
||||
func _process_interact() -> Array[TurnResult]:
|
||||
var merchant := _merchant_at_or_adjacent()
|
||||
if not merchant.is_empty():
|
||||
_open_shop_panel(merchant)
|
||||
var npc := _npc_at_or_adjacent()
|
||||
if not npc.is_empty():
|
||||
_open_dialogue(npc)
|
||||
return []
|
||||
# Ground pickup has precedence at the actor's cell; chests retain their D2
|
||||
# behavior and are deliberately not used as item sources in this slice.
|
||||
@@ -472,14 +497,58 @@ func _process_ground_pickup() -> Array[TurnResult]:
|
||||
_resolve_player_action([result], false)
|
||||
return [result]
|
||||
|
||||
func _merchant_at_or_adjacent() -> Dictionary:
|
||||
for npc: ActorState in game_state.floor_state.npcs.values():
|
||||
if npc.cell.distance_to(game_state.player.cell) <= 1.5:
|
||||
var definition := spell_catalog.get_npc(npc.content_id)
|
||||
if StringName(definition.get("role", "")) == &"merchant":
|
||||
func _npc_at_or_adjacent() -> Dictionary:
|
||||
for actor: ActorState in game_state.floor_state.npcs.values():
|
||||
if actor.cell.distance_to(game_state.player.cell) <= 1.5:
|
||||
var definition := spell_catalog.get_npc(actor.content_id)
|
||||
if not StringName(definition.get("dialogue_id", "")).is_empty():
|
||||
return definition
|
||||
return {}
|
||||
|
||||
func _open_dialogue(npc: Dictionary) -> void:
|
||||
var dialogue_id := StringName(npc.get("dialogue_id", ""))
|
||||
var definition := spell_catalog.get_dialogue(dialogue_id)
|
||||
if definition.is_empty():
|
||||
_record_dialogue_event(&"dialogue_unavailable", npc, {}, "Dialogue indisponible.")
|
||||
return
|
||||
active_dialogue_npc = npc.duplicate(true)
|
||||
var context := {"player": dialogue_player, "payload": {"gold": run_state.gold}}
|
||||
dialogue_panel.present(dialogue_engine, dialogue_engine.start(definition, context), context)
|
||||
_record_dialogue_event(&"dialogue_started", npc, {"dialogue_id": dialogue_id}, "%s vous adresse la parole." % String(npc.get("name", "Un inconnu")))
|
||||
|
||||
func _complete_dialogue_choice() -> void:
|
||||
var state := dialogue_panel.current_state()
|
||||
var choices := dialogue_panel.visible_choices()
|
||||
if state == null or choices.is_empty():
|
||||
return
|
||||
var selected_index := dialogue_panel._selected_index
|
||||
var choice: Dictionary = choices[selected_index]
|
||||
var choice_node_id := state.node_id
|
||||
var outcome := dialogue_panel.choose_selected()
|
||||
if not bool(outcome.get("accepted", false)):
|
||||
return
|
||||
_record_dialogue_event(&"dialogue_choice_selected", active_dialogue_npc, {"dialogue_id": state.dialogue_id, "node_id": choice_node_id, "choice_text": String(choice.get("text", "")), "applications": outcome.get("applications", [])}, String(choice.get("text", "")))
|
||||
if bool(outcome.get("opens_shop", false)):
|
||||
var npc := active_dialogue_npc.duplicate(true)
|
||||
_close_dialogue(false)
|
||||
_open_shop_panel(npc)
|
||||
elif state.is_over():
|
||||
_close_dialogue(false)
|
||||
|
||||
func _close_dialogue(journal: bool = true) -> void:
|
||||
if not dialogue_panel.visible:
|
||||
return
|
||||
var npc := active_dialogue_npc.duplicate(true)
|
||||
dialogue_panel.dismiss()
|
||||
active_dialogue_npc.clear()
|
||||
if journal:
|
||||
_record_dialogue_event(&"dialogue_closed", npc, {}, "Conversation terminee.")
|
||||
|
||||
func _record_dialogue_event(kind: StringName, npc: Dictionary, payload: Dictionary, message: String) -> Array[GameEvent]:
|
||||
var event_payload := {"npc_id": StringName(npc.get("id", ""))}
|
||||
event_payload.merge(payload, true)
|
||||
return _record_ranged_event(kind, &"", event_payload, message)
|
||||
|
||||
func _colony_info() -> Dictionary:
|
||||
var size := StringName(game_state.floor_state.metadata.get("colony_size", EconomyType.DEFAULT_COLONY_SIZE))
|
||||
var colony := EconomyType.colony_info(size)
|
||||
|
||||
@@ -10,6 +10,7 @@ const SUPPORTED_CATALOGS := {
|
||||
"npcs.json": "npc",
|
||||
"traps.json": "trap",
|
||||
}
|
||||
const SUPPORTED_DIALOGUE_DIRECTORY := "dialogues"
|
||||
const SUPPORTED_DIRECTORY_CATALOGS := {
|
||||
"quests": "quest",
|
||||
"events": "event",
|
||||
@@ -24,6 +25,7 @@ static func load_directory(path: String) -> Dictionary:
|
||||
result.entries[catalog_name] = []
|
||||
for catalog_name in SUPPORTED_DIRECTORY_CATALOGS.values():
|
||||
result.entries[catalog_name] = []
|
||||
result.entries["dialogue"] = []
|
||||
|
||||
var directory := DirAccess.open(path)
|
||||
if directory == null:
|
||||
@@ -78,4 +80,6 @@ static func _catalog_for(relative_path: String) -> String:
|
||||
var path_parts := relative_path.split("/", false)
|
||||
if path_parts.size() == 2 and SUPPORTED_DIRECTORY_CATALOGS.has(path_parts[0]):
|
||||
return SUPPORTED_DIRECTORY_CATALOGS[path_parts[0]]
|
||||
if path_parts.size() == 2 and path_parts[0] == SUPPORTED_DIALOGUE_DIRECTORY:
|
||||
return "dialogue"
|
||||
return ""
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
class_name DialoguePanel
|
||||
extends PanelContainer
|
||||
|
||||
@onready var title: Label = $Title
|
||||
@onready var body: RichTextLabel = $Body
|
||||
@onready var entries: RichTextLabel = $Entries
|
||||
|
||||
var _engine: DialogueEngine
|
||||
var _state: DialogueState
|
||||
var _context: Dictionary = {}
|
||||
var _selected_index := 0
|
||||
|
||||
func present(engine: DialogueEngine, state: DialogueState, context: Dictionary) -> void:
|
||||
_engine = engine
|
||||
_state = state
|
||||
_context = context
|
||||
_selected_index = 0
|
||||
refresh()
|
||||
visible = true
|
||||
|
||||
func dismiss() -> void:
|
||||
visible = false
|
||||
_engine = null
|
||||
_state = null
|
||||
_context = {}
|
||||
|
||||
func select_next(delta: int) -> void:
|
||||
var count := visible_choices().size()
|
||||
if count == 0:
|
||||
return
|
||||
_selected_index = posmod(_selected_index + delta, count)
|
||||
refresh()
|
||||
|
||||
func choose_selected() -> Dictionary:
|
||||
if _engine == null or _state == null:
|
||||
return {"accepted": false, "reason": &"inactive"}
|
||||
var outcome := _engine.choose(_state, _selected_index, _context)
|
||||
if bool(outcome.get("accepted", false)) and not _state.is_over():
|
||||
_selected_index = 0
|
||||
refresh()
|
||||
return outcome
|
||||
|
||||
func current_state() -> DialogueState:
|
||||
return _state
|
||||
|
||||
func visible_choices() -> Array[Dictionary]:
|
||||
return _engine.visible_choices(_state, _context) if _engine != null and _state != null else []
|
||||
|
||||
func refresh() -> void:
|
||||
if _state == null:
|
||||
return
|
||||
var node := _state.current_node()
|
||||
title.text = String(node.get("speaker", "Dialogue"))
|
||||
var text_value: Variant = node.get("text", "")
|
||||
body.text = "\n".join(text_value) if text_value is Array else String(text_value)
|
||||
var lines: PackedStringArray = []
|
||||
for index in range(visible_choices().size()):
|
||||
var cursor := "> " if index == _selected_index else " "
|
||||
lines.append("%s%s" % [cursor, String(visible_choices()[index].get("text", ""))])
|
||||
entries.text = "\n".join(lines)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dkhmc1ow2rfqv
|
||||
Reference in New Issue
Block a user