diff --git a/data/dialogue/ambient_lines.json b/data/dialogue/ambient_lines.json new file mode 100644 index 0000000..c854a85 --- /dev/null +++ b/data/dialogue/ambient_lines.json @@ -0,0 +1,46 @@ +{ + "exchanges": [ + { + "npcs": ["imogen", "violet"], + "lines": [ + { "speaker": "imogen", "text": "Sorrengail. Du liest im Gehen. Irgendwann läufst du in eine Lanze." }, + { "speaker": "violet", "text": "Dann weiß ich wenigstens, welche Legierung. Das nennt man Recherche." } + ] + }, + { + "npcs": ["imogen", "violet"], + "lines": [ + { "speaker": "violet", "text": "Deine Messer hängen heute anders als gestern. Neues System?" }, + { "speaker": "imogen", "text": "Wer fragt, kriegt die Antwort vorgeführt. Willst du fragen?" } + ] + }, + { + "npcs": ["rhiannon", "wren"], + "lines": [ + { "speaker": "rhiannon", "text": "Wren. Hast du heute gegessen? Und Frühstück zählt nur, wenn es nach etwas schmeckt." }, + { "speaker": "wren", "text": "Ich— ja? Das Graue mit den Klumpen. Ich glaube, es war mal Hafer." } + ] + }, + { + "npcs": ["rhiannon", "wren"], + "lines": [ + { "speaker": "wren", "text": "Wie schaffst du das, vor allen zu reden, ohne dass die Stimme wegkippt?" }, + { "speaker": "rhiannon", "text": "Übung. Und ich such mir ein Gesicht, das nickt. Nimm meins — ich nicke immer." } + ] + }, + { + "npcs": ["violet", "wren"], + "lines": [ + { "speaker": "wren", "text": "Stimmt es, dass die Drachen einen riechen können? Also... die Angst?" }, + { "speaker": "violet", "text": "Sie riechen alles. Aber Angst heißt nur, dass du begriffen hast, wo du bist. Das ist ein Anfang." } + ] + }, + { + "npcs": ["imogen", "rhiannon"], + "lines": [ + { "speaker": "rhiannon", "text": "Tauschst du morgen die Rotation mit mir? Ich hab da ein Versprechen stehen." }, + { "speaker": "imogen", "text": "Wenn du mir den Platz an der Wand freihältst. Ich verhandle hart." } + ] + } + ] +} diff --git a/scenes/hub/rider_quadrant.tscn b/scenes/hub/rider_quadrant.tscn index 7a525bf..ef63c22 100644 --- a/scenes/hub/rider_quadrant.tscn +++ b/scenes/hub/rider_quadrant.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=33 format=3] +[gd_scene load_steps=34 format=3] [ext_resource type="Script" path="res://scripts/gameplay/hub_room.gd" id="1_room"] [ext_resource type="PackedScene" path="res://scenes/player.tscn" id="2_player"] @@ -6,6 +6,7 @@ [ext_resource type="Script" path="res://scripts/gameplay/npc_dialogue.gd" id="4_imogen"] [ext_resource type="Script" path="res://scripts/gameplay/zone_trigger.gd" id="5_zone"] [ext_resource type="PackedScene" path="res://scenes/ui/speech_bubble.tscn" id="6_bubble"] +[ext_resource type="Script" path="res://scripts/gameplay/ambient_chatter.gd" id="7_chatter"] [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_sky"] sky_horizon_color = Color(0.64625, 0.65575, 0.67075, 1) @@ -180,10 +181,12 @@ shape = SubResource("BoxShape3D_cube_small") [node name="MeshInstance3D" type="MeshInstance3D" parent="ClimbBoxes/BoxB"] mesh = SubResource("BoxMesh_cube_small") -[node name="Imogen" type="StaticBody3D" parent="."] +[node name="Imogen" type="CharacterBody3D" parent="." groups=["npc"]] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 4, 0, 4) +collision_mask = 3 script = ExtResource("4_imogen") dialogue_path = "res://data/dialogue/imogen_intro.json" +waypoint_paths = Array[NodePath]([NodePath("../NpcWaypoints/ImogenA"), NodePath("../NpcWaypoints/ImogenB"), NodePath("../NpcWaypoints/ImogenC")]) [node name="CollisionShape3D" type="CollisionShape3D" parent="Imogen"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0) @@ -220,11 +223,13 @@ shape = SubResource("SphereShape3D_interact") [node name="SpeechBubble" parent="Imogen" instance=ExtResource("6_bubble")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.8, 0) -[node name="Violet" type="StaticBody3D" parent="."] +[node name="Violet" type="CharacterBody3D" parent="." groups=["npc"]] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5, 0, 7) +collision_mask = 3 script = ExtResource("4_imogen") npc_id = "violet" dialogue_path = "res://data/dialogue/violet_intro.json" +waypoint_paths = Array[NodePath]([NodePath("../NpcWaypoints/VioletA"), NodePath("../NpcWaypoints/VioletB"), NodePath("../NpcWaypoints/VioletC")]) [node name="CollisionShape3D" type="CollisionShape3D" parent="Violet"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0) @@ -261,11 +266,13 @@ shape = SubResource("SphereShape3D_interact") [node name="SpeechBubble" parent="Violet" instance=ExtResource("6_bubble")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.8, 0) -[node name="Rhiannon" type="StaticBody3D" parent="."] +[node name="Rhiannon" type="CharacterBody3D" parent="." groups=["npc"]] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 9, 0, 8) +collision_mask = 3 script = ExtResource("4_imogen") npc_id = "rhiannon" dialogue_path = "res://data/dialogue/rhiannon_intro.json" +waypoint_paths = Array[NodePath]([NodePath("../NpcWaypoints/RhiannonA"), NodePath("../NpcWaypoints/RhiannonB"), NodePath("../NpcWaypoints/RhiannonC")]) [node name="CollisionShape3D" type="CollisionShape3D" parent="Rhiannon"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0) @@ -302,11 +309,13 @@ shape = SubResource("SphereShape3D_interact") [node name="SpeechBubble" parent="Rhiannon" instance=ExtResource("6_bubble")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.8, 0) -[node name="Wren" type="StaticBody3D" parent="."] +[node name="Wren" type="CharacterBody3D" parent="." groups=["npc"]] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2) +collision_mask = 3 script = ExtResource("4_imogen") npc_id = "wren" dialogue_path = "res://data/dialogue/wren_intro.json" +waypoint_paths = Array[NodePath]([NodePath("../NpcWaypoints/WrenA"), NodePath("../NpcWaypoints/WrenB"), NodePath("../NpcWaypoints/WrenC")]) [node name="CollisionShape3D" type="CollisionShape3D" parent="Wren"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0) @@ -395,6 +404,47 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -8, 0.1, -16) [node name="FromClassroom" type="Marker3D" parent="SpawnPoints"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8, 0.1, -16) +[node name="NpcWaypoints" type="Node3D" parent="."] + +[node name="ImogenA" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 4, 0, 4) + +[node name="ImogenB" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 0, 10) + +[node name="ImogenC" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 10, 0, 2) + +[node name="VioletA" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5, 0, 7) + +[node name="VioletB" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 0) + +[node name="VioletC" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 12) + +[node name="RhiannonA" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 9, 0, 8) + +[node name="RhiannonB" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 14, 0, -2) + +[node name="RhiannonC" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 0, 14) + +[node name="WrenA" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2) + +[node name="WrenB" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -8, 0, -8) + +[node name="WrenC" type="Marker3D" parent="NpcWaypoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 6, 0, -10) + +[node name="AmbientChatter" type="Node" parent="."] +script = ExtResource("7_chatter") + [node name="Player" parent="." instance=ExtResource("2_player")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 8) diff --git a/scripts/autoloads/dialogue_system.gd b/scripts/autoloads/dialogue_system.gd index a5d3b14..8a31d44 100644 --- a/scripts/autoloads/dialogue_system.gd +++ b/scripts/autoloads/dialogue_system.gd @@ -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) diff --git a/scripts/gameplay/ambient_chatter.gd b/scripts/gameplay/ambient_chatter.gd new file mode 100644 index 0000000..ac57ac4 --- /dev/null +++ b/scripts/gameplay/ambient_chatter.gd @@ -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())] diff --git a/scripts/gameplay/ambient_chatter.gd.uid b/scripts/gameplay/ambient_chatter.gd.uid new file mode 100644 index 0000000..d22ffd8 --- /dev/null +++ b/scripts/gameplay/ambient_chatter.gd.uid @@ -0,0 +1 @@ +uid://bag10xgmx3qa diff --git a/scripts/gameplay/npc_dialogue.gd b/scripts/gameplay/npc_dialogue.gd index 22fa105..2523d89 100644 --- a/scripts/gameplay/npc_dialogue.gd +++ b/scripts/gameplay/npc_dialogue.gd @@ -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() diff --git a/scripts/ui/debug_overlay.gd b/scripts/ui/debug_overlay.gd index fd5e56a..713437a 100644 --- a/scripts/ui/debug_overlay.gd +++ b/scripts/ui/debug_overlay.gd @@ -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)