TowerDefense/Globals/transition.gd

57 lines
1.4 KiB
GDScript3
Raw Normal View History

2025-08-21 16:06:02 +02:00
extends ColorRect
@export var opaque := 0.0
@export var transparent := 11.0
var duration := 1.0
var target_scene_path : String
var loading_status : int
var progress : Array[float]
var tween : Tween
func goto(scene_path : String):
target_scene_path = scene_path
fade_in()
tween.tween_callback(load_scene)
func load_scene() -> void:
ResourceLoader.load_threaded_request(target_scene_path) #Fetch loading status and loading scene
func animate() -> void:
if tween:
tween.kill() # Abort the previous animation.
tween = create_tween()
func fade_in() -> void:
animate()
tween.tween_method(shader_transition, transparent, opaque, duration)
func fade_out() -> void:
animate()
tween.tween_method(shader_transition, opaque, transparent, duration)
func shader_transition(value) -> void:
material.set_shader_parameter("progress", value)
func _process(_delta: float) -> void:
# Update the status:
loading_status = ResourceLoader.load_threaded_get_status(target_scene_path, progress)
# Check the loading status:
match loading_status:
ResourceLoader.THREAD_LOAD_IN_PROGRESS:
pass
ResourceLoader.THREAD_LOAD_LOADED:
# When done loading, change to the target scene:
get_tree().change_scene_to_packed(ResourceLoader.load_threaded_get(target_scene_path))
fade_out()
pass
ResourceLoader.THREAD_LOAD_FAILED:
# Well some error happend:
printerr("Error. Could not load Resource")