glitch-in-the-system/scripts/TypeLabel.gd

53 lines
1.3 KiB
GDScript3
Raw Normal View History

2019-09-16 08:07:41 +02:00
extends Label
2023-02-01 10:43:50 +01:00
@export var time_to_show = 4.0
@export var fade_after_show = true
#Hide all charafcters checked start
2019-09-16 08:07:41 +02:00
func _ready():
show_text(time_to_show)
visible_characters = 0
var timePassed = 0 #Current amount of time spend displaying te
const CHECK_LENGTH = 0.05 #Interval to check for updates
var timeNeeded = 0 #Time that it takes to display the text
var timer #Timer object
var fadeTimer #Timer object
func show_text(seconds):
timeNeeded = seconds
timePassed = 0
if(!timer):
timer = Timer.new()
add_child(timer)
2023-02-01 10:43:50 +01:00
timer.connect("timeout",Callable(self,"on_timeout"))
2019-09-16 08:07:41 +02:00
timer.start(CHECK_LENGTH)
func fade_text(seconds):
timeNeeded = seconds
timePassed = 0
if(!fadeTimer):
fadeTimer = Timer.new()
add_child(fadeTimer)
2023-02-01 10:43:50 +01:00
fadeTimer.connect("timeout",Callable(self,"on_fade_timeout"))
2019-09-16 08:07:41 +02:00
fadeTimer.start(CHECK_LENGTH)
func on_fade_timeout():
timePassed += CHECK_LENGTH
modulate.a = 1 - (timePassed/timeNeeded)
if(timePassed >= timeNeeded):
fadeTimer.queue_free()
else:
fadeTimer.start(CHECK_LENGTH)
pass
func on_timeout():
timePassed += CHECK_LENGTH
visible_characters = (timePassed/timeNeeded) * get_total_character_count()
if(timePassed >= timeNeeded):
timer.queue_free()
if(fade_after_show):
fade_text(2)
else:
timer.start(CHECK_LENGTH)
pass