Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving jump functionality #69

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions COGITO/PrefabScenes/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,9 @@ start_sanity = 30.0

[node name="BrightnessComponent" parent="." instance=ExtResource("9_x4tef")]

[node name="JumpCooldownTimer" type="Timer" parent="."]
wait_time = 0.5
one_shot = true

[connection signal="animation_finished" from="Neck/Head/Eyes/AnimationPlayer" to="." method="_on_animation_player_animation_finished"]
[connection signal="timeout" from="SlidingTimer" to="." method="_on_sliding_timer_timeout"]
63 changes: 42 additions & 21 deletions COGITO/Scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ signal player_state_loaded()
@onready var crouch_raycast: RayCast3D = $CrouchRayCast
@onready var sliding_timer: Timer = $SlidingTimer
@onready var footstep_timer: Timer = $FootstepTimer
@onready var jump_timer: Timer = $JumpCooldownTimer

## Inventory resource that stores the player inventory.
@export var inventory_data : InventoryPD
Expand All @@ -56,6 +57,7 @@ signal player_state_loaded()
@export var SPRINTING_SPEED = 8.0
@export var CROUCHING_SPEED = 3.0
@export var CROUCHING_DEPTH = -0.9
@export var CAN_CROUCH_JUMP = true
@export var MOUSE_SENS = 0.25
@export var LERP_SPEED = 10.0
@export var AIR_LERP_SPEED = 6.0
Expand Down Expand Up @@ -504,28 +506,47 @@ func _physics_process(delta):
if fall_damage > 0 and last_velocity.y <= fall_damage_threshold:
health_component.subtract(fall_damage)

if Input.is_action_pressed("jump") and !is_movement_paused and is_on_floor():
snap = Vector3.ZERO
is_falling = true
# If Stamina Component is used, this checks if there's enough stamina to jump and denies it if not.
if is_using_stamina and stamina_component.current_stamina >= stamina_component.jump_exhaustion:
decrease_attribute("stamina",stamina_component.jump_exhaustion)
else:
print("Not enough stamina to jump.")
return
if Input.is_action_pressed("jump") and !is_movement_paused and is_on_floor() and jump_timer.is_stopped():
jump_timer.start() # prevent spam
var doesnt_need_stamina = not is_using_stamina or stamina_component.current_stamina >= stamina_component.jump_exhaustion
var crouch_jump = not is_crouching or CAN_CROUCH_JUMP

if doesnt_need_stamina and crouch_jump:
# If Stamina Component is used, this checks if there's enough stamina to jump and denies it if not.
if is_using_stamina:
decrease_attribute("stamina",stamina_component.jump_exhaustion)
snap = Vector3.ZERO
is_falling = true

animationPlayer.play("jump")
Audio.play_sound(jump_sound)
if !sliding_timer.is_stopped():
velocity.y = JUMP_VELOCITY * 1.5
sliding_timer.stop()
else:
velocity.y = JUMP_VELOCITY
if is_sprinting:
bunny_hop_speed += BUNNY_HOP_ACCELERATION
else:
bunny_hop_speed = SPRINTING_SPEED

animationPlayer.play("jump")
Audio.play_sound(jump_sound)
if !sliding_timer.is_stopped():
velocity.y = JUMP_VELOCITY * 1.5
sliding_timer.stop()
else:
velocity.y = JUMP_VELOCITY

if platform_on_leave != PLATFORM_ON_LEAVE_DO_NOTHING:
var platform_velocity = get_platform_velocity()
# TODO: Make PLATFORM_ON_LEAVE_ADD_VELOCITY work... somehow.
# Velocity X and Z gets overridden later, so you immediately lose the velocity
if PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY:
platform_velocity.x = 0
platform_velocity.z = 0
velocity += platform_velocity

if is_sprinting:
bunny_hop_speed += BUNNY_HOP_ACCELERATION
else:
bunny_hop_speed = SPRINTING_SPEED

if is_crouching:
#temporarily switch colliders to process jump correctly
standing_collision_shape.disabled = false
crouching_collision_shape.disabled = true
elif not doesnt_need_stamina:
print("Not enough stamina to jump.")

if sliding_timer.is_stopped():
if is_on_floor():
direction = lerp(
Expand Down