Exemplo n.º 1
0
        protected virtual IEnumerator WriteText(string text, Action onWritingComplete, Action onExitTag)
        {
            storyText.text = "";

            // Parse the story text & tag markup to produce a list of tokens for processing
            DialogParser parser = new DialogParser();

            parser.Tokenize(text);

            if (parser.tokens.Count == 0)
            {
                if (onWritingComplete != null)
                {
                    onWritingComplete();
                }
                yield break;
            }

            DialogText dialogText = new DialogText();

            dialogText.writingSpeed     = writingSpeed;
            dialogText.punctuationPause = punctuationPause;

            GameObject typingAudio = null;

            if (characterTypingSound != null || writingSound != null)
            {
                typingAudio = new GameObject("WritingSound");
                typingAudio.AddComponent <AudioSource>();

                if (characterTypingSound != null)
                {
                    typingAudio.audio.clip = characterTypingSound;
                }
                else if (writingSound != null)
                {
                    typingAudio.audio.clip = writingSound;
                }

                typingAudio.audio.loop = loopWritingSound;
                typingAudio.audio.Play();

                dialogText.typingAudio = typingAudio.audio;
            }

            foreach (Token token in parser.tokens)
            {
                switch (token.type)
                {
                case TokenType.Words:
                    dialogText.Append(token.param);
                    break;

                case TokenType.BoldStart:
                    dialogText.boldActive = true;
                    break;

                case TokenType.BoldEnd:
                    dialogText.boldActive = false;
                    break;

                case TokenType.ItalicStart:
                    dialogText.italicActive = true;
                    break;

                case TokenType.ItalicEnd:
                    dialogText.italicActive = false;
                    break;

                case TokenType.ColorStart:
                    dialogText.colorActive = true;
                    dialogText.colorText   = token.param;
                    break;

                case TokenType.ColorEnd:
                    dialogText.colorActive = false;
                    break;

                case TokenType.Wait:
                    float duration = 1f;
                    if (!Single.TryParse(token.param, out duration))
                    {
                        duration = 1f;
                    }
                    yield return(StartCoroutine(WaitForSecondsOrInput(duration)));

                    break;

                case TokenType.WaitForInputNoClear:
                    OnWaitForInputTag(true);
                    yield return(StartCoroutine(WaitForInput(null)));

                    OnWaitForInputTag(false);
                    break;

                case TokenType.WaitForInputAndClear:
                    OnWaitForInputTag(true);
                    yield return(StartCoroutine(WaitForInput(null)));

                    OnWaitForInputTag(false);
                    currentSpeed = writingSpeed;
                    dialogText.Clear();
                    StopVoiceOver();
                    break;

                case TokenType.WaitOnPunctuation:
                    float newPunctuationPause = 0f;
                    if (!Single.TryParse(token.param, out newPunctuationPause))
                    {
                        newPunctuationPause = punctuationPause;
                    }
                    dialogText.punctuationPause = newPunctuationPause;
                    break;

                case TokenType.Clear:
                    dialogText.Clear();
                    break;

                case TokenType.Speed:
                    float newSpeed = 0;
                    if (!Single.TryParse(token.param, out newSpeed))
                    {
                        newSpeed = 0f;
                    }
                    dialogText.writingSpeed = newSpeed;
                    break;

                case TokenType.Exit:

                    if (onExitTag != null)
                    {
                        Destroy(typingAudio);
                        onExitTag();
                    }

                    yield break;

                case TokenType.Message:
                    FungusScript.BroadcastFungusMessage(token.param);
                    break;
                }

                // Update text writing
                while (!dialogText.UpdateGlyphs(wasPointerClicked))
                {
                    storyText.text = dialogText.GetDialogText();
                    yield return(null);
                }
                storyText.text    = dialogText.GetDialogText();
                wasPointerClicked = false;

                // Now process next token
            }

            Destroy(typingAudio);

            if (onWritingComplete != null)
            {
                onWritingComplete();
            }

            yield break;
        }
