TowerDefense/Globals/transition.gd

93 lines
2.3 KiB
GDScript3
Raw Normal View History

2025-08-21 16:06:02 +02:00
extends ColorRect
2025-09-01 13:52:14 +02:00
enum LOAD_STATE { NO_LOAD, BASIC_LOAD, MAP_LOAD, MAP_LOADED }
const worldManager = preload("res://Levels/world_manager.tscn")
2025-08-21 16:06:02 +02:00
@export var opaque := 0.0
@export var transparent := 11.0
2025-09-06 00:05:22 +02:00
var duration := .5
2025-08-21 16:06:02 +02:00
var target_scene_path : String
2025-09-01 13:52:14 +02:00
var mapScene : PackedScene
var loadState : LOAD_STATE = LOAD_STATE.NO_LOAD
2025-08-21 16:06:02 +02:00
var loading_status : int
var progress : Array[float]
var tween : Tween
2025-09-01 13:52:14 +02:00
func goto(scene_path : String, _loadState : LOAD_STATE = LOAD_STATE.BASIC_LOAD):
loadState = _loadState
2025-08-21 16:06:02 +02:00
target_scene_path = scene_path
2025-08-21 16:06:02 +02:00
fade_in()
tween.tween_callback(load_scene)
2025-09-01 13:52:14 +02:00
func gotoLevel(mapSceneToLoad : String) -> void :
goto(mapSceneToLoad, LOAD_STATE.MAP_LOAD)
2025-08-21 16:06:02 +02:00
func load_scene() -> void:
ResourceLoader.load_threaded_request(target_scene_path) #Fetch loading status and loading scene
2025-09-01 13:52:14 +02:00
2025-08-21 16:06:02 +02:00
func animate() -> void:
if tween:
tween.kill() # Abort the previous animation.
tween = create_tween()
2025-09-01 13:52:14 +02:00
2025-08-21 16:06:02 +02:00
func fade_in() -> void:
animate()
tween.tween_method(shader_transition, transparent, opaque, duration)
2025-09-01 13:52:14 +02:00
2025-08-21 16:06:02 +02:00
func fade_out() -> void:
animate()
tween.tween_method(shader_transition, opaque, transparent, duration)
2025-09-01 13:52:14 +02:00
2025-08-21 16:06:02 +02:00
func shader_transition(value) -> void:
material.set_shader_parameter("progress", value)
2025-09-01 13:52:14 +02:00
2025-08-21 16:06:02 +02:00
func _process(_delta: float) -> void:
2025-09-01 13:52:14 +02:00
if loadState == LOAD_STATE.NO_LOAD:
return
2025-08-21 16:06:02 +02:00
# Update the status:
loading_status = ResourceLoader.load_threaded_get_status(target_scene_path, progress)
2025-09-01 13:52:14 +02:00
if loadState == LOAD_STATE.MAP_LOADED:
injectMap()
fade_out()
loadState = LOAD_STATE.NO_LOAD
2025-08-21 16:06:02 +02:00
# 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:
2025-09-01 13:52:14 +02:00
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()
2025-08-21 16:06:02 +02:00
ResourceLoader.THREAD_LOAD_FAILED:
# Well some error happend:
printerr("Error. Could not load Resource")
2025-09-01 13:52:14 +02:00
loadState = LOAD_STATE.NO_LOAD
func injectMap() -> void:
var node : Node = get_tree().current_scene
if node is WorldManager:
node.addMap(mapScene)