all repos — RPG @ 8afc9b1674608fc9537175aedc467381ad1aa746

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 NPC alreadyTalking = null;
 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    public delegate void MyDelegate();
 27    static public MyDelegate actionAfterDialogue;
 28
 29
 30    
 31    void Start()
 32    {
 33        asource = GetComponent<AudioSource>();
 34        sentences = new Queue<string>();
 35    }
 36    private void Update()
 37    {
 38        if (alreadyTalking)
 39        {
 40            if (Input.GetKey(KeyCode.LeftControl))
 41            {
 42                DisplayNextSentence();
 43            }
 44            if (Input.GetKeyDown("e"))
 45            {
 46                DisplayNextSentence();
 47            }
 48        }
 49    }
 50
 51    public void StartDialogue(NPC npc)
 52    {
 53        if(npc.dialogue.sentences.Length == 0)
 54        {
 55            return;
 56        }
 57        actionAfterDialogue += new MyDelegate(npc.actionAfterDialogue);
 58        alreadyTalking = npc;
 59        setInputEnabled(false);
 60        endDialogue = false;
 61        animator.SetBool("IsOpen", true);
 62        nameText.text = npc.dialogue.name;
 63        voiceClip = npc.dialogue.sound;
 64
 65        sentences.Clear();
 66
 67        foreach(string sentence in npc.dialogue.sentences)
 68        {
 69            sentences.Enqueue(sentence);
 70        }
 71
 72        DisplayNextSentence();
 73    }
 74
 75    public void DisplayNextSentence()
 76    {
 77        if(sentences.Count == 0)
 78        {
 79            endDialogue = true;
 80        }
 81        if (UI.displaying_text)
 82        {
 83            UI.stopText();
 84            dialogueText.text = sentence;
 85        } else
 86        {
 87            if (endDialogue)
 88            {
 89                EndDialogue();
 90                return;
 91            }
 92            sentence = sentences.Dequeue();
 93            UI.changeDialogue(dialogueText, sentence, asource, voiceClip);
 94        }
 95    }
 96
 97    public void EndDialogue()
 98    {
 99        setInputEnabled(true);
100        UI.stopText();
101        animator.SetBool("IsOpen", false);
102        if(actionAfterDialogue != null)
103            actionAfterDialogue();
104        actionAfterDialogue = null;
105        StartCoroutine(CoolDown(0.3f));
106    }
107
108    private void setInputEnabled(bool state)
109    {
110        if (mainCamera)
111        {
112            mainCamera.enabled = state;
113        } else
114        {
115            Debug.Log("Camera Component not set in inspector");
116        }
117        if (player)
118        {
119            player.enabled = state;
120            player.gameObject.GetComponent<Animator>().SetBool("Running", false);
121        }
122        else
123        {
124            Debug.Log("Player Component not set in inspector");
125        }
126    }
127
128    private IEnumerator CoolDown(float seconds)
129    {
130        yield return new WaitForSeconds(seconds);
131        alreadyTalking = null;
132    }
133}