]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Dialogue/Dialogue.cs
dialogue-system v0.8
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Dialogue / Dialogue.cs
1 using System;
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 skipButton;
11     public GameObject dialogueBox;
12     public GameObject SpeechBubble;
13     public GameObject mouse;
14
15     SpeechBubble speechBubble;
16
17     public Text dialogueText;
18
19     public string[] sentences;
20
21     [HideInInspector]
22     public int index;
23
24     public float typingSpeed;
25
26     void Start()
27     {
28         dialogueBox.SetActive(false);
29         continueButton.SetActive(false);
30         skipButton.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             skipButton.SetActive(true);
41         }
42     }
43
44     public IEnumerator Type()
45     {
46         dialogueBox.SetActive(true);
47         skipButton.SetActive(true);
48         speechBubble.NextSpeaker();
49         SpeechBubble.SetActive(true);
50         foreach (char letter in sentences[index].ToCharArray())
51         {
52             dialogueText.text += letter;
53             yield return new WaitForSeconds(typingSpeed);
54         }
55     }
56
57     public void NextSentence()
58     {
59         FindObjectOfType<AudioManager>().Play("click");
60         continueButton.SetActive(false);
61
62         if (index < sentences.Length - 1)
63         {
64             index++;
65             dialogueText.text = "";
66             StartCoroutine(Type());
67             speechBubble.NextSpeaker();
68         }
69         else
70         {
71             dialogueText.text = "";
72             dialogueBox.SetActive(false);
73             SpeechBubble.SetActive(false);
74             mouse.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
75             mouse.GetComponent<MouseController>().enabled = true;
76         }
77     }
78
79     public void Skip()
80     {
81         FindObjectOfType<AudioManager>().Play("click");
82         skipButton.SetActive(false);
83
84         if (index < sentences.Length - 1)
85         {
86             index++;
87             dialogueText.text = "";
88             dialogueText.text = sentences[index];
89             speechBubble.NextSpeaker();
90         }
91         else
92         {
93             dialogueText.text = "";
94             dialogueBox.SetActive(false);
95             SpeechBubble.SetActive(false);
96             mouse.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
97             mouse.GetComponent<MouseController>().enabled = true;
98         }
99     }
100 }