all repos — RPG @ 502924ae35666f2d5ac541310b3e57f2e4fe70b4

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Overworld/Dialogue/DialogueManager.cs (view raw)

  1using System.Collections;
  2using System.Collections.Generic;
  3using UnityEngine;
  4using UnityEngine.UI;
  5
  6[RequireComponent(typeof(AudioSource))]
  7public class DialogueManager : MonoBehaviour
  8{
  9    public bool alreadyTalking = false;
 10    [Header("Dialogue Box")]
 11    public Animator animator;
 12    public Text nameText;
 13    public Text dialogueText;
 14
 15    [Header("Player Input")]
 16    public Orbit mainCamera;
 17    public NoJumpController player;
 18    
 19    private bool endDialogue = false;
 20    private AudioClip voiceClip;
 21    private AudioSource asource;
 22
 23    private string sentence;
 24    private Queue<string> sentences;
 25
 26
 27    
 28    void Start()
 29    {
 30        asource = GetComponent<AudioSource>();
 31        sentences = new Queue<string>();
 32    }
 33    private void Update()
 34    {
 35        if (alreadyTalking)
 36        {
 37            if (Input.GetKey(KeyCode.LeftControl))
 38            {
 39                DisplayNextSentence();
 40            }
 41            if (Input.GetKeyDown("e"))
 42            {
 43                DisplayNextSentence();
 44            }
 45        }
 46    }
 47
 48    public void StartDialogue(Dialogue dialogue)
 49    {
 50        if(dialogue.sentences.Length == 0)
 51        {
 52            return;
 53        }
 54        alreadyTalking = true;
 55        setInputEnabled(false);
 56        endDialogue = false;
 57        animator.SetBool("IsOpen", true);
 58        nameText.text = dialogue.name;
 59        voiceClip = dialogue.sound;
 60
 61        sentences.Clear();
 62
 63        foreach(string sentence in dialogue.sentences)
 64        {
 65            sentences.Enqueue(sentence);
 66        }
 67
 68        DisplayNextSentence();
 69    }
 70
 71    public void DisplayNextSentence()
 72    {
 73        if(sentences.Count == 0)
 74        {
 75            endDialogue = true;
 76        }
 77        if (UI.displaying_text)
 78        {
 79            UI.stopText();
 80            dialogueText.text = sentence;
 81        } else
 82        {
 83            if (endDialogue)
 84            {
 85                EndDialogue();
 86                return;
 87            }
 88            sentence = sentences.Dequeue();
 89            UI.changeDialogue(dialogueText, sentence, asource, voiceClip);
 90        }
 91    }
 92
 93    public void EndDialogue()
 94    {
 95        setInputEnabled(true);
 96        UI.stopText();
 97        animator.SetBool("IsOpen", false);
 98        StartCoroutine(CoolDown(0.3f));
 99    }
100
101    private void setInputEnabled(bool state)
102    {
103        if (mainCamera)
104        {
105            mainCamera.enabled = state;
106        } else
107        {
108            Debug.Log("Camera Component not set in inspector");
109        }
110        if (player)
111        {
112            player.enabled = state;
113            player.gameObject.GetComponent<Animator>().SetBool("Running", false);
114        }
115        else
116        {
117            Debug.Log("Player Component not set in inspector");
118        }
119    }
120
121    private IEnumerator CoolDown(float seconds)
122    {
123        yield return new WaitForSeconds(seconds);
124        alreadyTalking = false;
125    }
126}