seeAndMoveToAttackRange method

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

Checks whether the Enemy 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(Enemy)? positioned,
  // return true to stop move.
  BoolCallback? notObserved,
  Function(Enemy)? observed,
  double radiusVision = 32,
  double? visionAngle,
  double? angle,
  double? minDistanceFromPlayer,
  bool runOnlyVisibleInScreen = true,
}) {
  if (isDead) return;

  seeComponentType<Enemy>(
    radiusVision: radiusVision,
    angle: angle ?? lastDirection.toRadians(),
    visionAngle: visionAngle,
    observed: (enemy) {
      var e = enemy.first;
      observed?.call(e);
      bool inDistance = keepDistance(
        e,
        (minDistanceFromPlayer ?? (radiusVision - 5)),
      );
      if (inDistance) {
        final playerDirection = getComponentDirectionFromMe(e);
        lastDirection = playerDirection;
        if (lastDirection == Direction.left ||
            lastDirection == Direction.right) {
          lastDirectionHorizontal = lastDirection;
        }

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