110 lines
4.1 KiB
Python
110 lines
4.1 KiB
Python
from typing import Optional
|
|
|
|
from sprite.DynamicSprite import DynamicSprite
|
|
from sprite.Sprite import Sprite
|
|
from sprite.StaticSprite import StaticSprite
|
|
from ui_elements.UiElement import UiElement
|
|
|
|
MOTION_STEPS = 10
|
|
TOLERANCE = 1
|
|
|
|
|
|
class PhysicsElementsHandler:
|
|
def __init__(self):
|
|
self.collision_callbacks = []
|
|
|
|
def add_collision_callback(self, callback):
|
|
self.collision_callbacks.append(callback)
|
|
|
|
def remove_collision_callback(self, callback):
|
|
self.collision_callbacks.remove(callback)
|
|
|
|
def tick(self, dt: float, layers: dict[str, list[UiElement]]):
|
|
sprites = []
|
|
for layer in layers:
|
|
for sprite in layers[layer]:
|
|
sprite.tick(dt)
|
|
if isinstance(sprite, Sprite):
|
|
sprites.append(sprite)
|
|
|
|
# handle motion and collisions. To do this:
|
|
# 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
|
|
# 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
|
|
|
|
colliders = [sprite for sprite in sprites if isinstance(sprite, StaticSprite) and sprite.is_collider]
|
|
|
|
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])
|
|
|
|
for sprite in sorted_dynamic_sprites:
|
|
collides_with = self.attempt_move(dt, sprite, colliders, MOTION_STEPS)
|
|
if collides_with is not None:
|
|
for callback in self.collision_callbacks:
|
|
callback(sprite, collides_with)
|
|
|
|
def attempt_move(self, dt: float, sprite: DynamicSprite, colliders: list[StaticSprite],
|
|
motion_steps: int) -> Optional[StaticSprite]:
|
|
total_motion = sprite.motion
|
|
motion_step = ((total_motion[0] * dt) / motion_steps, (total_motion[1] * dt) / motion_steps)
|
|
|
|
for i in range(motion_steps):
|
|
sprite.reset_touches()
|
|
|
|
sprite.position_scale.position = (
|
|
sprite.position_scale.position[0] + motion_step[0],
|
|
sprite.position_scale.position[1]
|
|
)
|
|
|
|
collides_with = self.check_collides(sprite, colliders)
|
|
if collides_with is not None:
|
|
sprite.position_scale.position = (
|
|
sprite.position_scale.position[0] - motion_step[0],
|
|
sprite.position_scale.position[1]
|
|
)
|
|
|
|
if sprite.motion[0] > 0:
|
|
sprite.set_touches_right(True)
|
|
|
|
if sprite.motion[0] < 0:
|
|
sprite.set_touches_left(True)
|
|
|
|
sprite.motion = (0, sprite.motion[1])
|
|
return collides_with
|
|
|
|
sprite.position_scale.position = (
|
|
sprite.position_scale.position[0],
|
|
sprite.position_scale.position[1] + motion_step[1]
|
|
)
|
|
|
|
collides_with = self.check_collides(sprite, colliders)
|
|
if collides_with is not None:
|
|
sprite.position_scale.position = (
|
|
sprite.position_scale.position[0],
|
|
sprite.position_scale.position[1] - motion_step[1]
|
|
)
|
|
|
|
if sprite.motion[1] > 0:
|
|
sprite.set_touches_bottom(True)
|
|
|
|
if sprite.motion[1] < 0:
|
|
sprite.set_touches_top(True)
|
|
|
|
sprite.motion = (sprite.motion[0], 0)
|
|
return collides_with
|
|
|
|
return None
|
|
|
|
def check_collides(self, sprite: StaticSprite, colliders: list[StaticSprite]) -> Optional[StaticSprite]:
|
|
for collider in colliders:
|
|
if sprite is not collider and sprite.collides_with(collider, TOLERANCE):
|
|
return collider
|
|
|
|
return None
|