diff --git a/project/physics/sprites/PlayerSprite.py b/project/physics/sprites/PlayerSprite.py index 42ef968..ff293d8 100644 --- a/project/physics/sprites/PlayerSprite.py +++ b/project/physics/sprites/PlayerSprite.py @@ -1,3 +1,4 @@ +from level.elements.dynamic.PushableBoxLevelElement import PushableBoxLevelElement from physics.CollisionDirection import CollisionDirection from physics.TickData import TickData from sprite.DynamicSprite import DynamicSprite @@ -25,9 +26,36 @@ class PlayerSprite(DynamicSprite): self.no_motion_since = 100 self.last_directional_input = KeyManager.KEY_RIGHT + self.is_potentially_stuck = 0 + def tick(self, tick_data: TickData): super().tick(tick_data) + left = self.get_collides_with_direction(CollisionDirection.LEFT) + right = self.get_collides_with_direction(CollisionDirection.RIGHT) + bottom = self.get_collides_with_direction(CollisionDirection.BOTTOM) + + if self.is_potentially_stuck == 0 and left and bottom: + if left.secondary_sprite == bottom.secondary_sprite: + if isinstance(left.secondary_sprite, PushableBoxLevelElement): + self.is_potentially_stuck = 1 + else: + self.is_potentially_stuck = 0 + else: + self.is_potentially_stuck = 0 + elif self.is_potentially_stuck == 1 and right and bottom: + if right.secondary_sprite == bottom.secondary_sprite: + if isinstance(right.secondary_sprite, PushableBoxLevelElement): + self.is_potentially_stuck = 2 + else: + self.is_potentially_stuck = 0 + else: + self.is_potentially_stuck = 0 + + if self.is_potentially_stuck == 2: + print('stuck') + self.position_scale.position = (self.position_scale.position[0] - 5, self.position_scale.position[1]) + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT): self.last_directional_input = KeyManager.KEY_RIGHT if self.motion[0] < self.max_motion_horizontal_via_input: diff --git a/project/sprite/Sprite.py b/project/sprite/Sprite.py index 8116c5e..e4b321e 100644 --- a/project/sprite/Sprite.py +++ b/project/sprite/Sprite.py @@ -47,6 +47,10 @@ class Sprite(UiElement): return collision_direction return None + def get_all_collides_with_direction(self, direction: int) -> list[CollisionDirection]: + return [collision_direction for collision_direction in self.collides_with_elements if + collision_direction.direction == direction] + def tick(self, tick_data: TickData): self.update_image(tick_data)