seeComponent method

Shape? seeComponent(
  1. GameComponent component, {
  2. required dynamic observed(
    1. GameComponent
    ),
  3. VoidCallback? notObserved,
  4. double radiusVision = 32,
  5. double? visionAngle,
  6. double angle = 3.14159,
})

This method we notify when detect the component when enter in radiusVision configuration Method that bo used in update method. visionAngle in radians angle in radians.

Implementation

Shape? seeComponent(
  GameComponent component, {
  required Function(GameComponent) observed,
  VoidCallback? notObserved,
  double radiusVision = 32,
  double? visionAngle,
  double angle = 3.14159,
}) {
  if (component.isRemoving) {
    notObserved?.call();
    return _currentShape = null;
  }

  String key = '$radiusVision/$visionAngle/$angle';
  PolygonShape shape;
  if (_polygonCache.containsKey(key)) {
    shape = _polygonCache[key]!;
    shape.position = center;
  } else {
    shape = _buildShape(radiusVision, visionAngle, angle, center);
    _polygonCache[key] = shape;
  }

  if (component.isRemoving) {
    notObserved?.call();
  }

  final rect = component.rectConsideringCollision;
  final otherShape = RectangleShape(
    rect.sizeVector2,
    position: rect.positionVector2,
  );

  if (shape.isCollision(otherShape)) {
    observed(component);
  } else {
    notObserved?.call();
  }
  return _currentShape = shape;
}