seeAndMoveToPlayer method

void seeAndMoveToPlayer({
  1. required dynamic closePlayer(
    1. Player
    ),
  2. BoolCallback? notObserved,
  3. double radiusVision = 32,
  4. double margin = 10,
  5. bool runOnlyVisibleInScreen = true,
})

Checks whether the player is within range. If so, move to it.

Implementation

void seeAndMoveToPlayer({
  required Function(Player) closePlayer,
  // return true to stop move.
  BoolCallback? notObserved,
  double radiusVision = 32,
  double margin = 10,
  bool runOnlyVisibleInScreen = true,
}) {
  if (isDead) return;
  if (runOnlyVisibleInScreen && !isVisible) return;

  seePlayer(
    radiusVision: radiusVision,
    observed: (player) {
      double radAngle = getAngleFromPlayer();

      Rect playerRect = player.rectCollision;
      Rect rectPlayerCollision = playerRect.inflate(margin);

      if (rectCollision.overlaps(rectPlayerCollision)) {
        closePlayer(player);
        moveFromAngle(radAngle);
        stopMove();
        return;
      }

      moveFromAngle(radAngle);
    },
    notObserved: () {
      bool stop = notObserved?.call() ?? true;
      if (stop) {
        stopMove();
      }
    },
  );
}