Files
DougDiggem/Assets/Scripts/Dialogue/DialogueManager.cs
T
2026-03-07 10:18:38 -06:00

96 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class DialogueManager : MonoBehaviour
{
private Queue<string> 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<string>();
}
public void ReloadReferences()
{
nameText = GameObject.FindWithTag("DialogNameText").GetComponent<TextMeshProUGUI>();
dialogueText = GameObject.FindWithTag("DialogMessageText").GetComponent<TextMeshProUGUI>();
itemText = GameObject.FindWithTag("DialogItemText").GetComponent<TextMeshProUGUI>();
pickupHint = GameObject.FindWithTag("ItemPickupText").GetComponent<TextMeshProUGUI>();
animator = GameObject.FindWithTag("DialogContainer").GetComponent<Animator>();
}
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 = "";
}
}