]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Dialogue/Dialogue.cs
Dialogue-System v0.6.1
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Dialogue / Dialogue.cs
1 using System;\r
2 using System.Collections;
3 using System.Collections.Generic;
4 using UnityEngine;
5 using UnityEngine.UI;
6
7 public class Dialogue : MonoBehaviour
8 {
9     public GameObject continueButton;
10     public GameObject dialogueBox;
11     public GameObject SpeechBubble;
12     public GameObject mouse;
13
14     SpeechBubble speechBubble;
15
16     public Text dialogueText;
17
18     public string[] sentences;
19     
20     [HideInInspector]
21     public int index;
22
23     public float typingSpeed;
24
25     public bool skip;
26
27     void Start()
28     {
29         dialogueBox.SetActive(false);
30         continueButton.SetActive(false);
31         SpeechBubble.SetActive(false);
32         speechBubble = SpeechBubble.GetComponent<SpeechBubble>();
33     }
34
35     void Update()
36     {
37         if (dialogueText.text == sentences[index])
38         {
39             continueButton.SetActive(true);
40         }
41
42         if (dialogueText.text != sentences[index])
43         {
44             if (Input.GetKeyDown(KeyCode.Return) || Input.GetMouseButtonDown(0))
45             {
46                 skip = true;
47             }
48             else
49             {
50                 skip = false;
51             }
52         }
53     }
54
55     public IEnumerator Type()
56     {
57         dialogueBox.SetActive(true);
58         speechBubble.NextSpeaker();
59         SpeechBubble.SetActive(true);
60         foreach (char letter in sentences[index].ToCharArray())
61         {
62             dialogueText.text += letter;
63             yield return new WaitForSeconds(typingSpeed);
64         }
65     }
66
67     public void Skip()
68     {
69         dialogueText.text = sentences[index];
70     }
71
72     public void NextSentence()
73     {
74         FindObjectOfType<AudioManager>().Play("click");
75         continueButton.SetActive(false);
76         skip= false;
77
78         if (index < sentences.Length - 1)
79         {
80             index++;
81             dialogueText.text = "";
82             StartCoroutine(Type());
83             speechBubble.NextSpeaker();
84         }
85         else
86         {
87             dialogueText.text = "";
88             dialogueBox.SetActive(false);
89             SpeechBubble.SetActive(false);
90             mouse.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
91             mouse.GetComponent<MouseController>().enabled = true;
92         }
93     }
94 }