seeAndMoveToAttackRange method
void
seeAndMoveToAttackRange({})
Checks whether the player is within range. If so, move to it.
visionAngle
in radians
angle
in radians. is automatically picked up using the component's direction.
Implementation
void seeAndMoveToAttackRange({
Function(Player)? positioned,
// return true to stop move.
BoolCallback? notObserved,
Function(Player)? observed,
double radiusVision = 32,
double? visionAngle,
double? angle,
double? minDistanceFromPlayer,
bool useDiagonal = true,
// bool useDiagonal = true,
}) {
if (minDistanceFromPlayer != null) {
assert(minDistanceFromPlayer < radiusVision);
}
if (isDead) return;
seePlayer(
radiusVision: radiusVision,
visionAngle: visionAngle,
angle: angle,
observed: (player) {
observed?.call(player);
double minD = (minDistanceFromPlayer ?? (radiusVision - 5));
if (useDiagonal) {
bool inDistance = keepDistance(
player,
minD,
);
if (inDistance) {
final playerDirection = getComponentDirectionFromMe(player);
lastDirection = playerDirection;
if (lastDirection == Direction.left ||
lastDirection == Direction.right) {
lastDirectionHorizontal = lastDirection;
}
if (checkInterval('seeAndMoveToAttackRange', 500, dtUpdate)) {
stopMove();
}
positioned?.call(player);
}
} else {
positionsItselfAndKeepDistance(
player,
minDistanceFromPlayer: minD,
radiusVision: radiusVision,
positioned: (player) {
final playerDirection = getComponentDirectionFromMe(player);
lastDirection = playerDirection;
if (lastDirection == Direction.left ||
lastDirection == Direction.right) {
lastDirectionHorizontal = lastDirection;
}
if (checkInterval('seeAndMoveToAttackRange', 500, dtUpdate)) {
stopMove();
}
positioned?.call(player);
},
);
}
},
notObserved: () {
bool stop = notObserved?.call() ?? true;
if (stop) {
stopMove(forceIdle: true);
}
},
);
}