onCollision method

void onCollision(
  1. Set<Vector2> intersectionPoints,
  2. PositionComponent other
)

Implementation

void onCollision(
  Set<Vector2> intersectionPoints,
  PositionComponent other,
) {
  if (other is WithSensor || !_isEnabled) {
    return;
  }
  var stopOtherMovement = true;
  final stopMovement = other is GameComponent
      ? blockMovementHandle(intersectionPoints, other)
      : true;
  if (other is WithCollision) {
    stopOtherMovement = other.collision.blockMovementHandle(
      intersectionPoints,
      comp,
    );
  }

  if (!stopMovement || !stopOtherMovement) {
    return;
  }

  if (_collisionsResolution.containsKey(other)) {
    _onMovementBlocked(
      other,
      _collisionsResolution[other]!,
    );
    _collisionsResolution.remove(other);
    return;
  }

  final shape1 = PolygonsIntersectUtil.getCollisionShapeHitbox(
    comp.shapeHitboxes,
    intersectionPoints,
  );
  final shape2 = PolygonsIntersectUtil.getCollisionShapeHitbox(
    other.children.query<ShapeHitbox>(),
    intersectionPoints,
  );

  if (shape1 == null || shape2 == null) {
    return;
  }

  ({Vector2 normal, double depth})? colisionResult;

  if (_isPolygon(shape1)) {
    if (_isPolygon(shape2)) {
      colisionResult = PolygonsIntersectUtil.intersectPolygons(
        shape1,
        shape2,
        other,
      );
    } else if (shape2 is CircleHitbox) {
      colisionResult = PolygonsIntersectUtil.intersectCirclePolygon(
        shape1,
        shape2,
        other,
      );
    }
  } else if (shape1 is CircleHitbox) {
    if (_isPolygon(shape2)) {
      colisionResult = PolygonsIntersectUtil.intersectCirclePolygon(
        shape2,
        shape1,
        other,
        inverted: true,
      );
    } else if (shape2 is CircleHitbox) {
      colisionResult = PolygonsIntersectUtil.intersectCircles(shape1, shape2);
    }
  }

  if (colisionResult != null) {
    final data = CollisionData(
      normal: colisionResult.normal,
      depth: colisionResult.depth,
      intersectionPoints: intersectionPoints.toList(),
      direction: colisionResult.normal.toDirection(),
    );
    _onMovementBlocked(other, data);
    if (other is WithCollision) {
      other.collision.setCollisionResolution(
        comp as WithCollision,
        data.inverted(),
      );
    }
  }
}