Skip to content

Commit 7fb6989

Browse files
committed
player: Add "glide" special ability
If the player holds down the "jump" action, their character's downwards speed is capped to a constant. This allows the player-character to travel across wider gaps by jumping then gliding. The function call for this ability is in _physics_process(), after other "normal" adjustments to the velocity but before move_and_slide(). However, it is commented out. A learner must find this commented-out line and uncomment it to enable this ability. The sound effect is from https://freesound.org/people/tothrec2/sounds/596541/, licensed under CC0-1.0.
1 parent 242bead commit 7fb6989

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed
30.9 KB
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[remap]
2+
3+
importer="oggvorbisstr"
4+
type="AudioStreamOggVorbis"
5+
uid="uid://p0b3e0t7h8j3"
6+
path="res://.godot/imported/596541__tothrec2__large-wings-flapping-foley.ogg-715998e9d4f908348de867f3ceb6fce5.oggvorbisstr"
7+
8+
[deps]
9+
10+
source_file="res://assets/sounds/596541__tothrec2__large-wings-flapping-foley.ogg"
11+
dest_files=["res://.godot/imported/596541__tothrec2__large-wings-flapping-foley.ogg-715998e9d4f908348de867f3ceb6fce5.oggvorbisstr"]
12+
13+
[params]
14+
15+
loop=false
16+
loop_offset=0
17+
bpm=0
18+
beat_count=0
19+
bar_beats=4

components/player/player.tscn

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
[gd_scene load_steps=8 format=3 uid="uid://8st4scqt06l8"]
1+
[gd_scene load_steps=9 format=3 uid="uid://8st4scqt06l8"]
22

33
[ext_resource type="Script" uid="uid://umb21eb2y1oj" path="res://scripts/player.gd" id="1_w3ms2"]
44
[ext_resource type="SpriteFrames" uid="uid://bo581k1esb50n" path="res://components/player/spriteframes-red.tres" id="2_msaml"]
55
[ext_resource type="AudioStream" uid="uid://bx1joarpc14j5" path="res://assets/sounds/538066__stevielematt__boing.ogg" id="3_wa6cj"]
6+
[ext_resource type="AudioStream" uid="uid://p0b3e0t7h8j3" path="res://assets/sounds/596541__tothrec2__large-wings-flapping-foley.ogg" id="4_mpawu"]
67

78
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_7x5a4"]
89
radius = 31.0
@@ -51,3 +52,7 @@ one_way_collision = true
5152
[node name="JumpSFX" type="AudioStreamPlayer" parent="SoundEffects"]
5253
unique_name_in_owner = true
5354
stream = SubResource("AudioStreamRandomizer_mpawu")
55+
56+
[node name="GlideSFX" type="AudioStreamPlayer" parent="SoundEffects"]
57+
unique_name_in_owner = true
58+
stream = ExtResource("4_mpawu")

scripts/player.gd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ class_name Player
33
extends CharacterBody2D
44
## A player's character, which can walk, jump, and stomp on enemies.
55

6+
## The player-character's maximum downwards speed while gliding.
7+
## Making this number smaller allows the player to glide further.
8+
## [br][br]
9+
## Used by [method _glide].
10+
const GLIDE_TERMINAL_VELOCITY = 100
11+
612
## Which player controls this character?
713
@export var player: Global.Player = Global.Player.ONE
814

@@ -57,6 +63,7 @@ var original_position: Vector2
5763
@onready var _double_jump_particles: CPUParticles2D = %DoubleJumpParticles
5864

5965
@onready var _jump_sfx: AudioStreamPlayer = %JumpSFX
66+
@onready var _glide_sfx: AudioStreamPlayer = %GlideSFX
6067

6168

6269
func _set_sprite_frames(new_sprite_frames):
@@ -106,6 +113,21 @@ func stomp():
106113
_jump()
107114

108115

116+
## If the player-character is in the air, and the "jump" action is held, clamp the downwards
117+
## velocity to a constant. Must be called after applying gravity to the player-character.
118+
func _glide() -> void:
119+
if not is_on_floor() and Input.is_action_pressed(Actions.lookup(player, "jump")):
120+
if velocity.y > GLIDE_TERMINAL_VELOCITY:
121+
velocity.y = GLIDE_TERMINAL_VELOCITY
122+
123+
# Only play the sound effect when the player-character is moving downwards, not while
124+
# jumping upwards
125+
if velocity.y > 0 and not _glide_sfx.playing:
126+
_glide_sfx.play()
127+
elif _glide_sfx.playing:
128+
_glide_sfx.stop()
129+
130+
109131
func _physics_process(delta):
110132
# Don't move if there are no lives left.
111133
if Global.lives <= 0:
@@ -142,6 +164,8 @@ func _physics_process(delta):
142164
else:
143165
velocity.x = move_toward(velocity.x, 0, acceleration * delta)
144166

167+
# _glide()
168+
145169
if velocity == Vector2.ZERO:
146170
_sprite.play("idle")
147171
else:

0 commit comments

Comments
 (0)