sep-pm-platformer/project/sprite/StaticSprite.py

34 lines
1.1 KiB
Python

from typing import Optional
from sprite.Sprite import Sprite
from sprite.Spritesheet import Spritesheet
class StaticSprite(Sprite):
def __init__(self, spritesheet: Optional[Spritesheet] = None):
super().__init__(spritesheet)
def collides_with(self, collider: 'StaticSprite', tolerance: float = 0.0):
self_bounds = self.get_bounding_box()
other_bounds = collider.get_bounding_box()
self_dimensions = self_bounds.get_dimensions()
other_dimensions = other_bounds.get_dimensions()
self_position = self_bounds.get_position()
other_position = other_bounds.get_position()
if self_position[0] + self_dimensions[0] < other_position[0] + tolerance:
return False
if self_position[0] > other_position[0] + other_dimensions[0] - tolerance:
return False
if self_position[1] + self_dimensions[1] < other_position[1] + tolerance:
return False
if self_position[1] > other_position[1] + other_dimensions[1] - tolerance:
return False
return True