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

@@ -8,6 +8,7 @@ func _ready() -> void:
load_from_game_state()
func load_from_game_state():
print("setting player in bakery at ", GameState.position_bakery)
$Bakery/player.set_feet_global_position(GameState.position_bakery)
emit_signal("loadFromGameState")

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

View File

@@ -1,13 +1,18 @@
[gd_scene load_steps=3 format=3 uid="uid://c645h6ap7niu1"]
[gd_scene load_steps=4 format=3 uid="uid://c645h6ap7niu1"]
[ext_resource type="PackedScene" uid="uid://44wla7mlivqm" path="res://UI/menu.tscn" id="1_64gp8"]
[ext_resource type="Script" path="res://scenes/game.gd" id="1_b2ju7"]
[ext_resource type="PackedScene" uid="uid://17macg0q4k8l" path="res://shaders/transition_shader.tscn" id="3_2p7jc"]
[node name="Game" type="Node2D"]
script = ExtResource("1_b2ju7")
[node name="Menu" parent="." instance=ExtResource("1_64gp8")]
[node name="TransitionShader" parent="." instance=ExtResource("3_2p7jc")]
[connection signal="load" from="Menu" to="." method="load_game"]
[connection signal="save" from="Menu" to="." method="save_game"]
[connection signal="start" from="Menu" to="." method="start_game"]
[connection signal="fade_in_done" from="TransitionShader" to="." method="_on_transition_shader_fade_in_done"]
[connection signal="fade_out_done" from="TransitionShader" to="." method="_on_transition_shader_fade_out_done"]