using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; public class DialogueManager : MonoBehaviour { private Queue sentences; private Dialogue currentDialogue; public TextMeshProUGUI nameText; public TextMeshProUGUI dialogueText; public TextMeshProUGUI itemText; // displays name of nearby item public TextMeshProUGUI pickupHint; // tells the player to press 'E' to pickup public Animator animator; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { sentences = new Queue(); } public void ReloadReferences() { nameText = GameObject.FindWithTag("DialogNameText").GetComponent(); dialogueText = GameObject.FindWithTag("DialogMessageText").GetComponent(); itemText = GameObject.FindWithTag("DialogItemText").GetComponent(); pickupHint = GameObject.FindWithTag("ItemPickupText").GetComponent(); animator = GameObject.FindWithTag("DialogContainer").GetComponent(); } public void StartDialogue(Dialogue dialogue) { animator.SetBool("IsOpen", true); sentences.Clear(); currentDialogue = dialogue; nameText.text = dialogue.charName; foreach (string sentence in dialogue.sentences) { sentences.Enqueue(sentence); } currentDialogue.isActive = true; DisplayNextSentence(); } public void DisplayNextSentence() { if (sentences.Count == 0) { EndDialogue(); return; } string sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence)); } public void EndDialogue() { animator.SetBool("IsOpen", false); if (currentDialogue != null) { currentDialogue.isActive = false; } } IEnumerator TypeSentence(string sentence) { dialogueText.text = ""; foreach (char c in sentence.ToCharArray()) { dialogueText.text += c; yield return new WaitForSeconds(0.01f); } } public void ShowItemText(string itemName) { itemText.text = itemName; pickupHint.text = "Press \"E\" to pick up"; } public void HideItemText() { itemText.text = ""; pickupHint.text = ""; } }