Example #1
0
class PhysicsManager(object):

  def __init__(self, map_data):
    self.collision_map = TileCollisionMap(map_data)
    self.collision_manager = CollisionHandler(
      self.collision_map,
      tilesize=map_data.tileheight
    )
    self.TILE_SIZE = self.collision_manager.TILE_SIZE

  def handle_tile_collision(self, phys_object, delta):
    tile_x = None
    tile_y = None

    if phys_object.velocity_x > 0:
      tile_x = self.collision_manager.collision_vert(
         phys_object.rect.x + phys_object.rect.width,
         phys_object.rect.y,
         phys_object.rect.height
      )

      if tile_x is not None:
        phys_object.rect.x = (
          (tile_x * self.TILE_SIZE) - phys_object.rect.width
        )
    elif phys_object.velocity_x < 0:
      tile_x = self.collision_manager.collision_vert(
         phys_object.rect.x,
         phys_object.rect.y,
         phys_object.rect.height
      )

      if tile_x is not None:
        phys_object.rect.x = (tile_x + 1) * self.TILE_SIZE

    if phys_object.velocity_y < 0:
      tile_y = self.collision_manager.collision_horiz_up(
         phys_object.rect.x,
         phys_object.rect.y,
         phys_object.rect.width
      )

      if tile_y is not None:
        phys_object.rect.y = (tile_y + 1) * self.TILE_SIZE
        phys_object.velocity_y = - phys_object.velocity_y / 2
    else:
      tile_y = self.collision_manager.collision_horiz_down(
         phys_object.rect.x,
         phys_object.rect.y + phys_object.rect.height,
         phys_object.rect.width
      )

      if tile_y is not None:
        phys_object.rect.y = (
          (tile_y * self.TILE_SIZE) - phys_object.rect.height
        )
        phys_object.velocity_y = 0
Example #2
0
 def __init__(self, map_data):
   self.collision_map = TileCollisionMap(map_data)
   self.collision_manager = CollisionHandler(
     self.collision_map,
     tilesize=map_data.tileheight
   )
   self.TILE_SIZE = self.collision_manager.TILE_SIZE