Skip to content

Commit

Permalink
Player: Add optional double jump
Browse files Browse the repository at this point in the history
If enabled, the player can jump a second time while still in the air from a
previous jump. A crude shower of particles is emitted to indicate ✨ magic ✨.
  • Loading branch information
wjt committed Oct 26, 2024
1 parent 58cb737 commit b14ffa8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
15 changes: 15 additions & 0 deletions components/player/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ floor_constant_speed = true
floor_snap_length = 32.0
script = ExtResource("1_w3ms2")

[node name="DoubleJumpParticles" type="CPUParticles2D" parent="."]
unique_name_in_owner = true
emitting = false
amount = 60
lifetime = 0.2
one_shot = true
explosiveness = 0.54
randomness = 0.25
emission_shape = 1
emission_sphere_radius = 36.72
particle_flag_align_y = true
gravity = Vector2(0, 1)
scale_amount_max = 5.0
color = Color(1, 1, 0, 1)

[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
unique_name_in_owner = true
position = Vector2(0, -64)
Expand Down
15 changes: 14 additions & 1 deletion scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ extends CharacterBody2D
## a small positive number to allow the player a little margin for error.
@export_range(0, 0.5, 1 / 60.0, "suffix:s") var jump_buffer: float = 5.0 / 60.0

## Can your character jump a second time while still in the air?
@export var double_jump: bool = false

# If positive, the player is either on the ground, or left the ground less than this long ago
var coyote_timer: float = 0

# If positive, the player pressed jump this long ago
var jump_buffer_timer: float = 0

# If true, the player is already jumping and can perform a double-jump
var double_jump_armed: bool = false

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var original_position: Vector2

@onready var _sprite: AnimatedSprite2D = %AnimatedSprite2D
@onready var _initial_sprite_frames: SpriteFrames = %AnimatedSprite2D.sprite_frames
@onready var _double_jump_particles: CPUParticles2D = %DoubleJumpParticles


func _set_sprite_frames(new_sprite_frames):
Expand Down Expand Up @@ -87,14 +94,20 @@ func _physics_process(delta):
# Handle jump
if is_on_floor():
coyote_timer = (coyote_time + delta)
double_jump_armed = false

if Input.is_action_just_pressed("ui_accept"):
jump_buffer_timer = (jump_buffer + delta)

if jump_buffer_timer > 0 and coyote_timer > 0:
if jump_buffer_timer > 0 and (double_jump_armed or coyote_timer > 0):
velocity.y = jump_velocity
coyote_timer = 0
jump_buffer_timer = 0
if double_jump_armed:
double_jump_armed = false
_double_jump_particles.emitting = true
elif double_jump:
double_jump_armed = true

# Reduce velocity if the player lets go of the jump key before the apex.
# This allows controlling the height of the jump.
Expand Down

0 comments on commit b14ffa8

Please sign in to comment.