-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogueMan.cs
236 lines (221 loc) · 6.88 KB
/
DialogueMan.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System.Collections;
using UnityEngine;
using TMPro;
public class DialogueMan : MonoBehaviour
{
//=========== Check the example for better understanding of how this is implemented :D ===========
//Dialogue Scriptable object
public Dialogue Dl;
//Speed of text written
public float textSpeed;
//Bools for commands (I dont know if they are needed but i dont wanna
//think)
bool canExcute = false;
bool clicked = true;
bool CantContinue = false;
//AudioSource for clicking
public AudioSource click;
public AudioSource VoiceMan;
//Player stats can be added to it
public int playerCharisma = 6;
public int playerSpeech = 6;
//Texts
public TextMeshProUGUI DialogueTxt;
public TextMeshProUGUI[] ChoiceTxt;
int dialogueID;
int choiceID;
//Which choise , list of choices .
//public List<int> choiceID = new List<int>();
//[END] Command stuff
public bool hasEnded = false;
float timer;
public float timeToDisable;
private void Start()
{
//When conersation starts with the first dialogue
ChooseDialogue(0);
}
private void Update()
{
//Timer after dialogue ends
if(hasEnded)
{
if(timer >= timeToDisable)
{
hasEnded = false;
DialogueTxt.gameObject.SetActive(false);
}
else
{
timer += Time.deltaTime;
}
}
//Skiping with E or left mouse
if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E))
{
VoiceMan.Stop();
if (DialogueTxt.text != Dl.DialogueTs[dialogueID].text)
{
StopAllCoroutines();
DialogueTxt.text = Dl.DialogueTs[dialogueID].text;
}
else
{
clicked = true;
Commands();
}
}
//For continue dialogue
if(VoiceMan.isPlaying == false && CantContinue)
{
Commands();
CantContinue = false;
}
}
//Choosing dialogue
void ChooseDialogue(int i)
{
//Stop voice
if (Dl.DialogueTs[i].Voice != null)
{
VoiceMan.Stop();
}
//Debug.Log("Updated");
//Update dialogue text
dialogueID = i;
DialogueTxt.text = "";
//Update buttons for choices
UpdateChoices(i);
//
canExcute = false;
Commands();
//Writing effect
StartCoroutine(TypeDialogue(Dl.DialogueTs[i].text));
//Play voice
if (Dl.DialogueTs[i].Voice != null)
{
VoiceMan.clip = Dl.DialogueTs[i].Voice;
VoiceMan.Play();
}
}
//Updating choices
void UpdateChoices(int i)
{
//clean out
for (int j = 0; j < ChoiceTxt.Length; j++)
{
ChoiceTxt[j].text = "";
}
//Add in
for (int j = 0; j < Dl.DialogueTs[i].ChoiceIDs.Length; j++)
{
ChoiceTxt[j].text = Dl.choices[Dl.DialogueTs[i].ChoiceIDs[j] - 1].text;
}
}
//Button
public void ChooseChoice(int choiceX)
{
//Clicking Sound effect
click.pitch = Random.Range(0.55f, 1.25f);
click.Play();
//When clicking a button with a value of 1 , 2 , 3 etc
if (hasEnded == false && ChoiceTxt[choiceX].text != "")
{
int nextNodeID = 0;
for (int i = 0; i < Dl.choices.Count; i++)
{
if (Dl.choices[i].text == ChoiceTxt[choiceX].text)
{
nextNodeID = Dl.choices[i].DiloguesID;
choiceID = i;
Debug.Log(choiceID);
break;
}
}
//If coroutine running stop then play the new one
StopAllCoroutines();
ChooseDialogue(nextNodeID - 1);
//Commands for choice like flirting and persuding etc
ChoiceCommands();
}
}
//================Commands and Effect =================
//Commands
void Commands()
{
//Add more commands like fight and you can use it how ever you want .
//End the conversation and a timer delay before dialogue gets disabled .
if (Dl.DialogueTs[dialogueID].commands == DialogueChoice.Commands.End)
{
//Debug.Log("Ended");
if (canExcute)
{
hasEnded = true;
}
for (int j = 0; j < ChoiceTxt.Length; j++)
{
//Remove choices
ChoiceTxt[j].text = "";
ChoiceTxt[j].transform.parent.gameObject.SetActive(false);
}
}
//Continue dialogue with a new dialogue the first ID is the next dialogue
if (Dl.DialogueTs[dialogueID].commands == DialogueChoice.Commands.Continue && canExcute)
{
if (DialogueTxt.text == Dl.DialogueTs[dialogueID].text && clicked && !VoiceMan.isPlaying)
{
ChooseDialogue(Dl.DialogueTs[dialogueID].ChoiceIDs[0] - 1);
clicked = true;
CantContinue = false;
}
else
{
CantContinue = true;
}
}
}
void ChoiceCommands()
{
//if(player has enough charisma )
if (Dl.choices[choiceID].commands == ChoiceChoice.Commands.Flirt)
CheckStat(playerCharisma);
//Persude stat
if (Dl.choices[choiceID].commands == ChoiceChoice.Commands.Persude)
CheckStat(playerSpeech);
}
void CheckStat(int Stat)
{
//If player has enough Stat then play the main dialogue
if (Dl.choices[choiceID].reqStat <= Stat)
{
Debug.Log(Dl.choices[choiceID].reqStat);
StopAllCoroutines();
ChooseDialogue(Dl.choices[choiceID].DiloguesID - 1);
}
//or else
else
{
StopAllCoroutines();
Debug.Log(Dl.choices[choiceID].reqStat);
ChooseDialogue(Dl.choices[choiceID].alternativeDialogue - 1);
}
}
//Typing dialogue And Commands
IEnumerator TypeDialogue(string dialogue)
{
//Typing effect
foreach(char c in dialogue)
{
DialogueTxt.text += c;
yield return new WaitForSeconds(textSpeed);
}
//End dialogue
canExcute = true;
clicked = false;
Commands();
//Continue dialogue
yield return new WaitForSeconds(.35f);
clicked = true;
Commands();
}
}