moveToPosition method

bool moveToPosition(
  1. Vector2 position, {
  2. double? speed,
  3. bool useCenter = true,
})

Implementation

bool moveToPosition(
  Vector2 position, {
  double? speed,
  bool useCenter = true,
}) {
  double diagonalSpeed = (speed ?? this.speed) * diaginalReduction;
  double dtSpeed = (speed ?? this.speed) * dtUpdate * 1.1;
  double dtDiagonalSpeed = diagonalSpeed * dtUpdate * 1.1;
  final rect = rectCollision;
  final compCenter = rect.centerVector2;
  final compPosition = rect.positionVector2;

  double diffX = position.x - (useCenter ? compCenter : compPosition).x;
  double diffY = position.y - (useCenter ? compCenter : compPosition).y;

  if (diffX.abs() < dtSpeed && diffY.abs() < dtSpeed) {
    return false;
  } else {
    if (diffX.abs() > dtDiagonalSpeed && diffY.abs() > dtDiagonalSpeed) {
      final minToMOve = dtDiagonalSpeed * 2;
      final xOnce = diffX.abs() / dtUpdate;
      final yOnce = diffY.abs() / dtUpdate;
      if (diffX > 0 && diffY > 0) {
        if (diffX.abs() < minToMOve) {
          moveRightOnce(speed: xOnce);
        } else if (diffY.abs() < minToMOve) {
          moveDownOnce(speed: yOnce);
        } else {
          moveDownRight(speed: speed);
        }
        return true;
      } else if (diffX < 0 && diffY > 0) {
        if (diffX.abs() < minToMOve) {
          moveLeftOnce(speed: xOnce);
        } else if (diffY.abs() < minToMOve) {
          moveDownOnce(speed: yOnce);
        } else {
          moveDownLeft(speed: speed);
        }
        return true;
      } else if (diffX > 0 && diffY < 0) {
        if (diffX.abs() < minToMOve) {
          moveRightOnce(speed: xOnce);
        } else if (diffY.abs() < minToMOve) {
          moveUpOnce(speed: yOnce);
        } else {
          moveUpRight(speed: speed);
        }
        return true;
      } else if (diffX < 0 && diffY < 0) {
        if (diffX.abs() < dtSpeed) {
          moveLeftOnce(speed: xOnce);
        } else if (diffY.abs() < dtSpeed) {
          moveUpOnce(speed: yOnce);
        } else {
          moveUpLeft(speed: speed);
        }
        return true;
      }
    } else if (diffX.abs() > dtSpeed) {
      if (diffX > 0) {
        moveRight(speed: speed);
        return true;
      } else if (diffX < 0) {
        moveLeft(speed: speed);
        return true;
      }
    } else if (diffY.abs() > dtSpeed) {
      if (diffY > 0) {
        moveDown(speed: speed);
        return true;
      } else if (diffY < 0) {
        moveUp(speed: speed);
        return true;
      }
    } else {
      translate(Vector2(diffX, diffY));
      return true;
    }
  }
  return false;
}