From 33ada24b84857fe3aa3f654112a54510f0df619d Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Tue, 28 Mar 2023 20:01:09 +0200 Subject: [PATCH] Modified collision checking sprite selection once more --- project/data/levels/level-01.csv | 4 ++-- project/physics/PhysicsElementsHandler.py | 18 ++++++++++++++++++ project/sprite/DynamicSprite.py | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/project/data/levels/level-01.csv b/project/data/levels/level-01.csv index ad2af12..eb4a382 100644 --- a/project/data/levels/level-01.csv +++ b/project/data/levels/level-01.csv @@ -5,13 +5,13 @@ #,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,C,,,# +#,#,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,L,,,,,,,,,,,,,,,,,,,,,,,,C,,,# #,#,S,,,,,,,,,,,,,,,,,,,+,+,+,+,S,S,S,S,S,+,+,+,+,+,S,S,S,S,S,+,+,+,+,+,S,S,S,S,S,+,+,+,+,+,+,+,+,+,# #,#,S,,,,,,,,,,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,S,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,S,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,,,,,,,,,,,,,,,,,,,,,,G,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,,,,,,,,,,,,,,,M,,,M,M,M,M,M,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,,,,,,,,,,,,,,,M,,,M,M,,M,,,,,,,,,,,,,,,,,M,,,,,,M,,,,,,,,,,,,,,,# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,L,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# diff --git a/project/physics/PhysicsElementsHandler.py b/project/physics/PhysicsElementsHandler.py index 667a2c0..cab0b2f 100644 --- a/project/physics/PhysicsElementsHandler.py +++ b/project/physics/PhysicsElementsHandler.py @@ -1,3 +1,4 @@ +import random from typing import Optional from physics.CollisionDirection import CollisionDirection @@ -55,6 +56,14 @@ class PhysicsElementsHandler: dynamic_sprites = [sprite for sprite in sprites if isinstance(sprite, DynamicSprite)] sorted_dynamic_sprites = sorted(dynamic_sprites, key=lambda spr: spr.position_scale.position[1]) + skip_sprites = [] + for sprite in sorted_dynamic_sprites: + if sprite.last_effective_motion[1] == 0 and sprite.last_effective_motion[0] == 0 \ + and random.randint(0, 100) > 50: + skip_sprites.append(sprite) + continue + sorted_dynamic_sprites = [sprite for sprite in sorted_dynamic_sprites if sprite not in skip_sprites] + closest_sprites: dict[UiElement, list[tuple[int, StaticSprite]]] = {} buffered_bounding_boxes: dict[UiElement, BoundingBox] = {} @@ -123,6 +132,7 @@ class PhysicsElementsHandler: collides_with_last = None collided = [False, False] + effective_motion = [0, 0] for i in range(motion_steps): if not collided[0]: @@ -132,6 +142,9 @@ class PhysicsElementsHandler: ) collides_with = self.check_collides(sprite, colliders) + if len(collides_with) == 0: + effective_motion[0] += motion_step[0] + for collider in collides_with: if collider is not None: if sprite.is_collider and collider.is_collider: @@ -176,6 +189,9 @@ class PhysicsElementsHandler: ) collides_with = self.check_collides(sprite, colliders) + if len(collides_with) == 0: + effective_motion[1] += motion_step[1] + for collider in collides_with: if collider is not None: if sprite.is_collider and collider.is_collider: @@ -213,6 +229,8 @@ class PhysicsElementsHandler: collides_with_last = collider collided[1] = True + sprite.last_effective_motion = (sprite.motion[0], sprite.motion[1]) + return collides_with_last def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite]) -> list[StaticSprite]: diff --git a/project/sprite/DynamicSprite.py b/project/sprite/DynamicSprite.py index 13c76a3..7cbc4c8 100644 --- a/project/sprite/DynamicSprite.py +++ b/project/sprite/DynamicSprite.py @@ -11,6 +11,7 @@ class DynamicSprite(StaticSprite): super().__init__(spritesheet) self.motion = (0, 0) + self.last_effective_motion = (1, 1) self.apply_base_deceleration = True self.deceleration_horizontal_air = 0.02