runRandomMovement method
void
runRandomMovement(
- double dt, {
- double? speed,
- double maxDistance = 50,
- double minDistance = 25,
- int timeKeepStopped = 2000,
- bool updateAngle = false,
- bool checkDirectionWithRayCast = false,
- RandomMovementDirections directions = RandomMovementDirections.all,
- dynamic onStartMove(
- Direction direction
- 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();
}
}
}