Exemplo n.º 2
0
        protected virtual IEnumerator WriteText(string text, Action onWritingComplete, Action onExitTag)
        {
            storyText.text = "";

            // Parse the story text & tag markup to produce a list of tokens for processing
            DialogParser parser = new DialogParser();

            parser.Tokenize(text);

            if (parser.tokens.Count == 0)
            {
                if (onWritingComplete != null)
                {
                    onWritingComplete();
                }
                yield break;
            }

            DialogText dialogText = new DialogText();

            dialogText.parentDialog     = this;
            dialogText.writingSpeed     = writingSpeed;
            dialogText.punctuationPause = punctuationPause;
            dialogText.beepPerCharacter = beepPerCharacter;
            dialogText.slowBeepsAt      = slowBeepsAt;
            dialogText.fastBeepsAt      = fastBeepsAt;

            AudioSource typingAudio = GetComponent <AudioSource>();

            if (characterTypingSound != null || writingSound != null)
            {
                if (characterTypingSound != null)
                {
                    typingAudio.clip = characterTypingSound;
                }
                else if (writingSound != null)
                {
                    typingAudio.clip = writingSound;
                }

                typingAudio.loop = loopWritingSound;

                // Start at full volume
                typingAudio.volume = writingVolume;
                SetTypingSoundVolume(true);

                typingAudio.Play();
            }

            foreach (Token token in parser.tokens)
            {
                switch (token.type)
                {
                case TokenType.Words:
                    dialogText.Append(token.param);
                    break;

                case TokenType.BoldStart:
                    dialogText.boldActive = true;
                    break;

                case TokenType.BoldEnd:
                    dialogText.boldActive = false;
                    break;

                case TokenType.ItalicStart:
                    dialogText.italicActive = true;
                    break;

                case TokenType.ItalicEnd:
                    dialogText.italicActive = false;
                    break;

                case TokenType.ColorStart:
                    dialogText.colorActive = true;
                    dialogText.colorText   = token.param;
                    break;

                case TokenType.ColorEnd:
                    dialogText.colorActive = false;
                    break;

                case TokenType.Wait:
                    float duration = 1f;
                    if (!Single.TryParse(token.param, out duration))
                    {
                        duration = 1f;
                    }
                    yield return(StartCoroutine(WaitForSecondsOrInput(duration)));

                    break;

                case TokenType.WaitForInputNoClear:
                    OnWaitForInputTag(true);
                    yield return(StartCoroutine(WaitForInput(null)));

                    OnWaitForInputTag(false);
                    break;

                case TokenType.WaitForInputAndClear:
                    OnWaitForInputTag(true);
                    yield return(StartCoroutine(WaitForInput(null)));

                    OnWaitForInputTag(false);
                    currentSpeed = writingSpeed;
                    dialogText.Clear();
                    StopVoiceOver();
                    break;

                case TokenType.WaitOnPunctuationStart:
                    float newPunctuationPause = 0f;
                    if (!Single.TryParse(token.param, out newPunctuationPause))
                    {
                        newPunctuationPause = 0f;
                    }
                    dialogText.punctuationPause = newPunctuationPause;
                    break;

                case TokenType.WaitOnPunctuationEnd:
                    dialogText.punctuationPause = punctuationPause;
                    break;

                case TokenType.Clear:
                    dialogText.Clear();
                    break;

                case TokenType.SpeedStart:
                    float newSpeed = 0;
                    if (!Single.TryParse(token.param, out newSpeed))
                    {
                        newSpeed = 0f;
                    }
                    dialogText.writingSpeed = newSpeed;
                    break;

                case TokenType.SpeedEnd:
                    dialogText.writingSpeed = writingSpeed;
                    break;

                case TokenType.Exit:
                    if (onExitTag != null)
                    {
                        prevStoryText = storyText.text;
                        SetTypingSoundVolume(false);
                        onExitTag();
                    }
                    yield break;

                case TokenType.Message:
                    Flowchart.BroadcastFungusMessage(token.param);
                    break;

                case TokenType.VerticalPunch:
                    float vPunchIntensity = 0;
                    if (!Single.TryParse(token.param, out vPunchIntensity))
                    {
                        vPunchIntensity = 0f;
                    }
                    VerticalPunch(vPunchIntensity);
                    break;

                case TokenType.HorizontalPunch:
                    float hPunchIntensity = 0;
                    if (!Single.TryParse(token.param, out hPunchIntensity))
                    {
                        hPunchIntensity = 0f;
                    }
                    HorizontalPunch(hPunchIntensity);
                    break;

                case TokenType.Shake:
                    float shakeIntensity = 0;
                    if (!Single.TryParse(token.param, out shakeIntensity))
                    {
                        shakeIntensity = 0f;
                    }
                    Shake(shakeIntensity);
                    break;

                case TokenType.Shiver:
                    float shiverIntensity = 0;
                    if (!Single.TryParse(token.param, out shiverIntensity))
                    {
                        shiverIntensity = 0f;
                    }
                    Shiver(shiverIntensity);
                    break;

                case TokenType.Flash:
                    float flashDuration = 0;
                    if (!Single.TryParse(token.param, out flashDuration))
                    {
                        flashDuration = 0f;
                    }
                    Flash(flashDuration);
                    break;

                case TokenType.Audio:
                {
                    AudioSource audioSource = FindAudio(token.param);
                    if (audioSource != null)
                    {
                        audioSource.PlayOneShot(audioSource.clip);
                    }
                }
                break;

                case TokenType.AudioLoop:
                {
                    AudioSource audioSource = FindAudio(token.param);
                    if (audioSource != null)
                    {
                        audioSource.Play();
                        audioSource.loop = true;
                    }
                }
                break;

                case TokenType.AudioPause:
                {
                    AudioSource audioSource = FindAudio(token.param);
                    if (audioSource != null)
                    {
                        audioSource.Pause();
                    }
                }
                break;

                case TokenType.AudioStop:
                {
                    AudioSource audioSource = FindAudio(token.param);
                    if (audioSource != null)
                    {
                        audioSource.Pause();
                    }
                }
                break;
                }

                // Update text writing
                while (!dialogText.UpdateGlyphs(wasPointerClicked))
                {
                    storyText.text = dialogText.GetDialogText();
                    yield return(null);
                }
                storyText.text    = dialogText.GetDialogText();
                wasPointerClicked = false;

                // Now process next token
            }

            prevStoryText = storyText.text;

            SetTypingSoundVolume(false);

            if (onWritingComplete != null)
            {
                onWritingComplete();
            }

            yield break;
        }
