add speech
This commit is contained in:
@@ -7,6 +7,12 @@ public class DialogueManager : MonoBehaviour
|
||||
{
|
||||
private Queue<string> sentences;
|
||||
private Dialogue currentDialogue;
|
||||
private DialogueVoice currentDialogueVoice;
|
||||
private float maxDialogueWaitTime = 1.0f;
|
||||
|
||||
// use array of audio sources so we don't cut off any of them
|
||||
private AudioSource[] voiceSourcePool;
|
||||
private int audioSourcePoolIndex = 0;
|
||||
|
||||
public TextMeshProUGUI nameText;
|
||||
public TextMeshProUGUI dialogueText;
|
||||
@@ -20,6 +26,7 @@ public class DialogueManager : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
sentences = new Queue<string>();
|
||||
voiceSourcePool = GetComponentsInChildren<AudioSource>();
|
||||
}
|
||||
|
||||
public void ReloadReferences()
|
||||
@@ -31,12 +38,13 @@ public void ReloadReferences()
|
||||
animator = GameObject.FindWithTag("DialogContainer").GetComponent<Animator>();
|
||||
}
|
||||
|
||||
public void StartDialogue(Dialogue dialogue)
|
||||
public void StartDialogue(Dialogue dialogue, DialogueVoice dialogueVoice)
|
||||
{
|
||||
animator.SetBool("IsOpen", true);
|
||||
|
||||
sentences.Clear();
|
||||
currentDialogue = dialogue;
|
||||
currentDialogueVoice = dialogueVoice;
|
||||
nameText.text = dialogue.charName;
|
||||
|
||||
foreach (string sentence in dialogue.sentences)
|
||||
@@ -58,12 +66,15 @@ public void DisplayNextSentence()
|
||||
|
||||
string sentence = sentences.Dequeue();
|
||||
StopAllCoroutines();
|
||||
StopVoices();
|
||||
StartCoroutine(TypeSentence(sentence));
|
||||
}
|
||||
|
||||
public void EndDialogue()
|
||||
{
|
||||
animator.SetBool("IsOpen", false);
|
||||
StopAllCoroutines();
|
||||
StopVoices();
|
||||
|
||||
if (currentDialogue != null)
|
||||
{
|
||||
@@ -74,10 +85,31 @@ public void EndDialogue()
|
||||
IEnumerator TypeSentence(string sentence)
|
||||
{
|
||||
dialogueText.text = "";
|
||||
foreach (char c in sentence.ToCharArray())
|
||||
char[] sentenceCharArray = sentence.ToCharArray();
|
||||
for (int i = 0; i < sentenceCharArray.Length; i++)
|
||||
{
|
||||
dialogueText.text += c;
|
||||
yield return new WaitForSeconds(0.01f);
|
||||
dialogueText.text += sentenceCharArray[i];
|
||||
|
||||
if (currentDialogueVoice != null && currentDialogueVoice.voice != Voices.None)
|
||||
{
|
||||
if (i % currentDialogueVoice.speechInterval == 0)
|
||||
{
|
||||
AudioClip voiceToPlay = currentDialogueVoice.GetClipFromChar(char.ToLower(sentenceCharArray[i]));
|
||||
|
||||
if (voiceToPlay != null)
|
||||
{
|
||||
PlayNextClip(voiceToPlay);
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(maxDialogueWaitTime - currentDialogueVoice.talkSpeed * maxDialogueWaitTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Add speed variable to non-voices (Talkable.cs)
|
||||
yield return new WaitForSeconds(0.03f);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,4 +124,19 @@ public void HideItemText()
|
||||
itemText.text = "";
|
||||
pickupHint.text = "";
|
||||
}
|
||||
|
||||
private void PlayNextClip(AudioClip clip)
|
||||
{
|
||||
audioSourcePoolIndex = (audioSourcePoolIndex + 1) % voiceSourcePool.Length;
|
||||
voiceSourcePool[audioSourcePoolIndex].clip = clip;
|
||||
voiceSourcePool[audioSourcePoolIndex].Play();
|
||||
}
|
||||
|
||||
private void StopVoices()
|
||||
{
|
||||
foreach (AudioSource voice in voiceSourcePool)
|
||||
{
|
||||
voice.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user