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 sentences; private void Awake() { if (Instance == null) Instance = this; } public void Start() { sentences = new Queue(); } 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 } } }