def calculate_hitbox(self, align_center=False): points_base = self.points # Calculate bounding rect leftmost, rightmost, topmost, bottommost = 1000, -1000, 1000, -1000 for point in points_base: leftmost = min(leftmost, point.x) rightmost = max(rightmost, point.x) topmost = min(topmost, point.y) bottommost = max(bottommost, point.y) width, height = rightmost - leftmost, bottommost - topmost # Adjust center basepos = self.pos[:2] center = Vector(leftmost + width / 2 + basepos.x, topmost + height / 2 + basepos.y) if align_center: center.to_dict(self._dict["m_Pos"]) self.points = (points_base := [point + basepos - center for point in points_base]) leftmost, rightmost = [x + basepos.x - center.x for x in (leftmost, rightmost)] topmost, bottommost = [y + basepos.y - center.y for y in (topmost, bottommost)] self._center_offset = (0, 0) else: self._center_offset = basepos - center # Create hitbox bitmap offset = (- leftmost, topmost) points_hitbox = [(HITBOX_RESOLUTION * (point + offset).flip_y()).round() for point in points_base] surface = Surface((HITBOX_RESOLUTION * width + 1, HITBOX_RESOLUTION * height + 1), pygame.SRCALPHA, 32) pygame.draw.polygon(surface, BLACK, points_hitbox) self._hitbox = mask_from_surface(surface)
def scale(self, value: Vector): old_scale = self.scale value.to_dict(self._dict["m_Scale"]) change = (value / old_scale)[:2] if abs(change.x - 1) > 0.000001 or abs(change.y - 1) > 0.000001: basepos, rot = self.pos[:2], self.rotation for pin in self.static_pins: ((Vector(pin).rotate(-rot, basepos) - basepos) * change + basepos).rotate(rot, basepos).to_dict(pin) for anchor in self.anchors: anchor.pos = ((anchor.pos.rotate(-rot, basepos) - basepos) * change + basepos).rotate(rot, basepos)
def pos(self, value: Vector): value.to_dict(self._dict["m_Pos"])