Created
May 17, 2021 20:17
-
-
Save slapin/caabea4d589098670ab31f9c25e27451 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NPCDialogue: | |
var db | |
var dialogue_sleeping = {} | |
var rnd | |
func _init(): | |
var fd = File.new() | |
fd.open("res://data/tagged_lines.json", File.READ) | |
var result = JSON.parse(fd.get_as_text()) | |
fd.close() | |
db = result.result | |
assert(db) | |
rnd = RandomNumberGenerator.new() | |
rnd.randomize() | |
# dialogue_sleeping = [ | |
# { | |
# "type": "text", | |
# "text": "{stat:firstname} is sleeping..." | |
# }, | |
# { | |
# "type": "question", | |
# "text": "What now?", | |
# "children": [ | |
# ["Prank", "prank"], | |
# ["", "prank"], | |
# ] | |
# }, | |
# ] | |
# var dialogue_normal = [ | |
# { | |
# "type": "text", | |
# "text": "{stat:firstname} is sleeping..." | |
# }, | |
# ] | |
func make_text_event(txt): | |
var event = { | |
'event_id':'dialogic_001', | |
"character": "", | |
"portrait":"", | |
"text": txt | |
} | |
return event | |
func make_question_event(txt): | |
var event = { | |
"character": "", | |
"event_id": "dialogic_010", | |
"options": [], | |
"portrait": "", | |
"question": txt | |
} | |
return event | |
func make_choice_event(txt): | |
var event = { | |
"choice": txt, | |
"condition": "", | |
"definition": "", | |
"event_id": "dialogic_011", | |
"value": "" | |
} | |
return event | |
func make_signal_event(txt): | |
var event = { | |
"emit_signal": "success", | |
"event_id": "dialogic_040" | |
} | |
return event | |
func make_end_choice_event(): | |
var event = { | |
"event_id": "dialogic_013" | |
} | |
return event | |
func get_tagged_lines(category, tags): | |
var all_lines = db[category] | |
var result = [] | |
var mtags = [] | |
for e in tags: | |
if !e.begins_with("!"): | |
mtags.push_back(e) | |
for e in all_lines: | |
var ltags = e[2] | |
if ltags.size() == 0: | |
result.push_back([e[0], e[1]]) | |
continue | |
var ok = true | |
for t in ltags: | |
if "!"+t in tags: | |
ok = false | |
break | |
if !ok: | |
continue | |
ok = false | |
for t in mtags: | |
if t in tags: | |
ok = true | |
break | |
if mtags.size() == 0: | |
ok = true | |
if ok: | |
result.push_back([e[0], e[1]]) | |
return result | |
func start(npc): | |
if !npc.is_in_group("character"): | |
return | |
if controls.is_gui: | |
return | |
var events = [] | |
var animation_tree = npc.get_meta("animation_tree") | |
assert(animation_tree) | |
var cstate = animation_tree["parameters/state/playback"].get_current_node() | |
var cdata = npc.get_meta("character_data") | |
var tags = [] | |
if cdata.sex == "female": | |
tags.push_back("!male") | |
else: | |
tags.push_back("!female") | |
# var src | |
if cstate == "sleeping": | |
tags.push_back("sleeping") | |
# src = dialogue_sleeping.duplicate() | |
else: | |
tags.push_back("!sleeping") | |
# src = dialogue_normal.duplicate() | |
var lines = get_tagged_lines("talk", tags) | |
assert(lines.size() > 0) | |
var line = lines[rnd.randi() % lines.size()] | |
events.push_back(make_text_event(line[0])) | |
# lines = get_tagged_lines("talk_options", tags) | |
# assert(lines.size() > 0) | |
# for e in lines: | |
# events.push_back(make_choice_event(e[0])) | |
# break | |
# if e[1].begins_with("signal:"): | |
# events.push_back(make_signal_event(e[1].replace("singal:", ""))) | |
events.push_back(make_end_choice_event()) | |
print(events) | |
var dlg = Dialogic.start("") | |
dlg.dialog_node.dialog_script = { | |
"events": events, | |
"metadata": { | |
"name": "", | |
"file": "" | |
} | |
} | |
controls.is_gui = true | |
scenario.add_child(dlg) | |
dlg.connect("timeline_end", self, "finish_dialogue", [dlg]) | |
dlg.connect("dialogic_signal", self, "signal_process", [dlg]) | |
func finish_dialogue(d, dialogue): | |
breakpoint | |
dialogue.queue_free() | |
controls.is_gui = false | |
func signal_process(s, d): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment