using System.Collections; using System.Collections.Generic; using System.Linq; using Scampz.GameJam.Assets.Scripts; using Scampz.GameJam.Assets.Scripts.Utilities; using TMPro; using UnityEngine; namespace Scampz.GameJam { public class DialogueManager : MonoBehaviour { [SerializeField] private Animator _animator; [SerializeField] private TextMeshProUGUI dialogueTextGUI; [SerializeField] private float _textSpeed = 0.1f; private Queue sentences; public static DialogueManager Instance; public bool IsTalking = false; private void Awake() { if (Instance != null && Instance != this) { Destroy(this); return; } Instance = this; } public void Start() { sentences = new Queue(); } private void Update() { if (IsTalking && Input.GetButtonDown(InputOptions.Cancel)) EndDialogue(); } public void StartDialogue(Dialogue dialogue) { _animator.SetBool("IsOpen", true); IsTalking = true; sentences.Clear(); foreach (var sentence in dialogue.sentences) sentences.Enqueue(sentence); DisplayNextSentence(); } public void DisplayNextSentence() { if (!sentences.Any()) { EndDialogue(); return; } var sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence)); } private IEnumerator TypeSentence(string sentence) { dialogueTextGUI.text = string.Empty; foreach (var letter in sentence.ToCharArray()) { dialogueTextGUI.text += letter; yield return null; yield return new WaitForSecondsRealtime(_textSpeed); } yield return new WaitForKeyDown(InputOptions.Submit); DisplayNextSentence(); } private void EndDialogue() { _animator.SetBool("IsOpen", false); IsTalking = false; } } }