using System.Collections; using System.Collections.Generic; using System.Linq; using Scampz.GameJam.Assets.Scripts; using Scampz.GameJam.Assets.Scripts.Audio; using Scampz.GameJam.Assets.Scripts.Utilities; using TMPro; using UnityEngine; using UnityEngine.InputSystem; namespace Scampz.GameJam { [RequireComponent(typeof(TextMeshProUGUI))] public class DialogueManager : MonoBehaviour { [SerializeField] private Animator _animator; [SerializeField] private TextMeshProUGUI _dialogueTextGUI; private Queue sentences; public static DialogueManager Instance; public bool IsTalking = false; private InputAction confirmAction; private InputAction escapeAction; private void Awake() { if (Instance != null && Instance != this) { Destroy(this); return; } Instance = this; } public void Start() { escapeAction = new InputAction("cancel", binding: "/escape"); escapeAction.AddBinding("/buttonEast"); escapeAction.Enable(); confirmAction = new InputAction("confirm", binding: "/x"); confirmAction.AddBinding("/buttonSouth"); confirmAction.Enable(); sentences = new Queue(); } private void OnDisable() { confirmAction.Disable(); escapeAction.Disable(); } private void Update() { if (IsTalking && escapeAction.triggered) EndDialogue(); } public void StartDialogue(Dialogue dialogue, TextMeshProUGUI textMeshProUGUI) { _dialogueTextGUI = textMeshProUGUI; _animator.SetBool("IsOpen", true); IsTalking = true; sentences.Clear(); foreach (var sentence in dialogue.Sentences) sentences.Enqueue(sentence); DisplayNextSentence(dialogue); } private IEnumerator PlayAudioCoroutine(SFXClip soundEffect) { yield return null; SFXManager.Instance.PlaySoundEffect(soundEffect.audioClip.name); yield return null; } public void DisplayNextSentence(Dialogue dialogue) { if (!sentences.Any()) { EndDialogue(); StopAllCoroutines(); return; } var sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence, dialogue)); StartCoroutine(nameof(PlayAudioCoroutine), dialogue.SoundEffect); } private IEnumerator TypeSentence(string sentence, Dialogue dialogue) { _dialogueTextGUI.text = string.Empty; foreach (var letter in sentence.ToCharArray()) { _dialogueTextGUI.text += letter; yield return null; yield return new WaitForSeconds(dialogue.TextSpeed); } yield return null; SFXManager.Instance.StopSoundEffect(); yield return new WaitForSeconds(0.3f); yield return new WaitForKeyDown(confirmAction); SFXManager.Instance.PlaySoundEffect(SoundEffectName.Ok); DisplayNextSentence(dialogue); } private void EndDialogue() { StopAllCoroutines(); _animator.SetBool("IsOpen", false); _dialogueTextGUI.text = string.Empty; IsTalking = false; SFXManager.Instance.StopSoundEffect(); var player = GameObject.FindGameObjectWithTag("Player"); var inputController = player.GetComponent(); inputController.EnableActions(); } } }