-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonologueProcess.gd
343 lines (275 loc) · 9.62 KB
/
MonologueProcess.gd
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
extends Control
class_name MonologueProcess
var dir_path
var root_node_id: String
var node_list: Array
var characters: Array
var variables: Array
var events: Array
var next_id
var fallback_id
var rng = RandomNumberGenerator.new()
signal monologue_node_reached(raw_node: Dictionary)
signal monologue_sentence(sentence: String, speaker: String, speaker_name: String)
signal monologue_new_choice(options: Array[Dictionary])
signal monologue_option_choosed(raw_option: Dictionary)
signal monologue_event_triggered(raw_event: Dictionary)
signal monologue_end(raw_end)
signal monologue_play_audio(path: String, stream)
signal monologue_update_background(path: String, background)
signal monologue_custom_action(raw_action: Dictionary)
func _init():
print("[INFO] Monologue Process initiated")
monologue_node_reached.connect(_process_node)
monologue_end.connect(_default_end_process)
func load_dialogue(dialogue_name, custom_start_point = -1):
var path = dialogue_name + ".json"
dir_path = path.replace("/", "\\").split("\\")
dir_path.remove_at(len(dir_path)-1)
dir_path = "\\".join(dir_path)
assert(FileAccess.file_exists(path), "Can't find dialogs file")
var file = FileAccess.open(path, FileAccess.READ)
var data: Dictionary = JSON.parse_string(file.get_as_text())
assert(data.has("RootNodeID"), "Invalid json file, can't find 'RootNodeID'")
assert(data.has("ListNodes"), "Invalid json file, can't find 'ListNodes'")
assert(data.has("Characters"), "Invalid json file, can't find 'Characters'")
assert(data.has("Variables"), "Invalid json file, can't find 'Variables'")
root_node_id = data.get("RootNodeID")
node_list = data.get("ListNodes")
characters = data.get("Characters")
variables = data.get("Variables")
events = node_list.filter(func(n): return n.get("$type") == "NodeEvent")
next_id = custom_start_point if custom_start_point else root_node_id
print("[INFO] Dialogue " + path + " loaded")
func next():
# Check for an event
for event in events:
var condition = event.get("Condition")
var variable = get_variable(condition.get("Variable"))
var v_val = variable.get("Value")
var c_val = condition.get("Value")
if variable == null:
print("[WARNING] Can't find variable. Skipping")
next()
return
var condition_pass: bool = false
match condition.get("Operator"):
"==":
condition_pass = v_val == c_val
">=":
condition_pass = v_val >= c_val
"<=":
condition_pass = v_val <= c_val
"!=":
condition_pass = v_val != c_val
if condition_pass:
monologue_event_triggered.emit(event)
fallback_id = next_id
next_id = event.get("NextID")
events.erase(event)
if next_id is float and next_id == -1:
if fallback_id:
next_id = fallback_id
fallback_id = null
else:
monologue_end.emit(null)
var node = find_node_from_id(next_id)
if node:
monologue_node_reached.emit(node)
func _process_node(node: Dictionary):
match node.get("$type"):
"NodeRoot":
next_id = node.get("NextID")
next()
return
"NodeBridgeIn":
next_id = node.get("NextID")
next()
return
"NodeBridgeOut":
next_id = node.get("NextID")
next()
return
"NodeSentence":
next_id = node.get("NextID")
var processed_sentence = process_conditional_text(node.get("Sentence"))
var speaker_name = node.get("DisplaySpeakerName") if node.get("DisplaySpeakerName") else get_speaker(node.get("SpeakerID"))
monologue_sentence.emit(
processed_sentence,
get_speaker(node.get("SpeakerID")),
speaker_name
)
"NodeChoice":
var options: Array = []
for option_id in node.get("OptionsID"):
var option = find_node_from_id(option_id)
if not option:
print("[WARNING] Can't find option with id: " + option_id)
continue
if option.get("Enable") == false:
continue
options.append(option)
monologue_new_choice.emit(options)
"NodeDiceRoll":
var roll = rng.randi_range(0, 100)
if roll <= node.get("Target"):
next_id = node.get("PassID")
else:
next_id = node.get("FailID")
next()
return
"NodeAction":
next_id = node.get("NextID")
var raw_action = node.get("Action")
match raw_action.get("$type"):
"ActionOption":
var option: Dictionary = find_node_from_id(raw_action.get("OptionID"))
if option == null:
print("[WARNING] Can't find option. Skipping")
next()
return
option["Enable"] = raw_action.get("Value")
"ActionVariable":
var variable = get_variable(raw_action.get("Variable"))
if variable == null:
print("[WARNING] Can't find variable. Skipping")
next()
return
match raw_action.get("Operator"):
"=":
variable["Value"] = raw_action.get("Value")
"+":
variable["Value"] += raw_action.get("Value")
"-":
variable["Value"] -= raw_action.get("Value")
"*":
variable["Value"] *= raw_action.get("Value")
"/":
if raw_action.get("Value") != 0:
variable["Value"] /= raw_action.get("Value")
else:
print("[WARNING] Can't divide by value 0")
"ActionCustom":
match raw_action.get("CustomType"):
"PlayAudio":
var full_path = dir_path + "\\assets\\audios\\" + raw_action.get("Value")
var sound = null
if FileAccess.file_exists(full_path):
var track = SfxLoader.load_track(full_path, raw_action.get("Pitch"), raw_action.get("Volume"))
track.loop = raw_action.get("Loop")
monologue_play_audio.emit(raw_action.get("Value"), sound)
"UpdateBackground":
var bg = Image.new()
var texture = null
var full_path = dir_path + "\\assets\\backgrounds\\" + raw_action.get("Value")
if FileAccess.file_exists(full_path):
var err = bg.load(full_path)
if err != OK:
print("[WARNING] Failed to load background (" + raw_action.get("Value") + ")")
else:
texture = ImageTexture.create_from_image(bg)
monologue_update_background.emit(raw_action.get("Value"), texture)
"Other":
monologue_custom_action.emit(raw_action)
"ActionTimer":
var time_to_wait = raw_action.get("Value", 0.0)
if not is_inside_tree():
print("[WARNING] MonologueProcess is not inside tree and can't create a timer...")
else:
await get_tree().create_timer(time_to_wait).timeout
next()
return
"NodeCondition":
var condition = node.get("Condition")
var variable = get_variable(condition.get("Variable"))
var if_nid = node.get("IfNextID")
var else_nid = node.get("ElseNextID")
var v_val = variable.get("Value")
var c_val = condition.get("Value")
next_id = if_nid
if variable == null:
print("[WARNING] Can't find variable. Skipping")
next()
return
match condition.get("Operator"):
"==":
next_id = if_nid if v_val == c_val else else_nid
">=":
next_id = if_nid if v_val >= c_val else else_nid
"<=":
next_id = if_nid if v_val <= c_val else else_nid
"!=":
next_id = if_nid if v_val != c_val else else_nid
next()
return
"NodeEndPath":
monologue_end.emit(node)
func find_node_from_id(id):
if not id is String:
return null
var nodes = node_list.filter(func (node): return node.get("ID") == id)
if nodes.size() <= 0:
return null
return nodes[0]
func get_speaker(id: int) -> String:
var speaker = characters.filter(func (c): return c["ID"] == id)
if speaker.size() <= 0:
return ""
return speaker[0]["Reference"]
func get_variable(var_name: String):
var variable = variables.filter(func (v): return v.get("Name") == var_name)
if variable.size() <= 0:
return null
return variable[0]
func process_conditional_text(text: String) -> String:
var regex = RegEx.new()
regex.compile("(?<=\\{{)(.*?)(?=\\}})")
var results = regex.search_all(text)
if not results:
return text
for r in results:
var condition: String = r.get_string()
var original_condition: String = "{{" + condition + "}}"
condition = condition.strip_edges(true, true)
var if_position = condition.find("if")
var then_position = condition.find("then")
var else_position = condition.find("else")
var var_name = condition.substr(if_position+3, then_position - (if_position+3)).strip_edges(true, true)
var then_name = condition.substr(then_position+5, else_position - (then_position+5)).strip_edges(true, true).trim_prefix("\"").trim_suffix("\"")
var else_name = condition.substr(else_position+5).strip_edges(true, true).trim_prefix("\"").trim_suffix("\"")
var variable = get_variable(var_name)
if not variable:
print("[ERROR] Can't find the variable " + var_name)
if variable.get("Type") != "Boolean":
print("[ERROR] The variable can only be of type Boolean (not " + variable.get("Type") + ")")
return text
if variable.get("Value") == true:
text = text.replace(original_condition, then_name)
continue
text = text.replace(original_condition, else_name)
return text
func _default_end_process(raw_end):
if not raw_end:
return
var next_story = raw_end.get("NextStoryName")
# Search in the same directory if the next story is in here.
var dir = DirAccess.open(dir_path)
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if not dir.current_is_dir() and file_name == next_story + ".json":
load_dialogue(dir_path + "/" + next_story)
return true
file_name = dir.get_next()
return dir != null
func option_selected(option):
monologue_option_choosed.emit(option)
if option == null:
print("[CRITICAL] Can't find option. Unexpected exit.")
monologue_end.emit(null)
return
next_id = option.get("NextID")
if option.get("OneShot") == true:
option["Enable"] = false
next()