109 lines
3.0 KiB
GDScript
109 lines
3.0 KiB
GDScript
extends Control
|
|
|
|
@export var tips: Array[String] = [
|
|
"Use your cards wisely!",
|
|
"Plan your moves ahead.",
|
|
"Watch out for obstacles!",
|
|
"Combine cards for powerful effects."
|
|
]
|
|
|
|
var is_loading: bool = false
|
|
var path: String = ""
|
|
var progress: Array[float] = []
|
|
|
|
@onready var progress_bar = $progress_bar
|
|
@onready var tip_value = $Control/tips/tip_value
|
|
@onready var scene_name_label = $Control/scene_name/name_of_load_scene
|
|
@onready var content_control = $Control
|
|
|
|
func _ready():
|
|
set_process(false)
|
|
|
|
var load_start_time = 0.0
|
|
var stuck_time = 0.0
|
|
var last_progress = 0.0
|
|
|
|
func _process(delta):
|
|
if is_loading:
|
|
var status = ResourceLoader.load_threaded_get_status(path, progress)
|
|
match status:
|
|
ResourceLoader.THREAD_LOAD_IN_PROGRESS:
|
|
if progress.size() > 0:
|
|
var actual_progress = progress[0] * 100
|
|
|
|
# If progress is actually advancing, use it and reset stuck timer
|
|
if actual_progress > last_progress:
|
|
progress_bar.value = actual_progress
|
|
last_progress = actual_progress
|
|
stuck_time = 0.0
|
|
else:
|
|
# Progress is stuck, increment stuck timer
|
|
stuck_time += delta
|
|
|
|
# If stuck for more than 0.5 seconds, start simulating progress
|
|
if stuck_time > 0.5:
|
|
# Calculate how much more progress needed
|
|
var remaining_percent = 99.0 - progress_bar.value
|
|
|
|
# Adjust increment speed based on how much progress we've already made
|
|
# Slower at the beginning, faster near the end
|
|
var increment_speed = 5.0 + (progress_bar.value / 20.0)
|
|
|
|
# Apply increment
|
|
progress_bar.value = min(progress_bar.value + increment_speed * delta, 99.0)
|
|
|
|
ResourceLoader.THREAD_LOAD_LOADED:
|
|
progress_bar.value = 100
|
|
call_deferred("_handle_load_complete")
|
|
|
|
ResourceLoader.THREAD_LOAD_FAILED:
|
|
print("Loading failed: ", path)
|
|
is_loading = false
|
|
set_process(false)
|
|
|
|
func _handle_load_complete():
|
|
var resource = ResourceLoader.load_threaded_get(path)
|
|
if resource:
|
|
change_scene(resource)
|
|
else:
|
|
print("Failed to get loaded resource")
|
|
|
|
set_process(false)
|
|
is_loading = false
|
|
|
|
func change_scene(resource: PackedScene):
|
|
print("Loading complete. Switching scene...")
|
|
# The Lobby (parent) should be removed by the caller or we handle it here if we are the root.
|
|
# But in this architecture, the Lobby is a child of root or MainMenu?
|
|
# Actually, usually we use get_tree().change_scene_to_packed(resource)
|
|
|
|
get_tree().change_scene_to_packed(resource)
|
|
|
|
# Clean up self (Loading Screen)
|
|
queue_free()
|
|
|
|
|
|
func load_level(_path: String):
|
|
print("Starting load for: ", _path)
|
|
path = _path
|
|
show()
|
|
content_control.show()
|
|
|
|
if scene_name_label:
|
|
scene_name_label.text = "Loading Game..." # _path.get_file().get_basename()
|
|
|
|
if tips.size() > 0:
|
|
$Control/tips.show()
|
|
tip_value.text = tips.pick_random()
|
|
else:
|
|
$Control/tips.hide()
|
|
|
|
# 1. Request Load
|
|
var error = ResourceLoader.load_threaded_request(path)
|
|
if error == OK:
|
|
is_loading = true
|
|
set_process(true)
|
|
else:
|
|
print("Failed to start loading: ", error)
|
|
# Fallback?
|