seeComponentType<T extends GameComponent> method
Shape?
seeComponentType<T extends GameComponent>({
- required dynamic observed(
- List<
T>
- List<
- VoidCallback? notObserved,
- double radiusVision = 32,
- double? visionAngle,
- 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;
}