62 lines
2.5 KiB
GDScript
62 lines
2.5 KiB
GDScript
extends StaticBody3D
|
|
|
|
@export var dialogue_data: Array[Dictionary] = [
|
|
{"name": "Сталкер", "text": "Привет! Наконец-то ты подошел."},
|
|
{"name": "Игрок", "text": "Привет. Что за проблемы со скриптами?"},
|
|
{"name": "Сталкер", "text": "Да вот, теперь и подсказки работают как надо!"}
|
|
]
|
|
|
|
# Укажи здесь точный путь к .tscn файлу твоей сцены диалога
|
|
const DIALOGUE_UI_FILE = preload("res://addons/dialoge/dialogue_ui.tscn")
|
|
|
|
var is_player_inside: bool = false
|
|
var is_dialogue_active: bool = false
|
|
var ui_instance: Node = null
|
|
var detect_area: Area3D = null
|
|
|
|
func _ready() -> void:
|
|
detect_area = get_node_or_null("DetectArea")
|
|
if detect_area == null:
|
|
detect_area = find_child("DetectArea", true, false)
|
|
|
|
# Спавним диалог ОДИН РАЗ при старте карты, чтобы он не багался при переспавне
|
|
if ui_instance == null:
|
|
ui_instance = DIALOGUE_UI_FILE.instantiate()
|
|
get_tree().get_root().add_child.call_deferred(ui_instance)
|
|
if "layer" in ui_instance: ui_instance.layer = 135
|
|
|
|
func _process(_delta: float) -> void:
|
|
if detect_area == null: return
|
|
|
|
var overlapping_bodies = detect_area.get_overlapping_bodies()
|
|
var found_player_this_frame = false
|
|
|
|
for body in overlapping_bodies:
|
|
if "Player" in body.name or "Character" in body.name or body.find_child("WeaponManager", true, false) != null:
|
|
found_player_this_frame = true
|
|
break
|
|
|
|
# Игрок ЗАШЕЛ в зону NPC
|
|
if found_player_this_frame and not is_player_inside:
|
|
is_player_inside = true
|
|
if ui_instance and not is_dialogue_active:
|
|
if ui_instance.has_method("show_prompt"):
|
|
ui_instance.show_prompt()
|
|
|
|
# Игрок ВЫШЕЛ из зоны NPC
|
|
elif not found_player_this_frame and is_player_inside:
|
|
is_player_inside = false
|
|
is_dialogue_active = false
|
|
if ui_instance:
|
|
if ui_instance.has_method("hide_prompt"): ui_instance.hide_prompt()
|
|
if ui_instance.has_method("end_dialogue"): ui_instance.end_dialogue()
|
|
|
|
# Старт диалога по кнопке E
|
|
if is_player_inside and not is_dialogue_active:
|
|
if Input.is_key_pressed(KEY_E) or Input.is_action_just_pressed("interact"):
|
|
is_dialogue_active = true
|
|
if ui_instance:
|
|
if ui_instance.has_method("hide_prompt"): ui_instance.hide_prompt()
|
|
if ui_instance.has_method("start_dialogue"):
|
|
ui_instance.start_dialogue(dialogue_data)
|