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,,,,,,,,,,#

1 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
11 # # S # #
12 # # S # #
13 # # G #
14 # M M M M M M #
15 # #
16 # + + + + + + + + + + + + + + + #
17 # L # # # # # # # # # # # # # # # #

View File

@ -1,17 +1,15 @@
import math
import time
from typing import Optional from typing import Optional
from physics.CollisionDirection import CollisionDirection from physics.CollisionDirection import CollisionDirection
from physics.TickData import TickData from physics.TickData import TickData
from sprite.BoundingBox import BoundingBox from sprite.BoundingBox import BoundingBox
from sprite.DynamicSprite import DynamicSprite from sprite.DynamicSprite import DynamicSprite
from sprite.PositionScale import PositionScale
from sprite.Sprite import Sprite from sprite.Sprite import Sprite
from sprite.StaticSprite import StaticSprite from sprite.StaticSprite import StaticSprite
from ui_elements.UiElement import UiElement from ui_elements.UiElement import UiElement
MAX_COLLIDER_CHECK_SPRITES = 15 MAX_COLLIDER_DISTANCE = 50
MAX_COLLIDER_CHECK_SPRITES = 10
MOTION_STEPS = 10 MOTION_STEPS = 10
TOLERANCE = 1 TOLERANCE = 1
@ -38,14 +36,15 @@ class PhysicsElementsHandler:
# 1. Find all sprites that have collision enabled and store them in a list # 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 # 2. Create a list of all sprites that are dynamic sprites
# 3. Sort the sprites by their y position # 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 # sprite itself
# 4. For each sprite: # 4.1 Filter out all that are further than MAX_COLLIDER_DISTANCE
# 4.1. Divide the motion into MOTION_STEPS steps # 5. For each sprite:
# 4.2. For each step: # 5.1. Divide the motion into MOTION_STEPS steps
# 4.2.1. Check if the sprite collides with any other sprite # 5.2. For each step:
# 4.2.2. If it does, move the sprite back to the previous position and stop the motion # 5.2.1. Check if the sprite collides with any other sprite from the list of colliders generated in step 4
# 4.2.3. If it doesn't, move the sprite to the new position # 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 colliders = [sprite for sprite in sprites
if isinstance(sprite, StaticSprite) and (sprite.is_collider or sprite.register_collisions)] if isinstance(sprite, StaticSprite) and (sprite.is_collider or sprite.register_collisions)]
@ -89,6 +88,10 @@ class PhysicsElementsHandler:
if max_index != -1: if max_index != -1:
current_closest_sprites[max_index] = (distance, collider) 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 # set visible false for all those that are not in the closest_sprites list
# for collider in colliders: # for collider in colliders:
# found = False # found = False
@ -212,29 +215,14 @@ class PhysicsElementsHandler:
return collides_with_last return collides_with_last
def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite]) -> \ def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite]) -> list[StaticSprite]:
list[StaticSprite]:
collides_with = [] collides_with = []
for collider in colliders: for collider in colliders:
if sprite is not collider: if sprite is not collider:
distance = self.calculate_basic_distance(sprite, collider)
if distance > 50:
continue
if sprite.collides_with(collider, TOLERANCE): if sprite.collides_with(collider, TOLERANCE):
collides_with.append(collider) collides_with.append(collider)
if len(collides_with) > 5: if len(collides_with) > 5:
break break
return collides_with 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])