necra-prototypes/dialog.gd
Antonio Dell'Annunziata cd35a9b1ac
All checks were successful
/ test (push) Successful in 2s
Fix operation bugs
2025-03-07 06:31:12 +01:00

60 lines
2.1 KiB
GDScript

class_name Dialog
extends Node2D
signal dialog_finished
var text_box_label: RichTextLabel
const SPEED_SLOW = 5.0
const SPEED_NORMAL = 10.0
const SPEED_FAST = 15.0
var dialog_text: Array[Dictionary] = [
{speaker = "?", text = "...", speed = SPEED_SLOW},
{speaker = "Necra", text = "Hello?"},
{speaker = "?", text = "!", speed = SPEED_SLOW},
{speaker = "Necra", text = "Can... can I help you?", speed = SPEED_SLOW},
{speaker = "?", text = "EEEEEEHHHsh...", speed = SPEED_SLOW},
{speaker = "?", text = "*Cough* Sorry, my vocal cords are rusty. Yes.", speed = SPEED_FAST},
{speaker = "Bob", text = "I am Bob. There is a bat in my chest. Can you remove it?"},
{speaker = "Necra", text = "How did that... on second thought, nevermind."},
]
var current_line = 0
func _ready():
text_box_label.text = ""
$Undead.modulate = Color(1.0,1.0,1.0,0)
next_line()
func _input(event):
if event is InputEventKey and event.key_label == KEY_SPACE and event.is_released():
next_line()
var tween: Tween
func next_line():
if not text_box_label.visible: return
if current_line >= dialog_text.size():
dialog_finished.emit()
text_box_label.hide()
return
if current_line == 0:
create_tween().tween_property($Undead, "modulate", Color(1.0,1.0,1.0, .1), 1)
if current_line == 2:
create_tween().tween_property($Undead, "modulate", Color(1.0,1.0,1.0, .2), 1)
if current_line == 4:
create_tween().tween_property($Undead, "modulate", Color(1.0,1.0,1.0, .5), 1)
if current_line == 5:
create_tween().tween_property($Undead, "modulate", Color(1.0,1.0,1.0, 1), .2)
var next = dialog_text[current_line]
text_box_label.text = "%s: %s" % [next["speaker"], next["text"]]
text_box_label.visible_characters = next["speaker"].length() + 2
var remaining_characters = text_box_label.text.length() - text_box_label.visible_characters
var tween_duration = remaining_characters / (next["speed"] if next.has("speed") else SPEED_NORMAL)
if tween:
tween.kill()
tween = create_tween()
tween.tween_property(text_box_label, "visible_characters", text_box_label.text.length(), tween_duration)
current_line += 1