Browse Source

add fancy scene change fades

Arnaud Vergnet 9 months ago
parent
commit
c1d28ea050

+ 44
- 0
addons/transit/Transit.gd View File

@@ -0,0 +1,44 @@
1
+extends Control
2
+
3
+signal scene_changed
4
+
5
+const DEFAULT_DURATION := 0.2
6
+const DEFAULT_DELAY := 0.0
7
+
8
+onready var _animator := $AnimationPlayer
9
+onready var _curtain := $CanvasLayer/ColorRect
10
+
11
+func set_color(color: Color):
12
+    color.a = _curtain.color.a
13
+    _curtain.color = color
14
+
15
+func change_scene(path: String, duration: float = DEFAULT_DURATION, delay: float = DEFAULT_DELAY):
16
+    if duration <= 0.0:
17
+        push_error("TRANSIT ERROR: change_scene duration must be > 0. Defaulting to %s" % DEFAULT_DURATION)
18
+        duration = DEFAULT_DURATION
19
+
20
+    if delay < 0.0:
21
+        push_error("TRANSIT ERROR: change_scene delay must be >= 0. Defaulting to %s" % DEFAULT_DELAY)
22
+        delay = DEFAULT_DELAY
23
+
24
+    # disable mouse interaction while fading out
25
+    _curtain.mouse_filter = MOUSE_FILTER_STOP
26
+
27
+    if delay > 0:
28
+        yield(get_tree().create_timer(delay), "timeout")
29
+
30
+    _animator.playback_speed = 1.0 / duration
31
+    _animator.play("fade")
32
+    yield(_animator, "animation_finished")
33
+
34
+    var err := get_tree().change_scene(path)
35
+    if err:
36
+        push_error("TRANSIT ERROR: Failed to change scene to %s: %s" % [path, err])
37
+
38
+    # re-enable mouse interaction before fading back in
39
+    _curtain.mouse_filter = MOUSE_FILTER_IGNORE
40
+
41
+    _animator.play_backwards("fade")
42
+    yield(_animator, "animation_finished")
43
+
44
+    emit_signal("scene_changed")

+ 42
- 0
addons/transit/Transit.tscn View File

@@ -0,0 +1,42 @@
1
+[gd_scene load_steps=3 format=2]
2
+
3
+[ext_resource path="res://addons/transit/Transit.gd" type="Script" id=1]
4
+
5
+[sub_resource type="Animation" id=1]
6
+resource_name = "fade"
7
+step = 0.2
8
+tracks/0/type = "value"
9
+tracks/0/path = NodePath("CanvasLayer/ColorRect:color:a")
10
+tracks/0/interp = 1
11
+tracks/0/loop_wrap = true
12
+tracks/0/imported = false
13
+tracks/0/enabled = true
14
+tracks/0/keys = {
15
+"times": PoolRealArray( 0, 1 ),
16
+"transitions": PoolRealArray( 1, 1 ),
17
+"update": 0,
18
+"values": [ 0.0, 1.0 ]
19
+}
20
+
21
+[node name="Transit" type="Control"]
22
+anchor_right = 1.0
23
+anchor_bottom = 1.0
24
+script = ExtResource( 1 )
25
+__meta__ = {
26
+"_edit_use_anchors_": false
27
+}
28
+
29
+[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
30
+anims/fade = SubResource( 1 )
31
+
32
+[node name="CanvasLayer" type="CanvasLayer" parent="."]
33
+layer = 128
34
+
35
+[node name="ColorRect" type="ColorRect" parent="CanvasLayer"]
36
+anchor_right = 1.0
37
+anchor_bottom = 1.0
38
+mouse_filter = 2
39
+color = Color( 0, 0, 0, 0 )
40
+__meta__ = {
41
+"_edit_use_anchors_": false
42
+}

+ 2
- 1
project.godot View File

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

+ 1
- 1
scenes/Credits.gd View File

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

+ 3
- 2
scenes/MainMenu.gd View File

@@ -7,11 +7,11 @@ func _ready():
7 7
 
8 8
 
9 9
 func load_main_game():
10
-	get_tree().change_scene("res://scenes/Main.tscn")
10
+	Transit.change_scene("res://scenes/Main.tscn", 0.5)
11 11
 
12 12
 
13 13
 func load_credits():
14
-	get_tree().change_scene("res://scenes/Credits.tscn")
14
+	Transit.change_scene("res://scenes/Credits.tscn", 0.2)
15 15
 
16 16
 
17 17
 func _on_NewGameButton_pressed():
@@ -19,6 +19,7 @@ func _on_NewGameButton_pressed():
19 19
 	load_main_game()
20 20
 
21 21
 
22
+
22 23
 func _on_ContinueButton_pressed():
23 24
 	load_main_game()
24 25
 

+ 1
- 1
scenes/PauseMenu.gd View File

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

scenes/BackgroundMusic.gd → scenes/autoload/BackgroundMusic.gd View File


scenes/BackgroundMusic.tscn → scenes/autoload/BackgroundMusic.tscn View File

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

+ 1
- 1
scenes/end-screen/EndRecap.gd View File

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

Loading…
Cancel
Save