seeAndMoveToAttackRange method

void seeAndMoveToAttackRange({
  1. dynamic positioned(
    1. Player
    )?,
  2. BoolCallback? notObserved,
  3. dynamic observed(
    1. Player
    )?,
  4. double radiusVision = 32,
  5. double? visionAngle,
  6. double? angle,
  7. double? minDistanceFromPlayer,
  8. bool useDiagonal = true,
})

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

void seeAndMoveToAttackRange({
  Function(Player)? positioned,
  // return true to stop move.
  BoolCallback? notObserved,
  Function(Player)? observed,
  double radiusVision = 32,
  double? visionAngle,
  double? angle,
  double? minDistanceFromPlayer,
  bool useDiagonal = true,
  // bool useDiagonal = true,
}) {
  if (minDistanceFromPlayer != null) {
    assert(minDistanceFromPlayer < radiusVision);
  }

  if (isDead) return;

  seePlayer(
    radiusVision: radiusVision,
    visionAngle: visionAngle,
    angle: angle,
    observed: (player) {
      observed?.call(player);
      double minD = (minDistanceFromPlayer ?? (radiusVision - 5));
      if (useDiagonal) {
        bool inDistance = keepDistance(
          player,
          minD,
        );
        if (inDistance) {
          final playerDirection = getComponentDirectionFromMe(player);
          lastDirection = playerDirection;
          if (lastDirection == Direction.left ||
              lastDirection == Direction.right) {
            lastDirectionHorizontal = lastDirection;
          }

          if (checkInterval('seeAndMoveToAttackRange', 500, dtUpdate)) {
            stopMove();
          }
          positioned?.call(player);
        }
      } else {
        positionsItselfAndKeepDistance(
          player,
          minDistanceFromPlayer: minD,
          radiusVision: radiusVision,
          positioned: (player) {
            final playerDirection = getComponentDirectionFromMe(player);
            lastDirection = playerDirection;
            if (lastDirection == Direction.left ||
                lastDirection == Direction.right) {
              lastDirectionHorizontal = lastDirection;
            }

            if (checkInterval('seeAndMoveToAttackRange', 500, dtUpdate)) {
              stopMove();
            }
            positioned?.call(player);
          },
        );
      }
    },
    notObserved: () {
      bool stop = notObserved?.call() ?? true;
      if (stop) {
        stopMove(forceIdle: true);
      }
    },
  );
}