add fancy scene change fades

This commit is contained in:
Arnaud Vergnet 2021-04-25 00:07:38 +02:00
parent f1fa580f05
commit c1d28ea050
9 changed files with 96 additions and 7 deletions

44
addons/transit/Transit.gd Normal file
View file

@ -0,0 +1,44 @@
extends Control
signal scene_changed
const DEFAULT_DURATION := 0.2
const DEFAULT_DELAY := 0.0
onready var _animator := $AnimationPlayer
onready var _curtain := $CanvasLayer/ColorRect
func set_color(color: Color):
color.a = _curtain.color.a
_curtain.color = color
func change_scene(path: String, duration: float = DEFAULT_DURATION, delay: float = DEFAULT_DELAY):
if duration <= 0.0:
push_error("TRANSIT ERROR: change_scene duration must be > 0. Defaulting to %s" % DEFAULT_DURATION)
duration = DEFAULT_DURATION
if delay < 0.0:
push_error("TRANSIT ERROR: change_scene delay must be >= 0. Defaulting to %s" % DEFAULT_DELAY)
delay = DEFAULT_DELAY
# disable mouse interaction while fading out
_curtain.mouse_filter = MOUSE_FILTER_STOP
if delay > 0:
yield(get_tree().create_timer(delay), "timeout")
_animator.playback_speed = 1.0 / duration
_animator.play("fade")
yield(_animator, "animation_finished")
var err := get_tree().change_scene(path)
if err:
push_error("TRANSIT ERROR: Failed to change scene to %s: %s" % [path, err])
# re-enable mouse interaction before fading back in
_curtain.mouse_filter = MOUSE_FILTER_IGNORE
_animator.play_backwards("fade")
yield(_animator, "animation_finished")
emit_signal("scene_changed")

View file

@ -0,0 +1,42 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://addons/transit/Transit.gd" type="Script" id=1]
[sub_resource type="Animation" id=1]
resource_name = "fade"
step = 0.2
tracks/0/type = "value"
tracks/0/path = NodePath("CanvasLayer/ColorRect:color:a")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/keys = {
"times": PoolRealArray( 0, 1 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 0,
"values": [ 0.0, 1.0 ]
}
[node name="Transit" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/fade = SubResource( 1 )
[node name="CanvasLayer" type="CanvasLayer" parent="."]
layer = 128
[node name="ColorRect" type="ColorRect" parent="CanvasLayer"]
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 2
color = Color( 0, 0, 0, 0 )
__meta__ = {
"_edit_use_anchors_": false
}

View file

@ -58,7 +58,8 @@ config/icon="res://icon.png"
DialogicSingleton="*res://addons/dialogic/Other/DialogicSingleton.gd"
Signals="*res://mini-game/scripts/Signals.gd"
BackgroundMusic="*res://scenes/BackgroundMusic.tscn"
BackgroundMusic="*res://scenes/autoload/BackgroundMusic.tscn"
Transit="*res://addons/transit/Transit.tscn"
[display]

View file

@ -3,7 +3,7 @@ extends Control
func load_main_menu():
get_tree().change_scene("res://scenes/MainMenu.tscn")
Transit.change_scene("res://scenes/MainMenu.tscn", 0.2)
func _on_BackButton_pressed():

View file

@ -7,11 +7,11 @@ func _ready():
func load_main_game():
get_tree().change_scene("res://scenes/Main.tscn")
Transit.change_scene("res://scenes/Main.tscn", 0.5)
func load_credits():
get_tree().change_scene("res://scenes/Credits.tscn")
Transit.change_scene("res://scenes/Credits.tscn", 0.2)
func _on_NewGameButton_pressed():
@ -19,6 +19,7 @@ func _on_NewGameButton_pressed():
load_main_game()
func _on_ContinueButton_pressed():
load_main_game()

View file

@ -1,7 +1,7 @@
extends Control
func load_main_menu():
get_tree().change_scene("res://scenes/MainMenu.tscn")
Transit.change_scene("res://scenes/MainMenu.tscn", 0.5)
func pause():

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scenes/BackgroundMusic.gd" type="Script" id=1]
[ext_resource path="res://scenes/autoload/BackgroundMusic.gd" type="Script" id=1]
[node name="BackgroundMusic" type="Control"]
pause_mode = 2

View file

@ -90,4 +90,4 @@ func _on_GaugesTimer_timeout():
func _on_ExitButton_pressed():
get_tree().change_scene("res://scenes/MainMenu.tscn")
Transit.change_scene("res://scenes/MainMenu.tscn", 0.5)