first commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(menuName = "Lab05/Dialogue Data", fileName = "DialogueData")]
|
||||
public class DialogueData : ScriptableObject
|
||||
{
|
||||
public string npcName;
|
||||
[TextArea(2, 5)] public string greeting;
|
||||
public List<DialogueResponse> responses = new List<DialogueResponse>();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class DialogueResponse
|
||||
{
|
||||
public string text;
|
||||
public QuestData questToStart;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 18a5c286e623a69a48c6546245b2b2c3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,81 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 20689d635fb38dec6a942f3c78ba6e6a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user