145 lines
3.5 KiB
GDScript3
145 lines
3.5 KiB
GDScript3
|
extends Base
|
||
|
|
||
|
var used_lines = []
|
||
|
var mouse_over = false
|
||
|
|
||
|
@onready var text_box = $"TextBox"
|
||
|
@onready var text_box_text = $"TextBox/Graphics/TextBoxText"
|
||
|
@onready var text_box_name = $"TextBox/Graphics/TextBoxName"
|
||
|
@onready var text_box_image = $"TextBox/Graphics/TextBoxAvatar"
|
||
|
@onready var minimize_handler = $"TextBox/MinimizeHandler"
|
||
|
|
||
|
var pop_tween
|
||
|
var last_lines = {}
|
||
|
|
||
|
func _spawned():
|
||
|
text_box.modulate = Color.TRANSPARENT
|
||
|
text_box.scale = Vector2(0, 0)
|
||
|
text_box.position.y = 360
|
||
|
|
||
|
minimize_handler.clicked.connect(_on_clicked)
|
||
|
|
||
|
if _triggerer:
|
||
|
_triggerer.listen("any", _on_trigger)
|
||
|
|
||
|
|
||
|
func _on_trigger(data: Dictionary) -> void:
|
||
|
if has_trigger(data.trigger):
|
||
|
trigger_dialogue(data.trigger)
|
||
|
|
||
|
|
||
|
func _on_clicked():
|
||
|
if pop_tween:
|
||
|
pop_tween.kill()
|
||
|
|
||
|
pop_tween = create_tween()
|
||
|
pop_tween.set_ease(Tween.EASE_IN)
|
||
|
pop_tween.set_trans(Tween.TRANS_BACK)
|
||
|
pop_tween.set_parallel()
|
||
|
pop_tween.tween_property(text_box, "modulate", Color.TRANSPARENT, 0.5)
|
||
|
pop_tween.tween_property(text_box, "scale", Vector2(0, 0), 0.5)
|
||
|
pop_tween.tween_property(text_box, "position:y", 360, 0.5)
|
||
|
pop_tween.chain()
|
||
|
|
||
|
func get_priority_from_string(string: String):
|
||
|
match string:
|
||
|
"low": return 1
|
||
|
"medium": return 2
|
||
|
"high": return 3
|
||
|
|
||
|
func trigger_dialogue(trigger: String):
|
||
|
_info("Dialogue trigger %s triggered" % [trigger])
|
||
|
var dialogue = get_dialogue(trigger)
|
||
|
|
||
|
if not dialogue:
|
||
|
return
|
||
|
|
||
|
var lines = dialogue.values()
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
if pop_tween:
|
||
|
pop_tween.kill()
|
||
|
|
||
|
#var person = Data.data.people[lines[0].character]
|
||
|
|
||
|
text_box_text.text = lines[0].text
|
||
|
#text_box_image.texture = Data.data.images[lines[0].character]
|
||
|
#text_box_name.text = "[center]%s" % [person.short]
|
||
|
|
||
|
pop_tween = create_tween()
|
||
|
pop_tween.set_ease(Tween.EASE_OUT)
|
||
|
pop_tween.set_trans(Tween.TRANS_BACK)
|
||
|
pop_tween.set_parallel()
|
||
|
pop_tween.tween_property(text_box, "modulate", Color.WHITE, 0.5)
|
||
|
pop_tween.tween_property(text_box, "scale", Vector2(1, 1), 0.5)
|
||
|
pop_tween.tween_property(text_box, "position:y", 280, 0.5)
|
||
|
pop_tween.chain()
|
||
|
|
||
|
#get_parent().move_child(self, get_parent().get_child_count() - 1)
|
||
|
|
||
|
|
||
|
func has_trigger(trigger: String):
|
||
|
var dialogue_files = Data.data.dialogue.values()
|
||
|
|
||
|
for file in dialogue_files:
|
||
|
for key in file:
|
||
|
if file[key].has("trigger"):
|
||
|
if file[key].trigger == trigger:
|
||
|
return true
|
||
|
|
||
|
return false
|
||
|
|
||
|
|
||
|
func get_dialogue(trigger: String):
|
||
|
var dialogue_files = Data.data.dialogue.values()
|
||
|
|
||
|
var combined_object = {}
|
||
|
|
||
|
for file in dialogue_files:
|
||
|
for key in file:
|
||
|
combined_object[key] = file[key]
|
||
|
combined_object[key].key = key
|
||
|
|
||
|
var lines = combined_object.values()
|
||
|
var trigger_lines = lines.filter(func(x): return x.trigger == trigger)
|
||
|
|
||
|
trigger_lines.sort_custom(func(a, b):
|
||
|
var apriority = a.rules.priority if a.rules.has("priority") else "medium"
|
||
|
var bpriority = b.rules.priority if b.rules.has("priority") else "medium"
|
||
|
|
||
|
if get_priority_from_string(apriority) == get_priority_from_string(bpriority):
|
||
|
return randf() < 0.5
|
||
|
|
||
|
return get_priority_from_string(apriority) > get_priority_from_string(bpriority)
|
||
|
)
|
||
|
|
||
|
for line in trigger_lines:
|
||
|
var invalid = false
|
||
|
|
||
|
if last_lines.has(line.trigger) and last_lines[line.trigger] == line.key:
|
||
|
invalid = true
|
||
|
|
||
|
for rule in line.rules:
|
||
|
if rule == "priority":
|
||
|
continue
|
||
|
elif rule == "unique":
|
||
|
if used_lines.has(line.key):
|
||
|
invalid = true
|
||
|
break
|
||
|
else:
|
||
|
var value = Persister.get_value(rule)
|
||
|
if value != line.rules[rule]:
|
||
|
invalid = true
|
||
|
break
|
||
|
|
||
|
if invalid:
|
||
|
continue
|
||
|
|
||
|
last_lines[line.trigger] = line.key
|
||
|
used_lines.push_back(line.key)
|
||
|
return line.lines
|
||
|
|
||
|
return null
|