diff --git a/project/data/levels/level-01.csv b/project/data/levels/level-01.csv index d5ff502..ad2af12 100644 --- a/project/data/levels/level-01.csv +++ b/project/data/levels/level-01.csv @@ -11,7 +11,7 @@ #,#,S,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,S,,,,,,,,,,,,,,,,,,,,,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,#,,,,,,,,,,,,,,,,,,,,,,G,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,,,,,,,,,,,,,,,M,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# +#,,,,,,,,,,,,,,,M,,,M,M,M,M,M,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,+,+,+,+,+,+,+,+,+,+,+,+,+,+,+,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,L,,,,,,,,,,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# diff --git a/project/physics/PhysicsElementsHandler.py b/project/physics/PhysicsElementsHandler.py index 42a176f..667a2c0 100644 --- a/project/physics/PhysicsElementsHandler.py +++ b/project/physics/PhysicsElementsHandler.py @@ -1,17 +1,15 @@ -import math -import time from typing import Optional from physics.CollisionDirection import CollisionDirection from physics.TickData import TickData from sprite.BoundingBox import BoundingBox from sprite.DynamicSprite import DynamicSprite -from sprite.PositionScale import PositionScale from sprite.Sprite import Sprite from sprite.StaticSprite import StaticSprite from ui_elements.UiElement import UiElement -MAX_COLLIDER_CHECK_SPRITES = 15 +MAX_COLLIDER_DISTANCE = 50 +MAX_COLLIDER_CHECK_SPRITES = 10 MOTION_STEPS = 10 TOLERANCE = 1 @@ -38,14 +36,15 @@ class PhysicsElementsHandler: # 1. Find all sprites that have collision enabled and store them in a list # 2. Create a list of all sprites that are dynamic sprites # 3. Sort the sprites by their y position - # 3. Find the MAX_COLLIDER_CHECK_SPRITES sprites that are closest to the sprite that are colliders but not the + # 4. Find the MAX_COLLIDER_CHECK_SPRITES sprites that are closest to the sprite that are colliders but not the # sprite itself - # 4. For each sprite: - # 4.1. Divide the motion into MOTION_STEPS steps - # 4.2. For each step: - # 4.2.1. Check if the sprite collides with any other sprite - # 4.2.2. If it does, move the sprite back to the previous position and stop the motion - # 4.2.3. If it doesn't, move the sprite to the new position + # 4.1 Filter out all that are further than MAX_COLLIDER_DISTANCE + # 5. For each sprite: + # 5.1. Divide the motion into MOTION_STEPS steps + # 5.2. For each step: + # 5.2.1. Check if the sprite collides with any other sprite from the list of colliders generated in step 4 + # 5.2.2. If it does, move the sprite back to the previous position and stop the motion + # 5.2.3. If it doesn't, move the sprite to the new position colliders = [sprite for sprite in sprites if isinstance(sprite, StaticSprite) and (sprite.is_collider or sprite.register_collisions)] @@ -89,6 +88,10 @@ class PhysicsElementsHandler: if max_index != -1: current_closest_sprites[max_index] = (distance, collider) + for sprite in closest_sprites: + closest_sprites[sprite] = [closest_sprite for closest_sprite in closest_sprites[sprite] + if closest_sprite[0] < MAX_COLLIDER_DISTANCE] + # set visible false for all those that are not in the closest_sprites list # for collider in colliders: # found = False @@ -212,29 +215,14 @@ class PhysicsElementsHandler: return collides_with_last - def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite]) -> \ - list[StaticSprite]: + def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite]) -> list[StaticSprite]: collides_with = [] for collider in colliders: if sprite is not collider: - distance = self.calculate_basic_distance(sprite, collider) - if distance > 50: - continue if sprite.collides_with(collider, TOLERANCE): collides_with.append(collider) if len(collides_with) > 5: break return collides_with - - def calculate_basic_distance(self, sprite1: StaticSprite, sprite2: StaticSprite) -> float: - return math.sqrt((sprite1.position_scale.position[0] - sprite2.position_scale.position[0]) ** 2 + - (sprite1.position_scale.position[1] - sprite2.position_scale.position[1]) ** 2) - - def get_sprite_size_for_distance(self, ui_element: UiElement, screen_transform: PositionScale) -> int: - image = ui_element.render_sprite_image() - if image is None: - return 0 - return max(image.get_width() * ui_element.position_scale.scale[0] * screen_transform.scale[0], - image.get_height() * ui_element.position_scale.scale[1] * screen_transform.scale[1])