crabs-game/scripts/DialogueBox.gd

108 lines
2.4 KiB
GDScript3
Raw Normal View History

2020-08-16 08:50:04 +02:00
extends Control
class_name DialogueBox
signal end
2020-08-17 10:45:39 +02:00
signal update
2020-08-16 08:50:04 +02:00
export (NodePath) var name_label_path
onready var name_label = get_node(name_label_path) as RichTextLabel
2020-08-16 08:50:04 +02:00
export (NodePath) var text_label_path
onready var text_label = get_node(text_label_path) as RichTextLabel
2020-08-16 08:50:04 +02:00
export (NodePath) var expression_node_path
onready var expression_node = get_node(expression_node_path) as RichTextLabel
2020-08-16 08:50:04 +02:00
onready var music_player = get_node('/root/MusicPlayer') as MusicPlayer
2020-08-18 11:05:27 +02:00
var dialogue : Array
var index : int
var character_name : String = ""
var text : String = ""
var expression : String = ""
var waiting = false
var auto_advance = false
2020-08-16 08:50:04 +02:00
func _ready():
assert(name_label != null)
assert(text_label != null)
assert(music_player != null)
2020-08-16 08:50:04 +02:00
2020-08-18 11:05:27 +02:00
func start(_dialogue : Array):
dialogue = _dialogue
index = 0
update()
2020-08-16 08:50:04 +02:00
func _input(event):
if !waiting and event.is_action_pressed("ui_accept"):
if text_label.visible_characters < text_label.text.length():
text_label.visible_characters = text_label.text.length()
2020-08-16 10:41:14 +02:00
else:
next()
2020-08-16 08:50:04 +02:00
func next() -> void:
index += 1
if index >= dialogue.size():
2020-08-16 08:50:04 +02:00
hide()
emit_signal('end')
return
update()
2020-08-16 08:50:04 +02:00
func update() -> void:
var line = dialogue[index]
# Self properties
character_name = line.get('name', '')
text = line.get('text', '')
expression = line.get('expression', '')
2020-08-18 11:05:27 +02:00
# Flow
waiting = line.get('wait', false)
auto_advance = line.get('auto', false)
2020-08-18 11:05:27 +02:00
# Timer
var timer = line.get('timer', 0)
if timer > 0:
waiting = true
2020-08-18 11:05:27 +02:00
# Audio
var audio = line.get('audio', null)
if audio != null:
if audio == '':
music_player.fade_out()
else:
music_player.play(audio)
2020-08-18 11:05:27 +02:00
# UI
name_label.bbcode_text = character_name
name_label.rect_min_size.x = name_label.get_font('normal_font').get_string_size(name_label.text).x
text_label.bbcode_text = text
if text == "":
hide()
else:
show()
2020-08-16 10:41:14 +02:00
2020-08-17 10:45:39 +02:00
emit_signal('update')
2020-08-16 10:41:14 +02:00
animate_text()
if timer > 0:
yield(get_tree().create_timer(timer), 'timeout')
stop_waiting()
2020-08-16 10:41:14 +02:00
func animate_text():
var animated = index
text_label.visible_characters = 0
while animated == index && text_label.visible_characters < text_label.text.length():
if text_label.text[text_label.visible_characters] == ' ':
text_label.visible_characters += 1
text_label.visible_characters += 1
2020-08-16 10:41:14 +02:00
yield(get_tree().create_timer(0.05), 'timeout')
func stop_waiting():
waiting = false
if auto_advance:
next()