update method

void update(
  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,
})

Executes random movement. Should be called inside the component's update method.

Implementation

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

  /// milliseconds
  int timeKeepStopped = 2000,
  bool updateAngle = false,
  bool checkDirectionWithRayCast = false,
  RandomMovementDirections directions = RandomMovementDirections.all,
}) {
  if (_distanceToArrived == null) {
    if (_comp.checkInterval(_intervalKeepStoppedKey, timeKeepStopped, dt)) {
      final target = _getTarget(
        minDistance,
        maxDistance,
        checkDirectionWithRayCast,
        directions,
      );
      if (target == null) {
        _stop();
        return;
      }
      _currentDirection = target.direction;
      _distanceToArrived = target.distance;
      _originPosition = _comp.absoluteCenter.clone();
      _notifyStartMove(_currentDirection);
    }
  } else {
    _travelledDistance = _comp.absoluteCenter.distanceTo(_originPosition);
    final isCanMove = _comp.canMove(_currentDirection, displacement: speed);
    if (_travelledDistance >= _distanceToArrived! || !isCanMove) {
      _stop();
      return;
    }

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