82 lines
1.9 KiB
C#
82 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
public class NPCDialogue : MonoBehaviour
|
|
{
|
|
public string npcId = "VillageElder";
|
|
public DialogueData dialogueData;
|
|
public float interactionRange = 3f;
|
|
public KeyCode interactionKey = KeyCode.E;
|
|
|
|
private Transform player;
|
|
private bool promptVisible;
|
|
|
|
private void Start()
|
|
{
|
|
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
|
|
if (playerObject != null)
|
|
{
|
|
player = playerObject.transform;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (dialogueData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (player == null)
|
|
{
|
|
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
|
|
player = playerObject != null ? playerObject.transform : null;
|
|
}
|
|
|
|
bool inRange = player != null && Vector3.Distance(player.position, transform.position) <= interactionRange;
|
|
bool dialogueOpen = DialogueUI.Instance != null && DialogueUI.Instance.IsOpen;
|
|
|
|
if (!inRange || dialogueOpen)
|
|
{
|
|
HidePrompt();
|
|
return;
|
|
}
|
|
|
|
ShowPrompt();
|
|
|
|
if (Input.GetKeyDown(interactionKey) && DialogueUI.Instance != null)
|
|
{
|
|
HidePrompt();
|
|
QuestManager.Instance?.OnNpcTalked(npcId);
|
|
DialogueUI.Instance.Open(this, dialogueData);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
HidePrompt();
|
|
}
|
|
|
|
private void ShowPrompt()
|
|
{
|
|
if (promptVisible)
|
|
{
|
|
return;
|
|
}
|
|
|
|
promptVisible = true;
|
|
string npcName = dialogueData != null ? dialogueData.npcName : "NPC";
|
|
InteractionPromptUI.Instance?.Show($"E Поговорить: {npcName}");
|
|
}
|
|
|
|
private void HidePrompt()
|
|
{
|
|
if (!promptVisible)
|
|
{
|
|
return;
|
|
}
|
|
|
|
promptVisible = false;
|
|
InteractionPromptUI.Instance?.Hide();
|
|
}
|
|
}
|