Phase 2c: NPC-Wander-KI + Ambient-Gespraeche
- npc_dialogue.gd: NPCs sind jetzt CharacterBody3D mit Zustandsmaschine (IDLE/WALKING/PLAYER_DIALOGUE/AMBIENT); Wandern zwischen Marker3D-Wegpunkten mit zufaelligen Wartezeiten, Kollisionsmaske inkl. Spieler; Wander-KI pausiert, solange der NPC Sprecher des Spieler-Dialogs ist, und wendet sich dem Spieler zu; Interaktions- Trigger haengt am Root und wandert mit - DialogueSystem.is_speaker() fuer KI-Pause und Ambient-Abbruch - AmbientChatter (Szenen-Manager) + data/dialogue/ambient_lines.json: sechs kanon-neutrale Kurz-Wechsel (Imogen/Violet, Rhiannon/Wren, Violet/Wren, Imogen/Rhiannon); Start bei Naehe mit Wahrscheinlichkeit und Pair-Cooldown; Wiedergabe ueber Sprechblasen (Chatbox bleibt dem Spieler-Dialog vorbehalten, Ambient ist Welt-Ambiente ohne Auswahl); keine Werte-Effekte, keine Spieler-Sperre; Spieler-Interaktion mit beteiligtem NPC bricht den Austausch sofort sauber ab - Debug-Overlay zeigt die NPC-Zustaende - Headless verifiziert (19 Checks): Wandern, Dialog-Pause mitten in der Bewegung, forcierter Ambient-Austausch, Werte-Neutralitaet, keine Spieler-Sperre, sauberer Ambient-Abbruch bei Interaktion Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
d1e07cc06f
commit
f0cd59c1e3
7 changed files with 415 additions and 19 deletions
|
|
@ -76,6 +76,12 @@ func _unhandled_input(event: InputEvent) -> void:
|
|||
_is_abort_requested = true
|
||||
|
||||
|
||||
## Gibt zurück, ob der Node Sprecher des gerade laufenden Dialogs ist
|
||||
## (z. B. für Wander-KI-Pause und Ambient-Abbruch).
|
||||
func is_speaker(node: Node) -> bool:
|
||||
return is_dialogue_active and _speakers.values().has(node)
|
||||
|
||||
|
||||
## 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)
|
||||
|
|
|
|||
170
scripts/gameplay/ambient_chatter.gd
Normal file
170
scripts/gameplay/ambient_chatter.gd
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
extends Node
|
||||
## Ambient-Gespräche zwischen NPCs (Phase 2c), Szenen-lokaler Manager.
|
||||
##
|
||||
## Prüft periodisch alle NPC-Paare (Gruppe "npc"): Sind sich zwei
|
||||
## verfügbare NPCs nahe genug und existiert für das Paar ein Austausch in
|
||||
## data/dialogue/ambient_lines.json, startet er mit exchange_chance einen
|
||||
## zufällig gewählten kurzen Wortwechsel — über die Sprechblasen, nicht
|
||||
## über die Chatbox: Die Box ist das Spieler-Dialog-UI mit
|
||||
## Auswahl-Semantik und würde Spieler-Beteiligung suggerieren;
|
||||
## Ambient-Zeilen sind Welt-Ambiente ohne Auswahl und gehören räumlich zu
|
||||
## den Figuren. Ambient-Gespräche verändern keine Werte und sperren den
|
||||
## Spieler nicht. Spricht der Spieler einen beteiligten NPC an, bricht
|
||||
## der Austausch sofort ab; NPCs im Spieler-Dialog werden nie
|
||||
## Ambient-Partner. Pro Paar gilt ein Cooldown.
|
||||
|
||||
const LINES_PATH := "res://data/dialogue/ambient_lines.json"
|
||||
const CHECK_INTERVAL := 3.0
|
||||
const PROXIMITY := 4.0
|
||||
const PAIR_COOLDOWN_MS := 25000
|
||||
|
||||
@export var exchange_chance := 0.35
|
||||
|
||||
var _exchanges: Array = []
|
||||
var _cooldowns: Dictionary = {}
|
||||
var _check_remaining := CHECK_INTERVAL
|
||||
var _active_npcs: Array = []
|
||||
var _is_exchange_running := false
|
||||
var _is_exchange_aborted := false
|
||||
|
||||
|
||||
## Lädt die Austausch-Daten und lauscht auf Spieler-Dialog-Starts.
|
||||
func _ready() -> void:
|
||||
_exchanges = _load_exchanges()
|
||||
DialogueSystem.dialogue_started.connect(_on_player_dialogue_started)
|
||||
|
||||
|
||||
## Prüft im Intervall, ob ein Ambient-Austausch starten kann.
|
||||
func _process(delta: float) -> void:
|
||||
if _is_exchange_running or _exchanges.is_empty():
|
||||
return
|
||||
_check_remaining -= delta
|
||||
if _check_remaining > 0.0:
|
||||
return
|
||||
_check_remaining = CHECK_INTERVAL
|
||||
_try_start_exchange()
|
||||
|
||||
|
||||
## Liest und validiert die Ambient-Datei; leeres Array bei Fehlern.
|
||||
func _load_exchanges() -> Array:
|
||||
var file := FileAccess.open(LINES_PATH, FileAccess.READ)
|
||||
if file == null:
|
||||
push_error("Ambient-Datei nicht lesbar: %s (%s)" % [LINES_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.get("exchanges") is Array):
|
||||
push_error("Ambient-Datei ist keine gültige Austausch-Liste: %s" % LINES_PATH)
|
||||
return []
|
||||
return parsed["exchanges"]
|
||||
|
||||
|
||||
## Sucht das erste verfügbare, nahe NPC-Paar mit passendem Austausch und
|
||||
## würfelt den Start über exchange_chance aus.
|
||||
func _try_start_exchange() -> void:
|
||||
var npcs := get_tree().get_nodes_in_group("npc")
|
||||
for i in npcs.size():
|
||||
for j in range(i + 1, npcs.size()):
|
||||
var npc_a: Node3D = npcs[i]
|
||||
var npc_b: Node3D = npcs[j]
|
||||
if not (npc_a.can_start_ambient() and npc_b.can_start_ambient()):
|
||||
continue
|
||||
if npc_a.global_position.distance_to(npc_b.global_position) > PROXIMITY:
|
||||
continue
|
||||
if _is_pair_on_cooldown(npc_a.npc_id, npc_b.npc_id):
|
||||
continue
|
||||
var exchange := _pick_exchange(npc_a.npc_id, npc_b.npc_id)
|
||||
if exchange.is_empty():
|
||||
continue
|
||||
if randf() > exchange_chance:
|
||||
continue
|
||||
_run_exchange(npc_a, npc_b, exchange)
|
||||
return
|
||||
|
||||
|
||||
## Wählt zufällig einen Austausch für das Paar (Reihenfolge egal),
|
||||
## leeres Dictionary wenn keiner existiert.
|
||||
func _pick_exchange(id_a: String, id_b: String) -> Dictionary:
|
||||
var candidates: Array = []
|
||||
for exchange in _exchanges:
|
||||
var pair: Array = exchange.get("npcs", [])
|
||||
if pair.size() != 2:
|
||||
continue
|
||||
if (pair[0] == id_a and pair[1] == id_b) or (pair[0] == id_b and pair[1] == id_a):
|
||||
candidates.append(exchange)
|
||||
if candidates.is_empty():
|
||||
return {}
|
||||
return candidates.pick_random()
|
||||
|
||||
|
||||
## Spielt einen Austausch als Coroutine über die Sprechblasen der beiden
|
||||
## NPCs ab; ein Abbruch (Spieler-Interaktion) beendet ihn sofort sauber.
|
||||
func _run_exchange(npc_a: Node3D, npc_b: Node3D, exchange: Dictionary) -> void:
|
||||
_is_exchange_running = true
|
||||
_is_exchange_aborted = false
|
||||
_active_npcs = [npc_a, npc_b]
|
||||
npc_a.begin_ambient(npc_b.global_position)
|
||||
npc_b.begin_ambient(npc_a.global_position)
|
||||
var by_id := { npc_a.npc_id: npc_a, npc_b.npc_id: npc_b }
|
||||
for line in exchange.get("lines", []):
|
||||
if _is_exchange_aborted:
|
||||
break
|
||||
var speaker: Node3D = by_id.get(str(line.get("speaker", "")))
|
||||
if speaker == null:
|
||||
continue
|
||||
var bubble: Node = speaker.get_node_or_null("SpeechBubble")
|
||||
if bubble == null:
|
||||
continue
|
||||
bubble.show_line(str(line.get("text", "")))
|
||||
await _wait_for_bubble(bubble)
|
||||
for npc in _active_npcs:
|
||||
npc.end_ambient()
|
||||
_set_pair_cooldown(npc_a.npc_id, npc_b.npc_id)
|
||||
_active_npcs = []
|
||||
_is_exchange_running = false
|
||||
|
||||
|
||||
## Wartet das Zeilenende einer Sprechblase ab, bricht bei Abbruch früh ab.
|
||||
func _wait_for_bubble(bubble: Node) -> void:
|
||||
while bubble.visible and not _is_exchange_aborted:
|
||||
await get_tree().process_frame
|
||||
if _is_exchange_aborted:
|
||||
bubble.hide_bubble()
|
||||
|
||||
|
||||
## Bricht den laufenden Austausch ab, wenn der Spieler einen der
|
||||
## beteiligten NPCs anspricht.
|
||||
func _on_player_dialogue_started(_dialogue_id: String) -> void:
|
||||
if not _is_exchange_running:
|
||||
return
|
||||
for npc in _active_npcs:
|
||||
if DialogueSystem.is_speaker(npc):
|
||||
_abort_exchange()
|
||||
return
|
||||
|
||||
|
||||
## Blendet die Blasen aus und entlässt die NPCs aus dem Ambient-Zustand;
|
||||
## der NPC im Spieler-Dialog bleibt davon unberührt.
|
||||
func _abort_exchange() -> void:
|
||||
_is_exchange_aborted = true
|
||||
for npc in _active_npcs:
|
||||
var bubble: Node = npc.get_node_or_null("SpeechBubble")
|
||||
if bubble != null:
|
||||
bubble.hide_bubble()
|
||||
npc.end_ambient()
|
||||
|
||||
|
||||
## Gibt zurück, ob das Paar noch im Cooldown steckt.
|
||||
func _is_pair_on_cooldown(id_a: String, id_b: String) -> bool:
|
||||
var key := _pair_key(id_a, id_b)
|
||||
return _cooldowns.has(key) and Time.get_ticks_msec() - _cooldowns[key] < PAIR_COOLDOWN_MS
|
||||
|
||||
|
||||
## Startet den Cooldown für das Paar.
|
||||
func _set_pair_cooldown(id_a: String, id_b: String) -> void:
|
||||
_cooldowns[_pair_key(id_a, id_b)] = Time.get_ticks_msec()
|
||||
|
||||
|
||||
## Liefert einen reihenfolge-unabhängigen Schlüssel für ein NPC-Paar.
|
||||
func _pair_key(id_a: String, id_b: String) -> String:
|
||||
return "%s|%s" % [mini(id_a.hash(), id_b.hash()), maxi(id_a.hash(), id_b.hash())]
|
||||
1
scripts/gameplay/ambient_chatter.gd.uid
Normal file
1
scripts/gameplay/ambient_chatter.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bag10xgmx3qa
|
||||
|
|
@ -1,32 +1,68 @@
|
|||
extends StaticBody3D
|
||||
## Ansprechbarer NPC mit daten-getriebenem Dialog (Phase 2a).
|
||||
extends CharacterBody3D
|
||||
## Ansprechbarer, wandernder NPC (Phasen 2a–2c).
|
||||
##
|
||||
## Zeigt in Reichweite (Area3D-Trigger) einen Interaktions-Prompt und
|
||||
## startet bei der interact-Action den Dialogbaum aus dialogue_path über
|
||||
## das DialogueSystem. Während eines laufenden Dialogs ist keine erneute
|
||||
## Interaktion möglich und der Prompt bleibt ausgeblendet — ein doppeltes
|
||||
## Öffnen wird sowohl hier als auch im DialogueSystem verhindert. NPC-IDs
|
||||
## sind snake_case-Strings, z. B. "imogen".
|
||||
## Dialog: Zeigt in Reichweite (Area3D-Trigger am NPC-Root, wandert mit)
|
||||
## einen Interaktions-Prompt und startet bei der interact-Action den
|
||||
## Dialogbaum aus dialogue_path über das DialogueSystem; doppeltes Öffnen
|
||||
## wird hier und im DialogueSystem verhindert. Wander-KI (Phase 2c):
|
||||
## Der NPC pendelt zwischen den Marker3D-Wegpunkten aus waypoint_paths
|
||||
## mit zufälligen Wartezeiten. Zustände: IDLE (warten), WALKING (zum
|
||||
## Wegpunkt), PLAYER_DIALOGUE (KI pausiert, solange dieser NPC Sprecher
|
||||
## des Spieler-Dialogs ist), AMBIENT (kurzer NPC-zu-NPC-Austausch,
|
||||
## gesteuert vom AmbientChatter der Szene). NPC-IDs sind
|
||||
## snake_case-Strings, z. B. "imogen".
|
||||
|
||||
enum NpcState { IDLE, WALKING, PLAYER_DIALOGUE, AMBIENT }
|
||||
|
||||
const ARRIVAL_DISTANCE := 0.35
|
||||
const WAIT_TIME_MIN := 2.5
|
||||
const WAIT_TIME_MAX := 6.0
|
||||
const MESH_TURN_SPEED := 8.0
|
||||
|
||||
@export var npc_id := "imogen"
|
||||
@export_file("*.json") var dialogue_path: String
|
||||
@export var waypoint_paths: Array[NodePath] = []
|
||||
@export var walk_speed := 1.6
|
||||
|
||||
var _state := NpcState.IDLE
|
||||
var _is_player_in_range := false
|
||||
var _waypoints: Array[Vector3] = []
|
||||
var _current_waypoint := 0
|
||||
var _wait_remaining := 0.0
|
||||
var _gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
|
||||
@onready var _prompt_label: Label3D = $PromptLabel
|
||||
@onready var _interaction_area: Area3D = $InteractionArea
|
||||
@onready var _mesh: MeshInstance3D = $MeshInstance3D
|
||||
|
||||
|
||||
## Verbindet Reichweiten- und Dialog-Signale.
|
||||
## Verbindet Reichweiten- und Dialog-Signale und löst die Wegpunkte auf.
|
||||
func _ready() -> void:
|
||||
_interaction_area.body_entered.connect(_on_body_entered)
|
||||
_interaction_area.body_exited.connect(_on_body_exited)
|
||||
DialogueSystem.dialogue_started.connect(_on_dialogue_state_changed)
|
||||
DialogueSystem.dialogue_finished.connect(_on_dialogue_state_changed)
|
||||
DialogueSystem.dialogue_started.connect(_on_dialogue_started)
|
||||
DialogueSystem.dialogue_finished.connect(_on_dialogue_finished)
|
||||
for path in waypoint_paths:
|
||||
var marker := get_node_or_null(path) as Node3D
|
||||
if marker != null:
|
||||
_waypoints.append(marker.global_position)
|
||||
_wait_remaining = randf_range(WAIT_TIME_MIN, WAIT_TIME_MAX)
|
||||
|
||||
|
||||
## Treibt die Wander-KI an; in Dialog- und Ambient-Zuständen steht der NPC.
|
||||
func _physics_process(delta: float) -> void:
|
||||
match _state:
|
||||
NpcState.IDLE:
|
||||
_process_idle(delta)
|
||||
NpcState.WALKING:
|
||||
_process_walking(delta)
|
||||
_:
|
||||
velocity = Vector3.ZERO
|
||||
|
||||
|
||||
## Startet den Dialog, wenn der Spieler in Reichweite ist, interact drückt
|
||||
## und noch kein Dialog läuft.
|
||||
## und noch kein Dialog läuft (auch mitten in der Bewegung oder während
|
||||
## eines Ambient-Austauschs).
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if not _is_player_in_range:
|
||||
return
|
||||
|
|
@ -37,6 +73,63 @@ func _unhandled_input(event: InputEvent) -> void:
|
|||
_start_dialogue()
|
||||
|
||||
|
||||
## Meldet, ob der NPC für ein Ambient-Gespräch verfügbar ist (wandert oder
|
||||
## wartet, ist also weder im Spieler-Dialog noch schon im Austausch).
|
||||
func can_start_ambient() -> bool:
|
||||
return _state == NpcState.IDLE or _state == NpcState.WALKING
|
||||
|
||||
|
||||
## Versetzt den NPC in den Ambient-Zustand und dreht ihn zum Partner.
|
||||
func begin_ambient(partner_position: Vector3) -> void:
|
||||
_state = NpcState.AMBIENT
|
||||
velocity = Vector3.ZERO
|
||||
_face_towards(partner_position)
|
||||
|
||||
|
||||
## Beendet den Ambient-Zustand und lässt den NPC nach kurzer Wartezeit
|
||||
## weiterwandern; in anderen Zuständen passiert nichts.
|
||||
func end_ambient() -> void:
|
||||
if _state == NpcState.AMBIENT:
|
||||
_state = NpcState.IDLE
|
||||
_wait_remaining = randf_range(WAIT_TIME_MIN, WAIT_TIME_MAX)
|
||||
|
||||
|
||||
## Liefert den Zustandsnamen für das Debug-Overlay.
|
||||
func get_state_name() -> String:
|
||||
return NpcState.keys()[_state]
|
||||
|
||||
|
||||
## Zählt die Wartezeit herunter und peilt dann den nächsten Wegpunkt an.
|
||||
func _process_idle(delta: float) -> void:
|
||||
if _waypoints.size() < 2:
|
||||
return
|
||||
_wait_remaining -= delta
|
||||
if _wait_remaining <= 0.0:
|
||||
_current_waypoint = (_current_waypoint + 1) % _waypoints.size()
|
||||
_state = NpcState.WALKING
|
||||
|
||||
|
||||
## Geht zum aktuellen Wegpunkt und wechselt bei Ankunft in die Wartezeit.
|
||||
func _process_walking(delta: float) -> void:
|
||||
var to_target := _waypoints[_current_waypoint] - global_position
|
||||
to_target.y = 0.0
|
||||
if to_target.length() <= ARRIVAL_DISTANCE:
|
||||
velocity = Vector3.ZERO
|
||||
_state = NpcState.IDLE
|
||||
_wait_remaining = randf_range(WAIT_TIME_MIN, WAIT_TIME_MAX)
|
||||
return
|
||||
var direction := to_target.normalized()
|
||||
velocity.x = direction.x * walk_speed
|
||||
velocity.z = direction.z * walk_speed
|
||||
if not is_on_floor():
|
||||
velocity.y -= _gravity * delta
|
||||
else:
|
||||
velocity.y = 0.0
|
||||
move_and_slide()
|
||||
var target_yaw := atan2(-direction.x, -direction.z)
|
||||
_mesh.rotation.y = lerp_angle(_mesh.rotation.y, target_yaw, minf(MESH_TURN_SPEED * delta, 1.0))
|
||||
|
||||
|
||||
## Übergibt sich selbst und den Spieler als Sprecher an das DialogueSystem.
|
||||
func _start_dialogue() -> void:
|
||||
var player := get_tree().get_first_node_in_group("player")
|
||||
|
|
@ -49,6 +142,14 @@ func _start_dialogue() -> void:
|
|||
})
|
||||
|
||||
|
||||
## Dreht das Kapsel-Mesh zu einer Zielposition.
|
||||
func _face_towards(target: Vector3) -> void:
|
||||
var direction := target - global_position
|
||||
direction.y = 0.0
|
||||
if direction.length_squared() > 0.01:
|
||||
_mesh.rotation.y = atan2(-direction.x, -direction.z)
|
||||
|
||||
|
||||
## Blendet den Prompt ein, sobald der Spieler die Reichweite betritt.
|
||||
func _on_body_entered(_body: Node3D) -> void:
|
||||
_is_player_in_range = true
|
||||
|
|
@ -61,8 +162,24 @@ func _on_body_exited(_body: Node3D) -> void:
|
|||
_update_prompt()
|
||||
|
||||
|
||||
## Hält den Prompt während laufender Dialoge ausgeblendet.
|
||||
func _on_dialogue_state_changed(_dialogue_id: String) -> void:
|
||||
## Pausiert die Wander-KI und wendet sich dem Spieler zu, wenn dieser NPC
|
||||
## Sprecher des gestarteten Dialogs ist; hält außerdem den Prompt aktuell.
|
||||
func _on_dialogue_started(_dialogue_id: String) -> void:
|
||||
if DialogueSystem.is_speaker(self):
|
||||
_state = NpcState.PLAYER_DIALOGUE
|
||||
velocity = Vector3.ZERO
|
||||
var player := get_tree().get_first_node_in_group("player") as Node3D
|
||||
if player != null:
|
||||
_face_towards(player.global_position)
|
||||
_update_prompt()
|
||||
|
||||
|
||||
## Lässt die Wander-KI nach dem Dialog weiterlaufen und aktualisiert den
|
||||
## Prompt.
|
||||
func _on_dialogue_finished(_dialogue_id: String) -> void:
|
||||
if _state == NpcState.PLAYER_DIALOGUE:
|
||||
_state = NpcState.IDLE
|
||||
_wait_remaining = randf_range(WAIT_TIME_MIN, WAIT_TIME_MAX)
|
||||
_update_prompt()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -76,6 +76,12 @@ func _process(_delta: float) -> void:
|
|||
for npc_id in TRACKED_NPC_IDS:
|
||||
relationship_parts.append("%s %+d" % [npc_id.capitalize(), RelationshipSystem.get_relationship(npc_id)])
|
||||
lines.append("Beziehungen: %s" % " ".join(relationship_parts))
|
||||
var npc_states := PackedStringArray()
|
||||
for npc in get_tree().get_nodes_in_group("npc"):
|
||||
if npc.has_method("get_state_name"):
|
||||
npc_states.append("%s: %s" % [str(npc.npc_id).capitalize(), npc.get_state_name()])
|
||||
if not npc_states.is_empty():
|
||||
lines.append("NPCs: %s" % " ".join(npc_states))
|
||||
lines.append("Permadeath: %s" % ("AN" if GameState.is_permadeath_enabled else "AUS"))
|
||||
_info_label.text = "\n".join(lines)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue