Add loading screen between main menu and overworld
This commit is contained in:
parent
ae885259ec
commit
fbabe444c7
73
game/ui/loading_screen/loading_screen.gd
Normal file
73
game/ui/loading_screen/loading_screen.gd
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
extends CanvasLayer
|
||||||
|
|
||||||
|
@onready var transition_rect : ColorRect = $TransitionRect
|
||||||
|
@onready var status_label : Label = %StatusLabel
|
||||||
|
@onready var progress_bar : ProgressBar = %ProgressBar
|
||||||
|
|
||||||
|
const FADE_DURATION := 0.6
|
||||||
|
const MIN_DISPLAY_TIME := 0.4 # évite le flash sur les chargements instantanés
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
transition_rect.visible = false
|
||||||
|
process_mode = Node.PROCESS_MODE_ALWAYS
|
||||||
|
|
||||||
|
func change_scene(scene_path : String, message : String = "Chargement...") -> void:
|
||||||
|
var start_ms := Time.get_ticks_msec()
|
||||||
|
|
||||||
|
# --- 1. Afficher l'écran de chargement (fade in) ---
|
||||||
|
status_label.text = message
|
||||||
|
progress_bar.value = 0
|
||||||
|
transition_rect.modulate.a = 0.0
|
||||||
|
transition_rect.visible = true
|
||||||
|
|
||||||
|
var fade_in := create_tween()
|
||||||
|
fade_in.tween_property(transition_rect, "modulate:a", 1.0, FADE_DURATION)
|
||||||
|
await fade_in.finished
|
||||||
|
|
||||||
|
# --- 2. Lancer le chargement en arrière-plan ---
|
||||||
|
var err := ResourceLoader.load_threaded_request(scene_path)
|
||||||
|
if err != OK:
|
||||||
|
push_error("Échec du lancement du chargement : %s (err %d)" % [scene_path, err])
|
||||||
|
await _fade_out()
|
||||||
|
return
|
||||||
|
|
||||||
|
# --- 3. Boucle jusqu'à ce que la scène soit prête ---
|
||||||
|
var prog : Array = []
|
||||||
|
while true:
|
||||||
|
var status := ResourceLoader.load_threaded_get_status(scene_path, prog)
|
||||||
|
match status:
|
||||||
|
ResourceLoader.THREAD_LOAD_IN_PROGRESS:
|
||||||
|
if prog.size() > 0:
|
||||||
|
progress_bar.value = prog[0] * 100.0
|
||||||
|
await get_tree().process_frame
|
||||||
|
ResourceLoader.THREAD_LOAD_LOADED:
|
||||||
|
break
|
||||||
|
_:
|
||||||
|
push_error("Échec du chargement de %s" % scene_path)
|
||||||
|
await _fade_out()
|
||||||
|
return
|
||||||
|
|
||||||
|
var packed : PackedScene = ResourceLoader.load_threaded_get(scene_path)
|
||||||
|
if packed == null:
|
||||||
|
push_error("PackedScene nulle pour %s" % scene_path)
|
||||||
|
await _fade_out()
|
||||||
|
return
|
||||||
|
|
||||||
|
# --- 4. Durée minimale visible (anti-flash) ---
|
||||||
|
var elapsed := (Time.get_ticks_msec() - start_ms) / 1000.0
|
||||||
|
if elapsed < MIN_DISPLAY_TIME:
|
||||||
|
await get_tree().create_timer(MIN_DISPLAY_TIME - elapsed).timeout
|
||||||
|
|
||||||
|
# --- 5. Changer de scène ---
|
||||||
|
get_tree().change_scene_to_packed(packed)
|
||||||
|
|
||||||
|
# --- 6. Laisser la nouvelle scène s'initialiser, puis fade out ---
|
||||||
|
await get_tree().process_frame
|
||||||
|
await get_tree().process_frame
|
||||||
|
await _fade_out()
|
||||||
|
|
||||||
|
func _fade_out() -> void:
|
||||||
|
var tween := create_tween()
|
||||||
|
tween.tween_property(transition_rect, "modulate:a", 0.0, FADE_DURATION)
|
||||||
|
await tween.finished
|
||||||
|
transition_rect.visible = false
|
||||||
1
game/ui/loading_screen/loading_screen.gd.uid
Normal file
1
game/ui/loading_screen/loading_screen.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://nl7jg02c6wwm
|
||||||
42
game/ui/loading_screen/loading_screen.tscn
Normal file
42
game/ui/loading_screen/loading_screen.tscn
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
[gd_scene format=3 uid="uid://ckyjxhrfhaqlc"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://nl7jg02c6wwm" path="res://game/ui/loading_screen/loading_screen.gd" id="1_v6dnv"]
|
||||||
|
|
||||||
|
[node name="LoadingScreen" type="CanvasLayer" unique_id=255554158]
|
||||||
|
layer = 128
|
||||||
|
script = ExtResource("1_v6dnv")
|
||||||
|
|
||||||
|
[node name="TransitionRect" type="ColorRect" parent="." unique_id=2119321044]
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
color = Color(0, 0, 0, 1)
|
||||||
|
|
||||||
|
[node name="MarginContainer" type="MarginContainer" parent="TransitionRect" unique_id=1396198220]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 3
|
||||||
|
anchor_left = 1.0
|
||||||
|
anchor_top = 1.0
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
offset_left = -1152.0
|
||||||
|
offset_top = -54.0
|
||||||
|
grow_horizontal = 0
|
||||||
|
grow_vertical = 0
|
||||||
|
theme_override_constants/margin_right = 15
|
||||||
|
theme_override_constants/margin_bottom = 15
|
||||||
|
|
||||||
|
[node name="VBoxContainer" type="VBoxContainer" parent="TransitionRect/MarginContainer" unique_id=550870747]
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 8
|
||||||
|
size_flags_vertical = 8
|
||||||
|
|
||||||
|
[node name="StatusLabel" type="Label" parent="TransitionRect/MarginContainer/VBoxContainer" unique_id=782325402]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 2
|
||||||
|
|
||||||
|
[node name="ProgressBar" type="ProgressBar" parent="TransitionRect/MarginContainer/VBoxContainer" unique_id=1836299440]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 2
|
||||||
|
|
@ -4,7 +4,6 @@ extends Node
|
||||||
@onready var Quit: Button = %Quit
|
@onready var Quit: Button = %Quit
|
||||||
@onready var background: TextureRect = %Background
|
@onready var background: TextureRect = %Background
|
||||||
@onready var music_player: AudioStreamPlayer = %MusicPlayer
|
@onready var music_player: AudioStreamPlayer = %MusicPlayer
|
||||||
@onready var transition_rect: ColorRect = %TransitionRect
|
|
||||||
@onready var version_label: Label = %VersionLabel
|
@onready var version_label: Label = %VersionLabel
|
||||||
@onready var game_infos: Control = %GameInfos
|
@onready var game_infos: Control = %GameInfos
|
||||||
|
|
||||||
|
|
@ -14,7 +13,6 @@ extends Node
|
||||||
|
|
||||||
const GAME_SCENE_PATH := "res://game/world/main.tscn"
|
const GAME_SCENE_PATH := "res://game/world/main.tscn"
|
||||||
const MUSIC_FADE_DURATION := 0.8
|
const MUSIC_FADE_DURATION := 0.8
|
||||||
const SCREEN_FADE_DURATION := 0.6
|
|
||||||
|
|
||||||
var is_transitioning : bool = false
|
var is_transitioning : bool = false
|
||||||
|
|
||||||
|
|
@ -57,23 +55,15 @@ func _on_start_pressed() -> void:
|
||||||
if is_transitioning: return
|
if is_transitioning: return
|
||||||
is_transitioning = true
|
is_transitioning = true
|
||||||
play_click_sound()
|
play_click_sound()
|
||||||
start_transition_to(GAME_SCENE_PATH)
|
LoadingScreen.change_scene(GAME_SCENE_PATH, "Préparation de l'aventure...")
|
||||||
|
|
||||||
func play_click_sound() -> void:
|
func play_click_sound() -> void:
|
||||||
UiAudio.play(start_click_sound)
|
UiAudio.play(start_click_sound)
|
||||||
|
|
||||||
|
func fade_out_music() -> void:
|
||||||
|
if !music_player.playing: return
|
||||||
|
var tween := create_tween()
|
||||||
|
tween.tween_property(music_player, "volume_db", -80.0, MUSIC_FADE_DURATION)
|
||||||
|
|
||||||
func _on_quit_pressed() -> void:
|
func _on_quit_pressed() -> void:
|
||||||
get_tree().quit()
|
get_tree().quit()
|
||||||
|
|
||||||
func start_transition_to(scene_path : String) -> void:
|
|
||||||
if music_player.playing:
|
|
||||||
var music_tween := create_tween()
|
|
||||||
music_tween.tween_property(music_player, "volume_db", -80.0, MUSIC_FADE_DURATION)
|
|
||||||
|
|
||||||
transition_rect.visible = true
|
|
||||||
transition_rect.modulate.a = 0.0
|
|
||||||
var screen_tween := create_tween()
|
|
||||||
screen_tween.tween_property(transition_rect, "modulate:a", 1.0, SCREEN_FADE_DURATION)
|
|
||||||
await screen_tween.finished
|
|
||||||
|
|
||||||
get_tree().change_scene_to_file(scene_path)
|
|
||||||
|
|
|
||||||
|
|
@ -167,16 +167,5 @@ grow_vertical = 0
|
||||||
[node name="MusicPlayer" type="AudioStreamPlayer" parent="." unique_id=1279071971]
|
[node name="MusicPlayer" type="AudioStreamPlayer" parent="." unique_id=1279071971]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
|
|
||||||
[node name="TransitionRect" type="ColorRect" parent="." unique_id=389028330]
|
|
||||||
unique_name_in_owner = true
|
|
||||||
modulate = Color(1, 1, 1, 0)
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 15
|
|
||||||
anchor_right = 1.0
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0, 0, 0, 1)
|
|
||||||
|
|
||||||
[connection signal="pressed" from="GameInfos/Buttons/VBoxContainer/StartContainer/HBoxContainer/Start" to="." method="_on_start_pressed"]
|
[connection signal="pressed" from="GameInfos/Buttons/VBoxContainer/StartContainer/HBoxContainer/Start" to="." method="_on_start_pressed"]
|
||||||
[connection signal="pressed" from="GameInfos/Buttons/VBoxContainer/QuitContainer/HBoxContainer/Quit" to="." method="_on_quit_pressed"]
|
[connection signal="pressed" from="GameInfos/Buttons/VBoxContainer/QuitContainer/HBoxContainer/Quit" to="." method="_on_quit_pressed"]
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ buses/default_bus_layout="res://game/core/default_bus_layout.tres"
|
||||||
ControllerIcons="*uid://b7ho1rjcpht1d"
|
ControllerIcons="*uid://b7ho1rjcpht1d"
|
||||||
GameData="*uid://d4beiel88cwc7"
|
GameData="*uid://d4beiel88cwc7"
|
||||||
UiAudio="*uid://7e7fe2xk3aue"
|
UiAudio="*uid://7e7fe2xk3aue"
|
||||||
|
LoadingScreen="*uid://ckyjxhrfhaqlc"
|
||||||
Pokedex="*uid://dijnkrewbwgmx"
|
Pokedex="*uid://dijnkrewbwgmx"
|
||||||
MountRegistry="*uid://u4gskwbwm0b0"
|
MountRegistry="*uid://u4gskwbwm0b0"
|
||||||
DebugPokemonPanel="*uid://b3058rssf6ud5"
|
DebugPokemonPanel="*uid://b3058rssf6ud5"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue