]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Dialogue/Dialogue.cs
Merge pull request #19 from EliasFleckenstein03/main
[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 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     public IEnumerator Type()
55     {
56         dialogueBox.SetActive(true);
57         speechBubble.NextSpeaker();
58         SpeechBubble.SetActive(true);
59         foreach (char letter in sentences[index].ToCharArray())
60         {
61             dialogueText.text += letter;
62             yield return new WaitForSeconds(typingSpeed);
63         }
64     }
65
66     public void Skip()
67     {
68         dialogueText.text = sentences[index];
69     }
70
71     public void NextSentence()
72     {
73         FindObjectOfType<AudioManager>().Play("click");
74         continueButton.SetActive(false);
75         skip= false;
76
77         if (index < sentences.Length - 1)
78         {
79             index++;
80             dialogueText.text = "";
81             StartCoroutine(Type());
82             speechBubble.NextSpeaker();
83         }
84         else
85         {
86             dialogueText.text = "";
87             dialogueBox.SetActive(false);
88             SpeechBubble.SetActive(false);
89             mouse.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
90             mouse.GetComponent<MouseController>().enabled = true;
91         }
92     }
93 }