seeAndMoveToPlayer method
void
seeAndMoveToPlayer({
- required dynamic closePlayer(),
- BoolCallback? notObserved,
- double radiusVision = 32,
- double margin = 10,
- bool runOnlyVisibleInScreen = true,
Checks whether the player is within range. If so, move to it.
Implementation
void seeAndMoveToPlayer({
required Function(Player) closePlayer,
// return true to stop move.
BoolCallback? notObserved,
double radiusVision = 32,
double margin = 10,
bool runOnlyVisibleInScreen = true,
}) {
if (isDead) return;
if (runOnlyVisibleInScreen && !isVisible) return;
seePlayer(
radiusVision: radiusVision,
observed: (player) {
double radAngle = getAngleFromPlayer();
Rect playerRect = player.rectCollision;
Rect rectPlayerCollision = playerRect.inflate(margin);
if (rectCollision.overlaps(rectPlayerCollision)) {
closePlayer(player);
moveFromAngle(radAngle);
stopMove();
return;
}
moveFromAngle(radAngle);
},
notObserved: () {
bool stop = notObserved?.call() ?? true;
if (stop) {
stopMove();
}
},
);
}