feat: port data-driven dialogues
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user