From b14ffa852de0f4917a700134c094a83cb0988623 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Fri, 25 Oct 2024 14:07:47 +0100 Subject: [PATCH] Player: Add optional double jump MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ✨. --- components/player/player.tscn | 15 +++++++++++++++ scripts/player.gd | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/components/player/player.tscn b/components/player/player.tscn index 408eb4d..3228bdb 100644 --- a/components/player/player.tscn +++ b/components/player/player.tscn @@ -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) diff --git a/scripts/player.gd b/scripts/player.gd index 0e9a118..046428a 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -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): @@ -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.