seeAndMoveToAlly method

void seeAndMoveToAlly({
  1. required dynamic closeAlly(
    1. Ally
    ),
  2. BoolCallback? notObserved,
  3. VoidCallback? observed,
  4. VoidCallback? notCanMove,
  5. double radiusVision = 32,
  6. double? visionAngle,
  7. double? angle,
  8. double margin = 10,
  9. bool runOnlyVisibleInScreen = true,
  10. MovementAxis movementAxis = MovementAxis.all,
})

Checks whether the ally is within range. If so, move to it. visionAngle in radians angle in radians. is automatically picked up using the component's direction.

Implementation

void seeAndMoveToAlly({
  required Function(Ally) closeAlly,
  // return true to stop move.
  BoolCallback? notObserved,
  VoidCallback? observed,
  VoidCallback? notCanMove,
  double radiusVision = 32,
  double? visionAngle,
  double? angle,
  double margin = 10,
  bool runOnlyVisibleInScreen = true,
  MovementAxis movementAxis = MovementAxis.all,
}) {
  if (runOnlyVisibleInScreen && !isVisible) return;

  seeComponentType<Ally>(
    radiusVision: radiusVision,
    visionAngle: visionAngle,
    angle: angle ?? lastDirection.toRadians(),
    observed: (ally) {
      observed?.call();
      bool move = moveTowardsTarget(
        target: ally.first,
        close: () {
          closeAlly(ally.first);
        },
        movementAxis: movementAxis,
        margin: margin,
      );
      if (!move) {
        notCanMove?.call();
      }
    },
    notObserved: () {
      bool stop = notObserved?.call() ?? true;
      if (stop) {
        stopMove();
      }
    },
  );
}