Skip to content
Snippets Groups Projects
CharacterDialog.cs 3.97 KiB
Newer Older
  • Learn to ignore specific revisions
  • John Schihada's avatar
    John Schihada committed
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    using static TaskCharakterAnimation;
    
    public class CharacterDialog : MonoBehaviour
    {
        public TextMeshPro textDisplay;
        public TextMeshPro textHint;
        public string[] sentences;
    
        private int sentenceIndex;
        private int letterIndex = 0;
        private bool typingActive = false;
    
    John Schihada's avatar
    John Schihada committed
        //speed for typing the Text
        public float typingSpeed;
    
        private float timer = 0;
    
    John Schihada's avatar
    John Schihada committed
    
        //Only reset once after Player is out of range of the TaskCharacter
        private bool textReseted = true;
    
        //If gameSucceeded -> Disable Talking with TaskCharacter
        private bool gameSucceeded = false;
    
    John Schihada's avatar
    John Schihada committed
    
        // Start is called before the first frame update
        void Start()
        {
    
            CommunicationEvents.gameSucceededEvent.AddListener(StartGameSucceededSentence);
            CommunicationEvents.gameNotSucceededEvent.AddListener(StopGameSucceededSentence);
    
    John Schihada's avatar
    John Schihada committed
            //Type first sentence
    
            typingActive = true;
            TypeFkt();
    
    John Schihada's avatar
    John Schihada committed
        }
    
        private void Update()
        {
    
            if(!gameSucceeded && Input.GetKeyDown(KeyCode.Return) && TaskCharakterAnimation.getPlayerInTalkingZone())
    
    John Schihada's avatar
    John Schihada committed
            {
                //Type Next sentence if player is in the talkinZone around the TaskCharacter AND the player typed the return-Key
                NextSentence();
            }
    
            else if (!gameSucceeded && !TaskCharakterAnimation.getPlayerInTalkingZone() && !textReseted)
    
    John Schihada's avatar
    John Schihada committed
            {
                //Reset Sentence if Player is out of range of the TaskCharacter and it's not already reseted
                ResetSentence();
            }
        }
    
    
        public void TypeFkt() {
            if (typingActive)
            {
                if (this.timer >= this.typingSpeed)
                {
                    if (letterIndex < sentences[sentenceIndex].Length)
                    {
                        textDisplay.text += sentences[sentenceIndex].ToCharArray()[letterIndex];
                        letterIndex++;
                    }
                    else
                    {
                        this.typingActive = false;
                        letterIndex = 0;
                    }
    
                    this.timer = 0;
                }
                else
                {
                    this.timer += Time.deltaTime;
                }
            }
        }
    
    
    John Schihada's avatar
    John Schihada committed
    
        public void NextSentence() {
            //-2 because the last sentence is only for SucceededPushout-Purposes
    
            if (sentenceIndex < sentences.Length - 2)
    
    John Schihada's avatar
    John Schihada committed
            {
    
                TaskCharakterAnimation.setTaskCharacterAddressed(true);
    
                sentenceIndex++;
                letterIndex = 0;
                typingActive = true;
                timer = 0;
    
    John Schihada's avatar
    John Schihada committed
                textDisplay.text = "";
    
                TypeFkt();
    
    John Schihada's avatar
    John Schihada committed
                textReseted = false;
            }
            else {
    
                letterIndex = 0;
                typingActive = false;
                timer = 0;
                textReseted = false;
    
    John Schihada's avatar
    John Schihada committed
                textDisplay.text = "";
            }
        }
    
        public void ResetSentence() {
    
            TaskCharakterAnimation.setTaskCharacterAddressed(false);
    
            sentenceIndex = 0;
            letterIndex = 0;
            typingActive = true;
            timer = 0;
    
    John Schihada's avatar
    John Schihada committed
            textDisplay.text = "";
            //Type first sentence again
    
            TypeFkt();
    
    John Schihada's avatar
    John Schihada committed
            textReseted = true;
        }
    
    
        public void StartGameSucceededSentence()
        {
            if (!gameSucceeded)
            {
                textDisplay.text = "";
                //Last Sentence is the Pushout-Sentence
                sentenceIndex = sentences.Length - 1;
                letterIndex = 0;
                typingActive = true;
                timer = 0;
                gameSucceeded = true;
                //Disable Hint With Enter-Key for Talking
                textHint.GetComponent<MeshRenderer>().enabled = false;
                //Type final message
                TypeFkt();
            }
        }
    
        public void StopGameSucceededSentence()
        {
            if (gameSucceeded)
            {
                gameSucceeded = false;
                //Enable Hint With Enter-Key for Talking
                textHint.GetComponent<MeshRenderer>().enabled = true;
                ResetSentence();
            }