all repos — RPG @ 9a9528c6bffc34089550b5e8fb4c5a1c4754c8cf

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        actionAfterDialogue();
103        actionAfterDialogue = null;
104        StartCoroutine(CoolDown(0.3f));
105    }
106
107    private void setInputEnabled(bool state)
108    {
109        if (mainCamera)
110        {
111            mainCamera.enabled = state;
112        } else
113        {
114            Debug.Log("Camera Component not set in inspector");
115        }
116        if (player)
117        {
118            player.enabled = state;
119            player.gameObject.GetComponent<Animator>().SetBool("Running", false);
120        }
121        else
122        {
123            Debug.Log("Player Component not set in inspector");
124        }
125    }
126
127    private IEnumerator CoolDown(float seconds)
128    {
129        yield return new WaitForSeconds(seconds);
130        alreadyTalking = null;
131    }
132}