Files
DougDiggem/Assets/Scripts/Dialogue/DialogueManager.cs
T
2025-06-20 22:58:44 -05:00

72 lines
1.6 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 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 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);
}
}
}