runRandomMovement method

void runRandomMovement(
  1. double dt, {
  2. double? speed,
  3. double maxDistance = 50,
  4. double minDistance = 25,
  5. int timeKeepStopped = 2000,
  6. bool updateAngle = false,
  7. bool checkDirectionWithRayCast = false,
  8. RandomMovementDirections directions = RandomMovementDirections.all,
  9. dynamic onStartMove(
    1. Direction direction
    )?,
  10. dynamic onStopMove()?,
})

Method that bo used in update method.

Implementation

void runRandomMovement(
  double dt, {
  double? speed,
  double maxDistance = 50,
  double minDistance = 25,

  /// milliseconds
  int timeKeepStopped = 2000,
  bool updateAngle = false,
  bool checkDirectionWithRayCast = false,
  RandomMovementDirections directions = RandomMovementDirections.all,
  Function(Direction direction)? onStartMove,
  Function()? onStopMove,
}) {
  _onStartMove = onStartMove;
  _onStopMove = onStopMove;

  if (_distanceToArrived == null) {
    if (checkInterval(_KEY_INTERVAL_KEEP_STOPPED, timeKeepStopped, dt)) {
      final target = _getTarget(
        minDistance,
        maxDistance,
        checkDirectionWithRayCast,
        directions,
      );
      if (target == null) {
        _stop();
        return;
      }
      _currentDirection = target.direction;
      _distanceToArrived = target.distance;
      _originPosition = absoluteCenter.clone();
      _onStartMove?.call(_currentDirection);
    }
  } else {
    _travelledDistance = absoluteCenter.distanceTo(_originPosition);
    final isCanMove = canMove(_currentDirection, displacement: speed);
    if (_travelledDistance >= _distanceToArrived! || !isCanMove) {
      _stop();
      return;
    }

    moveFromDirection(_currentDirection, speed: speed);
    if (updateAngle) {
      angle = _currentDirection.toRadians();
    }
  }
}