-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextMaker.cs
76 lines (64 loc) · 1.79 KB
/
TextMaker.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextMaker : MonoBehaviour {
private Text text;
private Queue<string> textQueue = new Queue<string> ();
private string curPrinting = "";
private int printIndex = 0;
private bool isPrinting = false;
private float printerTime;
void Awake(){
text = this.GetComponentsInChildren<Text> ()[0];
}
void Update (){
if (isPrinting) {
printerTime += Time.deltaTime;
while (printerTime > 0 && curPrinting.Length > printIndex) {
printChar(curPrinting [printIndex]);
printerTime -= 1 / GameHandler.instance.textSpeed;
printIndex += 1;
}
if (curPrinting.Length <= printIndex) {
isPrinting = false;
if (textQueue.Count == 0)
closeDialog ();
}
}
if (Input.GetButtonDown ("Move"))
Continue ();
}
public void Print(TextHandler.Dialog printMe){
Debug.Assert (textQueue.Count == 0 && isPrinting == false);
foreach (string str in printMe.lines) {
textQueue.Enqueue(str);
}
isPrinting = true;
printerTime = 0;
this.GetComponentInChildren<Image> ().enabled = true;
this.GetComponentInChildren<SpriteRenderer> ().enabled = true;
this.GetComponentInChildren<SpriteRenderer> ().sprite = TextHandler.getCharacter(printMe.character).sprite;
}
public void Continue(){
if (!isPrinting && textQueue.Count > 0) {
curPrinting = textQueue.Dequeue ();
printIndex = 0;
isPrinting = true;
} else {
text.text = curPrinting;
curPrinting = "";
}
}
private void printChar(char c){
text.text += c;
}
private void clearText(){
text.text = "";
}
private void closeDialog (){
GetComponentInChildren<Image> ().enabled = false;
GetComponentInChildren<SpriteRenderer> ().enabled = false;
GameHandler.instance.nextLevel ();
}
}