Exemplo n.º 3
0
        protected virtual IEnumerator WriteText(string text, Action onWritingComplete, Action onExitTag)
        {
            storyText.text = "";

            // Parse the story text & tag markup to produce a list of tokens for processing
            DialogParser parser = new DialogParser();
            parser.Tokenize(text);

            if (parser.tokens.Count == 0)
            {
                if (onWritingComplete != null)
                {
                    onWritingComplete();
                }
                yield break;
            }

            DialogText dialogText = new DialogText();
            dialogText.parentDialog = this;
            dialogText.writingSpeed = writingSpeed;
            dialogText.punctuationPause = punctuationPause;
            dialogText.beepPerCharacter = beepPerCharacter;
            dialogText.slowBeepsAt = slowBeepsAt;
            dialogText.fastBeepsAt = fastBeepsAt;

            AudioSource typingAudio = GetComponent<AudioSource>();
            if (characterTypingSound != null || writingSound != null)
            {
                if (characterTypingSound != null)
                {
                    typingAudio.clip = characterTypingSound;
                }
                else if (writingSound != null)
                {
                    typingAudio.clip = writingSound;
                }

                typingAudio.loop = loopWritingSound;

                // Start at full volume
                typingAudio.volume = writingVolume;
                SetTypingSoundVolume(true);

                typingAudio.Play();
            }

            foreach (Token token in parser.tokens)
            {
                switch (token.type)
                {
                case TokenType.Words:
                    dialogText.Append(token.param);
                    break;

                case TokenType.BoldStart:
                    dialogText.boldActive = true;
                    break;

                case TokenType.BoldEnd:
                    dialogText.boldActive = false;
                    break;

                case TokenType.ItalicStart:
                    dialogText.italicActive = true;
                    break;

                case TokenType.ItalicEnd:
                    dialogText.italicActive = false;
                    break;

                case TokenType.ColorStart:
                    dialogText.colorActive = true;
                    dialogText.colorText = token.param;
                    break;

                case TokenType.ColorEnd:
                    dialogText.colorActive = false;
                    break;

                case TokenType.Wait:
                    float duration = 1f;
                    if (!Single.TryParse(token.param, out duration))
                    {
                        duration = 1f;
                    }
                    yield return StartCoroutine(WaitForSecondsOrInput(duration));
                    break;

                case TokenType.WaitForInputNoClear:
                    OnWaitForInputTag(true);
                    yield return StartCoroutine(WaitForInput(null));
                    OnWaitForInputTag(false);
                    break;

                case TokenType.WaitForInputAndClear:
                    OnWaitForInputTag(true);
                    yield return StartCoroutine(WaitForInput(null));
                    OnWaitForInputTag(false);
                    currentSpeed = writingSpeed;
                    dialogText.Clear();
                    StopVoiceOver();
                    break;

                case TokenType.WaitOnPunctuationStart:
                    float newPunctuationPause = 0f;
                    if (!Single.TryParse(token.param, out newPunctuationPause))
                    {
                        newPunctuationPause = 0f;
                    }
                    dialogText.punctuationPause = newPunctuationPause;
                    break;
                case TokenType.WaitOnPunctuationEnd:
                    dialogText.punctuationPause = punctuationPause;
                    break;

                case TokenType.Clear:
                    dialogText.Clear();
                    break;

                case TokenType.SpeedStart:
                    float newSpeed = 0;
                    if (!Single.TryParse(token.param, out newSpeed))
                    {
                        newSpeed = 0f;
                    }
                    dialogText.writingSpeed = newSpeed;
                    break;

                case TokenType.SpeedEnd:
                    dialogText.writingSpeed = writingSpeed;
                    break;

                case TokenType.Exit:
                    if (onExitTag != null)
                    {
                        prevStoryText = storyText.text;
                        SetTypingSoundVolume(false);
                        onExitTag();
                    }
                    yield break;

                case TokenType.Message:
                    Flowchart.BroadcastFungusMessage(token.param);
                    break;
                case TokenType.VerticalPunch:
                    float vPunchIntensity = 0;
                    if (!Single.TryParse(token.param, out vPunchIntensity))
                    {
                        vPunchIntensity = 0f;
                    }
                    VerticalPunch(vPunchIntensity);
                    break;
                case TokenType.HorizontalPunch:
                    float hPunchIntensity = 0;
                    if (!Single.TryParse(token.param, out hPunchIntensity))
                    {
                        hPunchIntensity = 0f;
                    }
                    HorizontalPunch(hPunchIntensity);
                    break;
                case TokenType.Shake:
                    float shakeIntensity = 0;
                    if (!Single.TryParse(token.param, out shakeIntensity))
                    {
                        shakeIntensity = 0f;
                    }
                    Shake(shakeIntensity);
                    break;
                case TokenType.Shiver:
                    float shiverIntensity = 0;
                    if (!Single.TryParse(token.param, out shiverIntensity))
                    {
                        shiverIntensity = 0f;
                    }
                    Shiver(shiverIntensity);
                    break;
                case TokenType.Flash:
                    float flashDuration = 0;
                    if (!Single.TryParse(token.param, out flashDuration))
                    {
                        flashDuration = 0f;
                    }
                    Flash(flashDuration);
                    break;
                case TokenType.Audio:
                    {
                        AudioSource audioSource = FindAudio(token.param);
                        if (audioSource != null)
                        {
                            audioSource.PlayOneShot(audioSource.clip);
                        }
                    }
                    break;
                case TokenType.AudioLoop:
                    {
                        AudioSource audioSource = FindAudio(token.param);
                        if (audioSource != null)
                        {
                            audioSource.Play();
                            audioSource.loop = true;
                        }
                    }
                    break;
                case TokenType.AudioPause:
                    {
                        AudioSource audioSource = FindAudio(token.param);
                        if (audioSource != null)
                        {
                            audioSource.Pause ();
                        }
                    }
                    break;
                case TokenType.AudioStop:
                    {
                        AudioSource audioSource = FindAudio(token.param);
                        if (audioSource != null)
                        {
                            audioSource.Pause ();
                        }
                    }
                    break;
                }

                // Update text writing
                while (!dialogText.UpdateGlyphs(wasPointerClicked))
                {
                    storyText.text = dialogText.GetDialogText();
                    yield return null;
                }
                storyText.text = dialogText.GetDialogText();
                wasPointerClicked = false;

                // Now process next token
            }

            prevStoryText = storyText.text;

            SetTypingSoundVolume(false);

            if (onWritingComplete != null)
            {
                onWritingComplete();
            }

            yield break;
        }