//--------------------------------------------------------------------- // Typing //--------------------------------------------------------------------- /// <summary> /// The coroutine that handles typing out the sentence. /// </summary> /// <param name="sentence">The sentence to type.</param> /// <param name="autoAdvance">Whether or not to automatically advance the /// dialog after typing has finished.</param> /// <param name="delayBeforeAdvance">How long to delay before advancing the /// dialog, in seconds</param> /// <param name="speed">The speed of typing, in characters per second.</param> private IEnumerator _TypeSentence(string sentence, bool autoAdvance, float delayBeforeAdvance, float speed, bool animateSpeaker) { manager.StillWriting = true; TalkingSound.Play(); if (animateSpeaker) { DialogManager.StartTalking(currentCharacter); } ClearText(); float waitTime = 1 / speed; if (sentence != null) { for (int i = 0; i < sentence.Length; i++) { char c = sentence[i]; TypeCharacter(c); // Waiting for any time after the sentence is finished opens the door // for a race condition. if (i < sentence.Length - 1) { if (DialogManager.Punctuation.ContainsKey(c)) { TalkingSound.Stop(); yield return(new WaitForSeconds(DialogManager.Punctuation[c])); TalkingSound.Play(); } else { yield return(new WaitForSeconds(waitTime)); } } } } TryListDecisions(); if (animateSpeaker) { DialogManager.StopTalking(currentCharacter); } TalkingSound.Stop(); manager.StillWriting = false; if (delayBeforeAdvance > 0) { yield return(new WaitForSeconds(delayBeforeAdvance)); } if (autoAdvance) { DialogManager.ContinueDialog(); } }
/// <summary> /// Type out a sentence spoken by a certain speaker. If another sentence is /// already being typed, the dialog box will instead skip to the end of the /// sentence being typed. If the player will need to make a decision /// afterward, this will also display the options. /// </summary> /// <param name="sentence">The sentence to type.</param> /// <param name="speaker">The speaker of the sentence.</param> /// <param name="autoAdvance">Whether or not to automatically advance the /// dialog after typing has finished.</param> /// <param name="delayBeforeAdvance">How long to delay before advancing the /// dialog, in seconds</param> /// <param name="typingSpeed">The speed of typing, in characters per second.</param> /// <param name="animateSpeaker">Whether or not to control the speaker's animation.</param> public void Type(string sentence, string speaker = "", bool autoAdvance = false, float delayBeforeAdvance = 0f, float typingSpeed = 100f, bool animateSpeaker = true) { SetSpeakerText(speaker); if (manager.StillWriting && !IsFinishedTyping(sentence)) { // Stop typing, just display the whole thing and wait for the next input. SkipTyping(sentence); TryListDecisions(); if (animateSpeaker) { DialogManager.StopTalking(currentCharacter); } } else { // Start typing out the next sentence. if (typingCoroutine != null) { StopCoroutine(typingCoroutine); } typingCoroutine = StartCoroutine(_TypeSentence(sentence, autoAdvance, delayBeforeAdvance, typingSpeed, animateSpeaker)); } }