seeAndMoveToPlayer method

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

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

Implementation

Shape? seeAndMoveToPlayer({
  Function(Player)? closePlayer,
  // return true to stop move.
  BoolCallback? notObserved,
  VoidCallback? observed,
  VoidCallback? notCanMove,
  double radiusVision = 32,
  double margin = 2,
  double? visionAngle,
  double? angle,
  bool runOnlyVisibleInScreen = true,
  MovementAxis movementAxis = MovementAxis.all,
}) {
  if (runOnlyVisibleInScreen && !isVisible) return null;

  return seePlayer(
    radiusVision: radiusVision,
    visionAngle: visionAngle,
    angle: angle,
    observed: (player) {
      observed?.call();
      bool move = moveTowardsTarget(
        target: player,
        close: () => closePlayer?.call(player),
        margin: margin,
        movementAxis: movementAxis,
      );
      if (!move) {
        notCanMove?.call();
      }
    },
    notObserved: () {
      bool stop = notObserved?.call() ?? true;
      if (stop) {
        stopMove();
      }
    },
  );
}