feat: port narrative events and puzzles
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
class_name EventPanel
|
||||
extends PanelContainer
|
||||
|
||||
@onready var title: Label = $Title
|
||||
@onready var body: RichTextLabel = $Body
|
||||
@onready var entries: RichTextLabel = $Entries
|
||||
|
||||
var _engine: EventEngine
|
||||
var _definition: Dictionary = {}
|
||||
var _state: EventState
|
||||
var _context: Dictionary = {}
|
||||
var _selected_index := 0
|
||||
|
||||
func present(engine: EventEngine, definition: Dictionary, state: EventState, context: Dictionary) -> void:
|
||||
_engine = engine
|
||||
_definition = definition
|
||||
_state = state
|
||||
_context = context
|
||||
_selected_index = 0
|
||||
refresh()
|
||||
visible = true
|
||||
|
||||
func dismiss() -> void:
|
||||
visible = false
|
||||
_engine = null
|
||||
_definition = {}
|
||||
_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(_definition, _state, _selected_index, _context)
|
||||
if bool(outcome.get("accepted", false)) and not _state.is_over():
|
||||
_selected_index = 0
|
||||
refresh()
|
||||
return outcome
|
||||
|
||||
func current_state() -> EventState:
|
||||
return _state
|
||||
|
||||
func definition() -> Dictionary:
|
||||
return _definition
|
||||
|
||||
func visible_choices() -> Array[Dictionary]:
|
||||
return _engine.visible_choices(_definition, _state, _context) if _engine != null and _state != null else []
|
||||
|
||||
func refresh() -> void:
|
||||
if _state == null:
|
||||
return
|
||||
var node := _state.current_node(_definition)
|
||||
title.text = "Énigme" if _definition.get("tags", []).has("puzzle") else "Événement"
|
||||
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 = []
|
||||
var choices := visible_choices()
|
||||
for index in range(choices.size()):
|
||||
var cursor := "> " if index == _selected_index else " "
|
||||
lines.append("%s%s" % [cursor, String(choices[index].get("text", ""))])
|
||||
entries.text = "\n".join(lines)
|
||||
@@ -0,0 +1 @@
|
||||
uid://i2kut70gntxk
|
||||
Reference in New Issue
Block a user