]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Dialogue/Dialogue.cs
Dialogue System v0.9.2
[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     int sffIndex = 0;
20     int dtIndex = 0;
21
22     public float typingSpeed;
23
24     [SerializeField] DialogueTrigger[] dialogueTriggerer;
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 == dialogueTriggerer[dtIndex].stringFromFile[sffIndex].text)
38         {
39             continueButton.SetActive(true);
40             skipButton.SetActive(true);
41         } 
42         else 
43         {
44             continueButton.SetActive(false);
45         }
46     }
47
48     public IEnumerator Type()
49     {
50         dialogueBox.SetActive(true);
51         skipButton.SetActive(true);
52         speechBubble.NextSpeaker();
53         SpeechBubble.SetActive(true);
54         dialogueText.text = "";
55
56         foreach (char letter in dialogueTriggerer[dtIndex].stringFromFile[sffIndex].text.ToCharArray())
57         {
58             dialogueText.text += letter;
59             yield return new WaitForSeconds(typingSpeed);
60         }
61     }
62
63     public void NextSentence()
64     {
65         if(sffIndex < (dialogueTriggerer[dtIndex].stringFromFile.Length - 1)) {
66                     sffIndex++;
67         }
68         else if (sffIndex == (dialogueTriggerer[dtIndex].stringFromFile.Length - 1))
69         {
70             sffIndex = 0;
71             dtIndex++;
72         }
73
74         StartCoroutine(Type());
75     }
76
77     public void Skip()
78     {
79         dialogueText.text = dialogueTriggerer[dtIndex].stringFromFile[sffIndex].text;
80     }
81 }