seeComponentType<T extends GameComponent> method

Shape? seeComponentType<T extends GameComponent>({
  1. required dynamic observed(
    1. List<T>
    ),
  2. VoidCallback? notObserved,
  3. double radiusVision = 32,
  4. double? visionAngle,
  5. double angle = 3.14159,
})

This method we notify when detect components by type when enter in radiusVision configuration Method that bo used in update method. visionAngle in radians angle in radians.

Implementation

Shape? seeComponentType<T extends GameComponent>({
  required Function(List<T>) observed,
  VoidCallback? notObserved,
  double radiusVision = 32,
  double? visionAngle,
  double angle = 3.14159,
}) {
  var compVisible = gameRef.visibleComponentsByType<T>();

  if (compVisible.isEmpty) {
    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;
  }

  List<T> compObserved = compVisible.where((comp) {
    final rect = comp.rectConsideringCollision;
    final otherShape = RectangleShape(
      rect.sizeVector2,
      position: rect.positionVector2,
    );
    return !comp.isRemoving && shape.isCollision(otherShape);
  }).toList();

  if (compObserved.isNotEmpty) {
    observed(compObserved);
  } else {
    notObserved?.call();
  }

  return _currentShape = shape;
}