Scriptum/Assets/NPCDialogue.cs

95 lines
1.7 KiB
C#
Raw Normal View History

2022-05-25 17:11:49 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NPCDialogue : MonoBehaviour
{
public Text nameText;
public Text dialogueText;
public Dialogue dialogue;
private Queue<string> sentences;
public GameObject Panel;
bool triggered = false;
void Start()
{
sentences = new Queue<string>();
}
public void TriggerDialogue()
{
StartDialogue(dialogue);
}
void OnTriggerExit2D(Collider2D collision)
{
if (Panel != null)
{
Panel.SetActive(false);
}
triggered = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
triggered = true;
}
}
void Update()
{
if (triggered)
{
if (Input.GetKeyDown(KeyCode.E))
{
if (Panel != null)
{
Panel.SetActive(true);
}
StartDialogue(dialogue);
}
}
}
public void StartDialogue(Dialogue dialogue)
{
nameText.text = dialogue.name;
sentences.Clear();
foreach(string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
void EndDialogue()
{
Panel.SetActive(false);
}
}