114 lines
2.6 KiB
C#
114 lines
2.6 KiB
C#
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;
|
|
|
|
namespace Scampz.GameJam
|
|
{
|
|
[RequireComponent(typeof(TextMeshProUGUI))]
|
|
public class DialogueManager : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Animator _animator;
|
|
[SerializeField]
|
|
private TextMeshProUGUI _dialogueTextGUI;
|
|
[SerializeField]
|
|
private float _defaultTextSpeed = 0.1f;
|
|
|
|
private float _currentTextSpeed;
|
|
|
|
|
|
private Queue<string> 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<string>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (IsTalking && Input.GetButtonDown(InputOptions.Cancel))
|
|
EndDialogue();
|
|
|
|
_currentTextSpeed = _defaultTextSpeed;
|
|
}
|
|
|
|
public void StartDialogue(Dialogue dialogue)
|
|
{
|
|
_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(_currentTextSpeed);
|
|
}
|
|
|
|
yield return new WaitForSeconds(1f);
|
|
yield return new WaitForKeyDown(InputOptions.Submit);
|
|
SFXManager.Instance.PlaySoundEffect(SoundEffectName.Ok);
|
|
DisplayNextSentence(dialogue);
|
|
}
|
|
|
|
private void EndDialogue()
|
|
{
|
|
_animator.SetBool("IsOpen", false);
|
|
_dialogueTextGUI.text = string.Empty;
|
|
IsTalking = false;
|
|
SFXManager.Instance.StopSoundEffect();
|
|
}
|
|
}
|
|
}
|