Repository navigation
Expand file tree
/
Copy pathDialogManager.js
More file actions
209 lines (182 loc) · 5.69 KB
/
Copy pathDialogManager.js
File metadata and controls
209 lines (182 loc) · 5.69 KB
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
class DialogManager {
constructor() {
this.currentLine;
this.currentDialogue;
this.lastState;
this.step = 0;
this.speaker = null;
this.index = 1;
this.answer = null;
this.options = ["No", "Yes"];
}
draw() {
//Rolling text
let stepDialogue = "";
if (this.step < this.currentLine.line.length) {
this.step += settings.textSpeed;
stepDialogue = this.currentLine.line.substring(0, this.step);
} else {
stepDialogue = this.currentLine.line;
}
push();
//Draw dialog box
strokeWeight(3);
stroke(0);
fill(180);
rect(0, height - 140, width - 1.5, 138.5)
//Draw reply box
if (this.currentLine.type == DIALOGUE_TYPE.QUESTION) {
let chars = 0;
let charWidth = 25
this.options.forEach(option => {
if (option.length > chars) {
chars = option.length;
}
});
let w = chars * charWidth + 10;
let h = 35 * this.options.length;
let x = width - 1.5 - w;
let y = height - 140 - h;
rect(x, y, w, h);
textSize(24)
fill(0);
noStroke();
for (let i = 0; i < this.options.length; i++) {
text(this.options[i], x + 27, y + h + 5 - i * 30);
}
this.drawSelector(x + 10, y + h - 28 - this.index * 30);
}
//Draw dialog
textSize(30);
textLeading(50);
noStroke();
fill(0);
text(stepDialogue, 25, height - 65, width - 25);
pop();
}
async load(dialogue) {
this.lastState = state;
this.currentDialogue = dialogue.map((x) => x);
this.currentLine = this.currentDialogue[0];
state = STATE.DIALOGUE;
await this.display();
}
async display() {
this.continue = false;
this.index = 1;
this.step = 0;
this.answer = null;
if (this.currentLine.type == DIALOGUE_TYPE.BATTLE) {
state = this.lastState;
return;
}
//Wait until rolling text is done
while (this.step < this.currentLine.line.length) {
await sleep(5);
}
//If this is timed dialogue wait until the timer is done
if (this.currentLine.type == DIALOGUE_TYPE.TIMED) {
await sleep(this.currentLine.time);
state = this.lastState;
return;
}
//Otherwise, wait until continue is pressed;
while (!this.continue) {
await sleep(1);
}
//Check if this line advances relationship with character
if (this.currentLine.gain) {
this.speaker.setQuest(parseInt(this.currentLine.gain));
}
//If this is a question and the is 'no', skip the following yes-reply in the array.
if (this.currentLine.type == DIALOGUE_TYPE.QUESTION && this.answer == "No") {
this.currentDialogue.shift();
} else if (this.currentLine.type == DIALOGUE_TYPE.REPLY_YES) {
//If this line is a yes-reply skip the following no-reply in the array.
this.currentDialogue.shift();
}
this.currentDialogue.shift();
//Check if there are more lines.
if (this.currentDialogue.length > 0) {
this.currentLine = this.currentDialogue[0];
this.display();
} else {
state = this.lastState;
this.speaker = null;
}
}
async speak(dialogue, speaker) {
this.speaker = speaker;
await this.load(dialogue);
}
async ask(newLine) {
if (this.options[1] == "Yes") {
this.index = 1;
} else {
this.index = 0;
}
this.continue = false;
this.answer = null;
this.lastState = state;
this.step = 0;
state = STATE.DIALOGUE;
this.currentLine = { type: DIALOGUE_TYPE.QUESTION, line: newLine };
while (this.step < this.currentLine.line.length) {
await sleep(5);
}
while (!this.continue) {
await sleep(1);
}
state = this.lastState;
this.options = ["No", "Yes"];
this.index = 1;
return this.answer;
}
inputA() {
//Check if rolling text is done.
if (this.step >= this.currentLine.line.length) {
this.continue = true;
if (this.currentLine.type == DIALOGUE_TYPE.QUESTION) {
this.answer = this.options[this.index];
}
} else {
this.step = this.currentLine.line.length;
}
}
inputB() {
if (this.step >= this.currentLine.line.length) {
if (this.currentLine.type == DIALOGUE_TYPE.QUESTION) {
if (this.options[1] == "Yes") {
this.index = 0;
this.draw();
this.inputA();
} else {
this.continue = true;
this.answer = false;
}
}
} else {
this.step = this.currentLine.line.length;
}
}
clear() {
this.currentLine = [];
}
drawSelector(x, y) {
push();
noStroke();
fill(255);
triangle(x, y, x + 10, y + 10, x, y + 20);
pop();
}
indexDown() {
if (this.index != 0 && this.step >= this.currentLine.line.length) {
this.index--
}
}
indexUp() {
if (this.index != this.options.length - 1 && this.step >= this.currentLine.line.length) {
this.index++;
}
}
}