crabs-game/scripts/DialogueBox.gd

59 lines
1.3 KiB
GDScript

extends Control
class_name DialogueBox
signal end
export (NodePath) var title_path
onready var title = get_node(title_path) as RichTextLabel
export (NodePath) var text_path
onready var text = get_node(text_path) as RichTextLabel
export (NodePath) var expression_path
onready var expression = get_node(expression_path) as RichTextLabel
var _dialogue : Array
var _index : int
func _ready():
assert(title != null)
assert(text != null)
func start(dialogue : Array):
_dialogue = dialogue
_index = 0
_update()
show()
func _input(event):
if event.is_action_pressed("ui_accept"):
if text.visible_characters < text.text.length():
text.visible_characters = text.text.length()
else:
next()
func next() -> void:
_index += 1
if _index >= _dialogue.size():
hide()
emit_signal('end')
return
_update()
func _update() -> void:
title.text = _dialogue[_index].name
title.rect_min_size.x = title.get_font('normal_font').get_string_size(title.text).x
text.text = _dialogue[_index].text
#expression = _dialogue[_index].expression
animate_text()
func animate_text():
var animated = _index
text.visible_characters = 0
while animated == _index && text.visible_characters < text.text.length():
if text.text[text.visible_characters] == ' ':
text.visible_characters += 1
text.visible_characters += 1
yield(get_tree().create_timer(0.05), 'timeout')