Skip to content

Commit 974e653

Browse files
committed
Fix: Jump state reset and multi-jump spam in PhysicsEnginePlatformer
1 parent c33125b commit 974e653

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

arcade/physics_engines.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import math
66
from collections.abc import Iterable
7+
import time
78

89
from arcade import (
910
BasicSprite,
@@ -518,6 +519,12 @@ def __init__(
518519
* :py:meth:`disable_multi_jump`
519520
"""
520521

522+
self.jump_cooldown: float = 0.2
523+
"""Minimum time in seconds between jumps."""
524+
525+
self.last_jump_time: float = 0.0
526+
"""Time in seconds when the player last jumped."""
527+
521528
# The property object for ladders. This allows us setter/getter/deleter
522529
# capabilities in safe manner
523530
# TODO: figure out what do do with 15_ladders_moving_platforms.py
@@ -683,24 +690,28 @@ def can_jump(self, y_distance: float = 5) -> bool:
683690
``True`` if the player can jump.
684691
685692
"""
686-
693+
current_time = time.time()
694+
687695
# Temporarily move the player down to collide floor-like sprites
688696
self.player_sprite.center_y -= y_distance
689697
hit_list = check_for_collision_with_lists(self.player_sprite, self._all_obstacles)
690698
self.player_sprite.center_y += y_distance
691699

692-
# Reset the number jumps if the player touched a floor-like sprite
693-
if len(hit_list) > 0:
694-
self.jumps_since_ground = 0
700+
#Checks is the player is on a ladder, and if so, allow them to jump regardless of cooldown or jump count
701+
on_ladder = self.is_on_ladder()
695702

696-
if (
697-
len(hit_list) > 0
698-
or self.allow_multi_jump
699-
and self.jumps_since_ground < self.allowed_jumps
700-
):
703+
# If we are on solid ground or a ladder, reset the counter
704+
if len(hit_list) > 0 or on_ladder:
705+
self.jumps_since_ground = 0
701706
return True
702-
else:
703-
return False
707+
708+
# Must have multi-jump enabled, have jumps remaining, and be past cooldown
709+
if self.allow_multi_jump:
710+
if self.jumps_since_ground < self.allowed_jumps:
711+
if current_time - self.last_jump_time >= self.jump_cooldown:
712+
return True
713+
714+
return False
704715

705716
def enable_multi_jump(self, allowed_jumps: int) -> None:
706717
"""Enable multi-jump.
@@ -749,6 +760,7 @@ def jump(self, velocity: float) -> None:
749760
A positive value to set the player's y velocity to.
750761
"""
751762
self.player_sprite.change_y = velocity
763+
self.last_jump_time = time.time()
752764
self.increment_jump_counter()
753765

754766
def increment_jump_counter(self) -> None:
@@ -758,8 +770,8 @@ def increment_jump_counter(self) -> None:
758770
``1`` to :py:attr:`jumps_since_ground`. Otherwise, it does
759771
nothing.
760772
"""
761-
if self.allow_multi_jump:
762-
self.jumps_since_ground += 1
773+
#This always increments because we the logic is handled in can_jump
774+
self.jumps_since_ground += 1
763775

764776
def update(self) -> list[BasicSprite]:
765777
"""Move the player and platforms, then return colliding sprites.

0 commit comments

Comments
 (0)