Files
Scampz/Assets/Scripts/DialogueManager.cs
2022-08-14 18:31:08 -07:00

71 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
namespace Scampz.GameJam
{
public class DialogueManager : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI nameText;
[SerializeField]
private TextMeshProUGUI dialogueText;
public float TextSpeed = 0.1f;
public static DialogueManager Instance { get; private set; }
public Queue<string> sentences;
private void Awake()
{
if (Instance == null)
Instance = this;
}
public void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue)
{
nameText.text = dialogue.name;
sentences.Clear();
foreach (var sentence in dialogue.sentences)
sentences.Enqueue(sentence);
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (!sentences.Any())
EndDialogue();
var sentence = sentences.Dequeue();
StopAllCoroutines();
StartCoroutine(TypeSentence(sentence));
}
private IEnumerator TypeSentence(string sentence)
{
dialogueText.text = string.Empty;
foreach (var letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return null;
yield return new WaitForSecondsRealtime(TextSpeed);
}
}
private void EndDialogue()
{
// do nothing currently
}
}
}