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