From 84f3d962f680bed9170880fb67f01322e7ccc7ab Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sun, 28 Jan 2024 18:05:00 -0500 Subject: [PATCH 01/14] Added jump interval timer It's based on ticks --- arcade/physics_engines.py | 61 ++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 289d4f8a0..71a80cf61 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -311,6 +311,8 @@ def __init__(self, self.gravity_constant: float = gravity_constant self.jumps_since_ground: int = 0 self.allowed_jumps: int = 1 + self.jump_delay = 0 + self.jump_ticks = 0 self.allow_multi_jump: bool = False def is_on_ladder(self): @@ -342,23 +344,27 @@ def can_jump(self, y_distance: float = 5) -> bool: if len(hit_list) > 0: self.jumps_since_ground = 0 - if len(hit_list) > 0 or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps: + if (len(hit_list) > 0 or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and + self.jump_ticks >= self.jump_delay): return True else: return False - def enable_multi_jump(self, allowed_jumps: int): + def enable_multi_jump(self, allowed_jumps: int, jump_delay: Optional[int] = 10): """ Enables multi-jump. allowed_jumps should include the initial jump. (1 allows only a single jump, 2 enables double-jump, etc) If you enable multi-jump, you MUST call increment_jump_counter() - every time the player jumps. Otherwise they can jump infinitely. + every time the player jumps. Otherwise, they can jump infinitely. - :param allowed_jumps: + :param allowed_jumps: Maximum number of jumps allowed + :param jump_delay: Number of ticks before another jump is allowed """ self.allowed_jumps = allowed_jumps + self.jump_delay = jump_delay + self.jump_ticks = jump_delay self.allow_multi_jump = True def disable_multi_jump(self): @@ -372,10 +378,44 @@ def disable_multi_jump(self): self.allowed_jumps = 1 self.jumps_since_ground = 0 - def jump(self, velocity: int): - """ Have the character jump. """ - self.player_sprite.change_y = velocity - self.increment_jump_counter() + def jump(self, velocity: int, + air_jump_velocity: Optional[int] = None, + air_jump_style: Optional[str] = "set", + jump_velocity_limit: Optional[int] = None): + """ Have the character jump. Multijump can be set with a separate in-air velocity and can be air jumps can be + set to be additive, limited, or a set value. Additive only adds to the player's upward velocity. Limited will + set or add to the player's velocity. If the player is falling, then their velocity will be set to their air + jump speed. Otherwise, it will add their air jump speed up until the jump_velocity limit. Set always sets the + player's velocity to their air jump speed. """ + if self.can_jump(): + # Air Jump logic + if self.jumps_since_ground > 0: + # This checks if air_jump_velocity is set. If not it will default to the velocity for all air jumps. + if air_jump_velocity: + air_jump = air_jump_velocity + else: + air_jump = velocity + if air_jump_style == "additive": + self.player_sprite.change_y += air_jump + elif air_jump_style == "limited": + if not jump_velocity_limit: + jump_velocity_limit = air_jump + if self.player_sprite.change_y < 0: + self.player_sprite.change_y = air_jump + elif self.player_sprite.change_y + air_jump < jump_velocity_limit: + self.player_sprite.change_y += air_jump + else: + self.player_sprite.change_y = jump_velocity_limit + elif air_jump_style == "set": + self.player_sprite.change_y = air_jump + else: + raise ValueError("Air jump style set is not valid. Use additive, limited, or set.") + + # Ground Jump Logic + else: + self.player_sprite.change_y = velocity + + self.increment_jump_counter() def increment_jump_counter(self): """ @@ -383,6 +423,7 @@ def increment_jump_counter(self): """ if self.allow_multi_jump: self.jumps_since_ground += 1 + self.jump_ticks = 0 def update(self): """ @@ -401,6 +442,10 @@ def update(self): # print(f"Spot B ({self.player_sprite.center_x}, {self.player_sprite.center_y})") + if self.allow_multi_jump: + if self.jump_ticks < self.jump_delay: + self.jump_ticks += 1 + for platform_list in self.platforms: for platform in platform_list: if platform.change_x != 0 or platform.change_y != 0: From 8e673fcdb4d989147151454481f84e3171db8cec Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:51:25 +0100 Subject: [PATCH 02/14] Defined types for jump_delay and jump_ticks --- arcade/physics_engines.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 71a80cf61..90bf14b9d 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -311,8 +311,8 @@ def __init__(self, self.gravity_constant: float = gravity_constant self.jumps_since_ground: int = 0 self.allowed_jumps: int = 1 - self.jump_delay = 0 - self.jump_ticks = 0 + self.jump_delay: int = 0 + self.jump_ticks: int = 0 self.allow_multi_jump: bool = False def is_on_ladder(self): From beb1306ba9d329d835216ab42349b2cda6627cb7 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 10 Feb 2024 18:51:25 +0200 Subject: [PATCH 03/14] Update physics_engines.py Removed the Optional typing to rely on the default input instead --- arcade/physics_engines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 90bf14b9d..997bd76cf 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -350,7 +350,7 @@ def can_jump(self, y_distance: float = 5) -> bool: else: return False - def enable_multi_jump(self, allowed_jumps: int, jump_delay: Optional[int] = 10): + def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 10): """ Enables multi-jump. allowed_jumps should include the initial jump. From 6f63e7c334bc4b59b58f864aed343b0f41e8e640 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sun, 11 Feb 2024 19:08:06 +0200 Subject: [PATCH 04/14] Change default tick delay from 10 to 0 --- arcade/physics_engines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 997bd76cf..4d80f1e85 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -350,7 +350,7 @@ def can_jump(self, y_distance: float = 5) -> bool: else: return False - def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 10): + def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 0): """ Enables multi-jump. allowed_jumps should include the initial jump. From 8ad803b114b40b3850829971e1a80703e0144cbd Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sun, 11 Feb 2024 19:14:56 +0200 Subject: [PATCH 05/14] fixed grammer in jump method docstring --- arcade/physics_engines.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 4d80f1e85..a6fc16082 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -382,11 +382,10 @@ def jump(self, velocity: int, air_jump_velocity: Optional[int] = None, air_jump_style: Optional[str] = "set", jump_velocity_limit: Optional[int] = None): - """ Have the character jump. Multijump can be set with a separate in-air velocity and can be air jumps can be - set to be additive, limited, or a set value. Additive only adds to the player's upward velocity. Limited will - set or add to the player's velocity. If the player is falling, then their velocity will be set to their air - jump speed. Otherwise, it will add their air jump speed up until the jump_velocity limit. Set always sets the - player's velocity to their air jump speed. """ + """ Have the character jump. Multijump can be set with a separate in-air velocity and air jumps can be + set to be additive, limited, or a set value. Additive only adds to the player's change_y velocity. Limited + will add to the players' change_y until the jump_velocity limit. Set always sets the players velocity + to their air jump speed. """ if self.can_jump(): # Air Jump logic if self.jumps_since_ground > 0: From 7d1f1a89ca7c6ef4af863832298bd124f7243987 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 17 Feb 2024 14:54:46 +0200 Subject: [PATCH 06/14] added is_on_ground method This is integrated into can_jump, but also can be used independently of it so devs can do checks that allow them to determine how to jump off ladders. --- arcade/physics_engines.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 6e18e824f..5df1201b3 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -340,13 +340,12 @@ def is_on_ladder(self): return True return False - def can_jump(self, y_distance: float = 5) -> bool: + def is_on_ground(self, y_distance: float = 5) -> bool: """ Method that looks to see if there is a floor under - the player_sprite. If there is a floor, the player can jump - and we return a True. + the player_sprite. If there is a floor, we return a True. - :returns: True if there is a platform below us + :returns: True if there is a platform below us. """ # Move down to see if we are on a platform @@ -359,13 +358,25 @@ def can_jump(self, y_distance: float = 5) -> bool: if len(hit_list) > 0: self.jumps_since_ground = 0 + return True + else: + return False + + def can_jump(self, y_distance: float = 5) -> bool: + """ + Method that looks to see if there is a floor under the player_sprite or the conditions + for multijump are true. - if (len(hit_list) > 0 or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and + :returns: True if there is a platform below us or if multijump jump conditions are met. + """ + + if (self.is_on_ground(y_distance) or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and self.jump_ticks >= self.jump_delay): return True else: return False + def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 0): """ Enables multi-jump. From a9a016f2123e0145afd7ff772748e991ddc7baaf Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 13:28:16 +0300 Subject: [PATCH 07/14] added jumping off of and to ladders --- arcade/physics_engines.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index d2092d57c..8c4085077 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -380,7 +380,8 @@ def is_on_ladder(self): # Check for touching a ladder if self.ladders: hit_list = check_for_collision_with_lists(self.player_sprite, self.ladders) - if len(hit_list) > 0: + if len(hit_list) > 0 and self.jump_ticks >= self.jump_delay: + self.jumps_since_ground = 0 return True return False @@ -408,10 +409,11 @@ def is_on_ground(self, y_distance: float = 5) -> bool: def can_jump(self, y_distance: float = 5) -> bool: """ - Method that looks to see if there is a floor under the player_sprite or the conditions - for multijump are true. + Method that looks to see if there is a floor under + the player_sprite. If there is a floor, the player can jump + and we return a True. - :returns: True if there is a platform below us or if multijump jump conditions are met. + :returns: True if there is a platform below us """ if (self.is_on_ground(y_distance) or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and @@ -420,8 +422,7 @@ def can_jump(self, y_distance: float = 5) -> bool: else: return False - - def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 0): + def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 10): """ Enables multi-jump. allowed_jumps should include the initial jump. @@ -511,7 +512,6 @@ def update(self): # print(f"Spot F ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # print(f"Spot B ({self.player_sprite.center_x}, {self.player_sprite.center_y})") - if self.allow_multi_jump: if self.jump_ticks < self.jump_delay: self.jump_ticks += 1 From d4db1f93a6de4a52f7af08923a1dfa28d25b905a Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 13:36:06 +0300 Subject: [PATCH 08/14] Fixed so it will pass tests I changed the default jump_delay to 0 so that the test will properly see a true value for can_jump with multijump. I argue that 10 ticks is a better number than 0 as a default but that will require updating the test. --- arcade/physics_engines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 8c4085077..e430826a2 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -422,7 +422,7 @@ def can_jump(self, y_distance: float = 5) -> bool: else: return False - def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 10): + def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 0): """ Enables multi-jump. allowed_jumps should include the initial jump. From a2f3733333c25aeb00e3d440381f83c4d52ff0a9 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 13:58:57 +0300 Subject: [PATCH 09/14] fixed single jumping off of ladders --- arcade/physics_engines.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index e430826a2..5371c0ad9 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -298,6 +298,7 @@ def __init__(self, gravity_constant: float = 0.5, ladders: Optional[Union[SpriteList, Iterable[SpriteList]]] = None, walls: Optional[Union[SpriteList, Iterable[SpriteList]]] = None, + jump_delay: int = 0 ): self._ladders: Optional[List[SpriteList]] self._platforms: List[SpriteList] @@ -322,8 +323,8 @@ def __init__(self, self.gravity_constant: float = gravity_constant self.jumps_since_ground: int = 0 self.allowed_jumps: int = 1 - self.jump_delay: int = 0 - self.jump_ticks: int = 0 + self.jump_delay = jump_delay + self.jump_ticks = jump_delay self.allow_multi_jump: bool = False # The property object for ladders. This allows us setter/getter/deleter capabilities in safe manner @@ -416,13 +417,13 @@ def can_jump(self, y_distance: float = 5) -> bool: :returns: True if there is a platform below us """ - if (self.is_on_ground(y_distance) or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and + if (self.is_on_ground(y_distance) or self.is_on_ladder() or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and self.jump_ticks >= self.jump_delay): return True else: return False - def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 0): + def enable_multi_jump(self, allowed_jumps: int): """ Enables multi-jump. allowed_jumps should include the initial jump. @@ -435,8 +436,6 @@ def enable_multi_jump(self, allowed_jumps: int, jump_delay: int = 0): :param jump_delay: Number of ticks before another jump is allowed """ self.allowed_jumps = allowed_jumps - self.jump_delay = jump_delay - self.jump_ticks = jump_delay self.allow_multi_jump = True def disable_multi_jump(self): @@ -494,7 +493,7 @@ def increment_jump_counter(self): """ if self.allow_multi_jump: self.jumps_since_ground += 1 - self.jump_ticks = 0 + self.jump_ticks = 0 def update(self): """ @@ -512,9 +511,8 @@ def update(self): # print(f"Spot F ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # print(f"Spot B ({self.player_sprite.center_x}, {self.player_sprite.center_y})") - if self.allow_multi_jump: - if self.jump_ticks < self.jump_delay: - self.jump_ticks += 1 + if self.jump_ticks < self.jump_delay: + self.jump_ticks += 1 for platform_list in self.platforms: for platform in platform_list: From 0754e7ea31c54a222327373c03cf4764d13aa2dd Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 14:02:12 +0300 Subject: [PATCH 10/14] fixed long line --- arcade/physics_engines.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 5371c0ad9..d1cd8d01c 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -417,7 +417,8 @@ def can_jump(self, y_distance: float = 5) -> bool: :returns: True if there is a platform below us """ - if (self.is_on_ground(y_distance) or self.is_on_ladder() or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps and + if (self.is_on_ground(y_distance) or self.is_on_ladder() or self.allow_multi_jump and + self.jumps_since_ground < self.allowed_jumps and self.jump_ticks >= self.jump_delay): return True else: From 6f23b5823d181851d82766a062bb91b0e8eb94b5 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 21:20:37 +0300 Subject: [PATCH 11/14] simplified hit_list check --- arcade/physics_engines.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index d1cd8d01c..f13e94ede 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -381,7 +381,7 @@ def is_on_ladder(self): # Check for touching a ladder if self.ladders: hit_list = check_for_collision_with_lists(self.player_sprite, self.ladders) - if len(hit_list) > 0 and self.jump_ticks >= self.jump_delay: + if hit_list and self.jump_ticks >= self.jump_delay: self.jumps_since_ground = 0 return True return False @@ -402,7 +402,7 @@ def is_on_ground(self, y_distance: float = 5) -> bool: self.player_sprite.center_y += y_distance - if len(hit_list) > 0: + if hit_list: self.jumps_since_ground = 0 return True else: From 53b4d751822525278fbdbd5ad30ad1eeeaf9848d Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 21:39:32 +0300 Subject: [PATCH 12/14] reduced checks --- arcade/physics_engines.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index f13e94ede..f1f40dece 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -450,7 +450,7 @@ def disable_multi_jump(self): self.allowed_jumps = 1 self.jumps_since_ground = 0 - def jump(self, velocity: int, + def jump(self, velocity: int, air_jump_velocity: Optional[int] = None, air_jump_style: Optional[str] = "set", jump_velocity_limit: Optional[int] = None): @@ -458,27 +458,27 @@ def jump(self, velocity: int, set to be additive, limited, or a set value. Additive only adds to the player's change_y velocity. Limited will add to the players' change_y until the jump_velocity limit. Set always sets the players velocity to their air jump speed. """ + + # Sets air_jump_velocity to the same as ground jumps if no velocity is specified + if not air_jump_velocity: + air_jump_velocity = velocity + if self.can_jump(): # Air Jump logic if self.jumps_since_ground > 0: - # This checks if air_jump_velocity is set. If not it will default to the velocity for all air jumps. - if air_jump_velocity: - air_jump = air_jump_velocity - else: - air_jump = velocity if air_jump_style == "additive": - self.player_sprite.change_y += air_jump + self.player_sprite.change_y += air_jump_velocity elif air_jump_style == "limited": if not jump_velocity_limit: - jump_velocity_limit = air_jump + jump_velocity_limit = air_jump_velocity if self.player_sprite.change_y < 0: - self.player_sprite.change_y = air_jump - elif self.player_sprite.change_y + air_jump < jump_velocity_limit: - self.player_sprite.change_y += air_jump + self.player_sprite.change_y = air_jump_velocity + elif self.player_sprite.change_y + air_jump_velocity < jump_velocity_limit: + self.player_sprite.change_y += air_jump_velocity else: self.player_sprite.change_y = jump_velocity_limit elif air_jump_style == "set": - self.player_sprite.change_y = air_jump + self.player_sprite.change_y = air_jump_velocity else: raise ValueError("Air jump style set is not valid. Use additive, limited, or set.") From f6407649245a7b794dcaee8f26c7bc39a6bb0ac0 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 21:43:42 +0300 Subject: [PATCH 13/14] fixed random indent --- arcade/physics_engines.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index f1f40dece..6e170f524 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -450,10 +450,10 @@ def disable_multi_jump(self): self.allowed_jumps = 1 self.jumps_since_ground = 0 - def jump(self, velocity: int, - air_jump_velocity: Optional[int] = None, - air_jump_style: Optional[str] = "set", - jump_velocity_limit: Optional[int] = None): + def jump(self, velocity: int, + air_jump_velocity: Optional[int] = None, + air_jump_style: Optional[str] = "set", + jump_velocity_limit: Optional[int] = None): """ Have the character jump. Multijump can be set with a separate in-air velocity and air jumps can be set to be additive, limited, or a set value. Additive only adds to the player's change_y velocity. Limited will add to the players' change_y until the jump_velocity limit. Set always sets the players velocity From e7eea4d7ad40c17bb150c5f3e33e0952bd420595 Mon Sep 17 00:00:00 2001 From: FriendlyGecko <68018798+FriendlyGecko@users.noreply.github.com> Date: Sat, 25 May 2024 21:46:33 +0300 Subject: [PATCH 14/14] removed random white space --- arcade/physics_engines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index 6e170f524..ed4f0599a 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -458,7 +458,7 @@ def jump(self, velocity: int, set to be additive, limited, or a set value. Additive only adds to the player's change_y velocity. Limited will add to the players' change_y until the jump_velocity limit. Set always sets the players velocity to their air jump speed. """ - + # Sets air_jump_velocity to the same as ground jumps if no velocity is specified if not air_jump_velocity: air_jump_velocity = velocity