Implemented fading transition between levels. Fixed respawn bug when falling out of bounds while gravity is flipped.
This commit is contained in:
parent
c51bca1775
commit
a3a880f8e4
8 changed files with 121 additions and 33 deletions
|
|
@ -1,11 +0,0 @@
|
|||
[gd_scene format=3 uid="uid://ci0yqv1tna0ph"]
|
||||
|
||||
[node name="Fade" type="CanvasLayer"]
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
color = Color(1, 1, 1, 0)
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -1,9 +1,10 @@
|
|||
[gd_scene load_steps=6 format=3 uid="uid://syx6ov00a585"]
|
||||
[gd_scene load_steps=7 format=3 uid="uid://syx6ov00a585"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ohb2t6o7wr1u" path="res://Scripts/player.gd" id="1_cvnsp"]
|
||||
[ext_resource type="Texture2D" uid="uid://btqnhg54e1p66" path="res://Sprites/png/spritesheet.png" id="2_6t5aa"]
|
||||
[ext_resource type="Material" uid="uid://ck13qte606w1j" path="res://Materials/Particles/down_particles.tres" id="2_vgqql"]
|
||||
[ext_resource type="Script" uid="uid://de1fdvk35epdt" path="res://Scripts/player_camera.gd" id="4_vgqql"]
|
||||
[ext_resource type="PackedScene" uid="uid://botdddpmgoc8o" path="res://Scenes/scene_transition_rect.tscn" id="5_fkybt"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jjgbg"]
|
||||
size = Vector2(16, 16)
|
||||
|
|
@ -32,14 +33,10 @@ rect = Rect2(-8, -8, 16, 16)
|
|||
top_level = true
|
||||
script = ExtResource("4_vgqql")
|
||||
|
||||
[node name="Fade" type="CanvasLayer" parent="."]
|
||||
|
||||
[node name="Rectangle" type="ColorRect" parent="Fade"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
color = Color(1, 1, 1, 0)
|
||||
[node name="SceneTransitionRect" parent="." instance=ExtResource("5_fkybt")]
|
||||
offset_left = -5000.0
|
||||
offset_top = -5000.0
|
||||
offset_right = 5000.0
|
||||
offset_bottom = 5000.0
|
||||
|
||||
[connection signal="screen_exited" from="VisibleOnScreenNotifier2D" to="." method="_on_visible_on_screen_notifier_2d_screen_exited"]
|
||||
|
|
|
|||
54
Scenes/scene_transition_rect.tscn
Normal file
54
Scenes/scene_transition_rect.tscn
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
[gd_scene load_steps=5 format=3 uid="uid://botdddpmgoc8o"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ndoq8wxl5da7" path="res://Scripts/scene_transition_rect.gd" id="1_mgv8w"]
|
||||
|
||||
[sub_resource type="Animation" id="Animation_cqlxs"]
|
||||
length = 0.001
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath(".:color")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 0,
|
||||
"values": [Color(1, 1, 1, 0)]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_4152e"]
|
||||
resource_name = "fade"
|
||||
length = 1.5
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath(".:color")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0, 1.5),
|
||||
"transitions": PackedFloat32Array(1, 1),
|
||||
"update": 0,
|
||||
"values": [Color(1, 1, 1, 0), Color(1, 1, 1, 1)]
|
||||
}
|
||||
|
||||
[sub_resource type="AnimationLibrary" id="AnimationLibrary_dhvso"]
|
||||
_data = {
|
||||
&"RESET": SubResource("Animation_cqlxs"),
|
||||
&"fade": SubResource("Animation_4152e")
|
||||
}
|
||||
|
||||
[node name="SceneTransitionRect" type="ColorRect"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
color = Color(1, 1, 1, 0)
|
||||
script = ExtResource("1_mgv8w")
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
|
||||
libraries = {
|
||||
&"": SubResource("AnimationLibrary_dhvso")
|
||||
}
|
||||
|
|
@ -2,3 +2,5 @@ extends Node2D
|
|||
|
||||
var startPosX : int = 40
|
||||
var startPosY : int = 328
|
||||
|
||||
var next_level : String = "res://Scenes/Levels/level_1.tscn"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
extends CharacterBody2D
|
||||
|
||||
@onready var tile_layer : TileMapLayer = $"../TileMapLayer"
|
||||
@onready var level : Node2D = $".."
|
||||
@onready var particles : GPUParticles2D = $GPUParticles2D
|
||||
@onready var fade : ColorRect = $Fade/Rectangle
|
||||
@onready var current_level : Node2D = $".." #The current level.
|
||||
@onready var tile_layer : TileMapLayer = $"../TileMapLayer" #The tile map layer of the current level.
|
||||
@onready var particles : GPUParticles2D = $GPUParticles2D #The particles of the player.
|
||||
@onready var transition_rect : ColorRect = $SceneTransitionRect #The white rectangle used for fading in and out.
|
||||
@onready var transition_anim : AnimationPlayer = $SceneTransitionRect/AnimationPlayer #The animation player attached to the transition rectangle.
|
||||
|
||||
@export_category("Movement")
|
||||
@export var acceleration : float = 50
|
||||
|
|
@ -14,11 +15,15 @@ var move_input : float
|
|||
var dead : bool
|
||||
|
||||
func _ready() -> void:
|
||||
#Triggers fade in animation, and then triggers respawn script.
|
||||
transition_rect.fade_in()
|
||||
respawn()
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
|
||||
#Only allows movement if the player is not considered dead.
|
||||
if dead == false:
|
||||
|
||||
#Horizontal movement.
|
||||
move_input = Input.get_axis("move_left", "move_right")
|
||||
if move_input != 0:
|
||||
|
|
@ -29,9 +34,11 @@ func _physics_process(delta: float) -> void:
|
|||
#Vertical movement.
|
||||
velocity.y += al_globals.gravity * delta
|
||||
|
||||
#Gravity flip.
|
||||
if Input.is_action_just_pressed("flip") and (is_on_floor() or is_on_ceiling()):
|
||||
al_globals.gravity *= -1
|
||||
|
||||
#If the player is moving downwards fast, turn on particles. Otherwise, disable them.
|
||||
if velocity.y > 500 and not (is_on_floor() or is_on_ceiling()):
|
||||
particles.emitting = true
|
||||
else:
|
||||
|
|
@ -40,6 +47,7 @@ func _physics_process(delta: float) -> void:
|
|||
move_and_slide() #Allow physics control.
|
||||
tilemap_detection() #Checks for tiles with the "Spike" custom data enabled.
|
||||
|
||||
#Facilitates the detection and interaction with tiles on the tile map.
|
||||
func tilemap_detection() -> void:
|
||||
#Adapted from Godot forum post.
|
||||
|
||||
|
|
@ -58,31 +66,59 @@ func tilemap_detection() -> void:
|
|||
#Checks for exits.
|
||||
var exit_data = tile_data.get_custom_data("exit")
|
||||
if exit_data == true:
|
||||
next_level(level.next_level) #Pulls next level data from parent Node2D.
|
||||
next_level(current_level.next_level) #Pulls next level data from parent Node2D.
|
||||
|
||||
#Facilitates the death of the player.
|
||||
func death(fallen: bool) -> void:
|
||||
|
||||
#Prevents movement.
|
||||
dead = true
|
||||
|
||||
#Checks if the player has not fallen out of bounds.
|
||||
#If not, halt all current velocity, and shrink them down to 0 scale as a death animation.
|
||||
if !fallen:
|
||||
velocity.x = 0
|
||||
velocity.y = 0
|
||||
scale.x = lerp(scale.x, 0.0, 0.1)
|
||||
scale.y = lerp(scale.y, 0.0, 0.1)
|
||||
|
||||
#Sets the gravity back to default (downwards)
|
||||
if al_globals.gravity < 0:
|
||||
al_globals.gravity *= -1
|
||||
|
||||
#Pauses for a second for effect, then triggers the respawn function.
|
||||
await get_tree().create_timer(1.0).timeout
|
||||
respawn()
|
||||
|
||||
#Facilitates the respawn of the player.
|
||||
func respawn() -> void:
|
||||
dead = false
|
||||
#Reset player velocity to 0, scale them back to full size.
|
||||
velocity.y = 0
|
||||
scale.x = lerp(scale.x, 1.0, 0.1)
|
||||
scale.y = lerp(scale.y, 1.0, 0.1)
|
||||
position.x = level.startPosX
|
||||
position.y = level.startPosY
|
||||
|
||||
func next_level(level : String):
|
||||
dead == true
|
||||
#Change their position to the start position of the current level.
|
||||
position.x = current_level.startPosX
|
||||
position.y = current_level.startPosY
|
||||
|
||||
#Mark them as alive.
|
||||
dead = false
|
||||
|
||||
#Facilitates transitioning to the next level.
|
||||
#level = the path to the next level desired.
|
||||
func next_level(level : String) -> void:
|
||||
#Prevent movement, and halt their current velocity.
|
||||
dead = true
|
||||
velocity.x = 0
|
||||
velocity.y = 0
|
||||
|
||||
#Trigger the fade out animation, and then change to the level specified.
|
||||
transition_rect.fade_out()
|
||||
await get_tree().create_timer(transition_anim.current_animation_length).timeout
|
||||
get_tree().change_scene_to_file(level)
|
||||
|
||||
#Detects if the player fell out of view of the camera.
|
||||
#Most likely due to falling out of bounds.
|
||||
func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
|
||||
if !dead:
|
||||
death(true)
|
||||
|
|
|
|||
9
Scripts/scene_transition_rect.gd
Normal file
9
Scripts/scene_transition_rect.gd
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
extends ColorRect
|
||||
|
||||
@onready var anim_player := $AnimationPlayer
|
||||
|
||||
func fade_in() -> void:
|
||||
anim_player.play_backwards("fade")
|
||||
|
||||
func fade_out() -> void:
|
||||
anim_player.play("fade")
|
||||
1
Scripts/scene_transition_rect.gd.uid
Normal file
1
Scripts/scene_transition_rect.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ndoq8wxl5da7
|
||||
Loading…
Add table
Reference in a new issue