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