extends Node ## Daten-getriebenes Dialog-System (Autoload, SPEC.md Abschnitte 6 und 8). ## ## Spielt verzweigte Dialogbäume aus JSON-Dateien unter data/dialogue ab. ## Sprecher-Zeilen erscheinen über der Sprechblase des jeweiligen Sprechers ## (Kind-Node "SpeechBubble"), die vom Spieler gewählte Antwort über seiner ## eigenen. Antwortoptionen laufen über die fokus-navigierbare Auswahl ## (dialogue_choices.tscn). Options-Tags verschieben die ## Klinge-↔-Herz-Achse (blade_heart) und Beziehungswerte (relationship: ## NPC-ID -> Delta). dialogue_started/dialogue_finished erlauben es, ## Spielerbewegung und UI während des Dialogs zu sperren. signal dialogue_started(dialogue_id: String) signal dialogue_finished(dialogue_id: String) const CHOICES_SCENE := preload("res://scenes/ui/dialogue_choices.tscn") const PLAYER_SPEAKER_ID := "player" var is_dialogue_active := false var _speakers: Dictionary = {} var _choices: CanvasLayer ## Startet einen Dialog aus einer JSON-Datei. speakers bildet Sprecher-IDs ## aus den Dialog-Knoten auf Szenen-Nodes mit SpeechBubble-Kind ab; der ## Spieler läuft unter der ID "player". Läuft bereits ein Dialog, passiert ## nichts. func start_dialogue(dialogue_path: String, speakers: Dictionary) -> void: if is_dialogue_active: return var data := _load_dialogue(dialogue_path) if data.is_empty(): return _speakers = speakers is_dialogue_active = true dialogue_started.emit(str(data["id"])) _run_dialogue(data) ## Liest und validiert eine Dialog-Datei; leeres Dictionary bei Fehlern. func _load_dialogue(dialogue_path: String) -> Dictionary: var file := FileAccess.open(dialogue_path, FileAccess.READ) if file == null: push_error("Dialog-Datei nicht lesbar: %s (%s)" % [dialogue_path, error_string(FileAccess.get_open_error())]) return {} var parsed: Variant = JSON.parse_string(file.get_as_text()) file.close() if not (parsed is Dictionary) or not parsed.has("id") or not parsed.has("start") or not (parsed.get("nodes") is Dictionary): push_error("Dialog-Datei ist kein gültiger Dialogbaum: %s" % dialogue_path) return {} return parsed ## Spielt den Dialogbaum als Coroutine ab: Zeilen über die Sprechblasen, ## Entscheidungen über die Antwortauswahl, Effekte über die Options-Tags. func _run_dialogue(data: Dictionary) -> void: var nodes: Dictionary = data["nodes"] var node_id := str(data["start"]) while node_id != "" and nodes.has(node_id): var node: Dictionary = nodes[node_id] var bubble := _bubble_for(str(node.get("speaker", ""))) var options: Array = node.get("options", []) if options.is_empty(): node_id = await _play_line(bubble, node) else: node_id = await _play_choice(bubble, node, options) _end_dialogue(str(data["id"])) ## Zeigt eine Zeile ohne Optionen und liefert die ID des Folge-Knotens. func _play_line(bubble: Node, node: Dictionary) -> String: if bubble != null: bubble.show_line(str(node.get("text", ""))) await bubble.line_finished return _next_id(node) ## Zeigt eine Zeile mit Antwortoptionen, wendet die Tags der gewählten ## Option an, spielt die Antwort über der Spieler-Sprechblase und liefert ## die ID des Folge-Knotens. func _play_choice(bubble: Node, node: Dictionary, options: Array) -> String: if bubble != null: bubble.show_line(str(node.get("text", "")), true) var option_texts: Array[String] = [] for option in options: option_texts.append(str(option.get("text", ""))) var index: int = await _get_choices().choose(option_texts) if bubble != null: bubble.hide_bubble() var option: Dictionary = options[clampi(index, 0, options.size() - 1)] _apply_option_effects(option) var player_bubble := _bubble_for(PLAYER_SPEAKER_ID) if player_bubble != null: player_bubble.show_line(str(option.get("text", ""))) await player_bubble.line_finished return _next_id(option) ## Wendet die Effekt-Tags einer Option an: blade_heart verschiebt die ## Klinge-↔-Herz-Achse, relationship verschiebt Beziehungswerte pro NPC. func _apply_option_effects(option: Dictionary) -> void: var tone_shift := int(option.get("blade_heart", 0)) if tone_shift != 0: GameState.shift_blade_heart_tone(tone_shift) var relationship_shifts: Dictionary = option.get("relationship", {}) for npc_id in relationship_shifts: RelationshipSystem.adjust_relationship(str(npc_id), int(relationship_shifts[npc_id])) ## Beendet den Dialog und gibt die Sprecher-Referenzen frei. func _end_dialogue(dialogue_id: String) -> void: is_dialogue_active = false _speakers.clear() dialogue_finished.emit(dialogue_id) ## Liefert die Sprechblase (Kind "SpeechBubble") eines Sprechers, null mit ## Warnung, wenn Sprecher oder Blase fehlen. func _bubble_for(speaker_id: String) -> Node: var speaker: Node = _speakers.get(speaker_id) if speaker == null: push_warning("Kein Sprecher-Node für ID '%s' registriert." % speaker_id) return null var bubble := speaker.get_node_or_null("SpeechBubble") if bubble == null: push_warning("Sprecher '%s' hat keine SpeechBubble." % speaker_id) return bubble ## Liefert die ID des Folge-Knotens aus einem Knoten oder einer Option; ## leerer String beendet den Dialog (auch bei JSON-null). func _next_id(source: Dictionary) -> String: var next: Variant = source.get("next") return next if next is String else "" ## Instanziiert die Antwortauswahl beim ersten Bedarf. func _get_choices() -> CanvasLayer: if _choices == null: _choices = CHOICES_SCENE.instantiate() add_child(_choices) return _choices