TowerDefense/Globals/transition.gd
2025-09-06 00:07:24 +02:00

92 lines
2.3 KiB
GDScript

extends ColorRect
enum LOAD_STATE { NO_LOAD, BASIC_LOAD, MAP_LOAD, MAP_LOADED }
const worldManager = preload("res://Levels/world_manager.tscn")
@export var opaque := 0.0
@export var transparent := 11.0
var duration := .5
var target_scene_path : String
var mapScene : PackedScene
var loadState : LOAD_STATE = LOAD_STATE.NO_LOAD
var loading_status : int
var progress : Array[float]
var tween : Tween
func goto(scene_path : String, _loadState : LOAD_STATE = LOAD_STATE.BASIC_LOAD):
loadState = _loadState
target_scene_path = scene_path
fade_in()
tween.tween_callback(load_scene)
func gotoLevel(mapSceneToLoad : String) -> void :
goto(mapSceneToLoad, LOAD_STATE.MAP_LOAD)
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:
if loadState == LOAD_STATE.NO_LOAD:
return
# Update the status:
loading_status = ResourceLoader.load_threaded_get_status(target_scene_path, progress)
if loadState == LOAD_STATE.MAP_LOADED:
injectMap()
fade_out()
loadState = LOAD_STATE.NO_LOAD
# 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:
var scene = ResourceLoader.load_threaded_get(target_scene_path)
if loadState == LOAD_STATE.MAP_LOAD:
mapScene = scene
get_tree().change_scene_to_packed(worldManager)
loadState = LOAD_STATE.MAP_LOADED
if loadState == LOAD_STATE.BASIC_LOAD:
get_tree().change_scene_to_packed(scene)
loadState = LOAD_STATE.NO_LOAD
fade_out()
ResourceLoader.THREAD_LOAD_FAILED:
# Well some error happend:
printerr("Error. Could not load Resource")
loadState = LOAD_STATE.NO_LOAD
func injectMap() -> void:
var node : Node = get_tree().current_scene
if node is WorldManager:
node.addMap(mapScene)