Un shader de transition entre les scènes

ça fait plus propre braw
This commit is contained in:
Thomas
2025-04-14 17:24:10 +02:00
parent bf2ed6312d
commit 5e32ed4196
13 changed files with 228 additions and 18 deletions

View File

@@ -3,7 +3,10 @@ class_name GameControler
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("menu"):
open_menu()
if prev_scene == menu:
start_game()
else:
open_menu()
# list of scenes
var outside:Node2D = null
@@ -11,41 +14,46 @@ var dest_outside = preload("res://scenes/outside.tscn")
var bakery:Node2D = null
var dest_bakery = preload("res://scenes/bakery_interior.tscn")
var menu = null
var prev_scene = null
func _ready() -> void:
GameState._game = self
menu = get_child(0)
prev_scene = menu
func start_game():
menu = get_child(0)
switch_to(GameState.current_scene)
func switch_to(to: String):
call_deferred("switch_scene", to)
var next_scene = null
var toMenu = false;
var switching_to
func switch_scene(to: String):
var prev = get_child(0)
GameState.current_scene = to
var scene
switching_to= to
print("switching to", switching_to)
toMenu = to == "menu"
match to:
"outside":
if outside == null:
outside = dest_outside.instantiate()
scene = outside
next_scene = outside
"bakery":
if bakery == null:
bakery = dest_bakery.instantiate()
scene = bakery
scene.unload()
call_deferred("add_child", scene)
call_deferred("remove_child", prev)
call_deferred("init_scence", scene)
next_scene = bakery
"menu":
next_scene = menu
$TransitionShader.fade_out()
func init_scence(scene):
scene.load_from_game_state()
$TransitionShader.fade_in()
func open_menu():
remove_child(get_child(0))
add_child(menu)
switch_scene("menu")
func load_game():
var save_file = FileAccess.open("user://savegame.save", FileAccess.READ)
@@ -62,3 +70,19 @@ func save_game():
save_file.store_line(JSON.stringify(GameState.save(), " ", true, true))
print(OS.get_data_dir())
save_file.close()
func _on_transition_shader_fade_in_done() -> void:
if !toMenu:
GameState.current_scene = switching_to
func _on_transition_shader_fade_out_done() -> void:
print("fade out done")
if !toMenu:
next_scene.unload()
call_deferred("add_child", next_scene)
call_deferred("remove_child", prev_scene)
if !toMenu:
call_deferred("init_scence", next_scene)
else:
$TransitionShader.fade_in()
prev_scene = next_scene