seeAndMoveToAttackRange method
void
seeAndMoveToAttackRange({})
Checks whether the Enemy 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(Enemy)? positioned,
// return true to stop move.
BoolCallback? notObserved,
Function(Enemy)? observed,
double radiusVision = 32,
double? visionAngle,
double? angle,
double? minDistanceFromPlayer,
bool runOnlyVisibleInScreen = true,
}) {
if (isDead) return;
seeComponentType<Enemy>(
radiusVision: radiusVision,
angle: angle ?? lastDirection.toRadians(),
visionAngle: visionAngle,
observed: (enemy) {
var e = enemy.first;
observed?.call(e);
bool inDistance = keepDistance(
e,
(minDistanceFromPlayer ?? (radiusVision - 5)),
);
if (inDistance) {
final playerDirection = getComponentDirectionFromMe(e);
lastDirection = playerDirection;
if (lastDirection == Direction.left ||
lastDirection == Direction.right) {
lastDirectionHorizontal = lastDirection;
}
if (checkInterval('seeAndMoveToAttackRange', 500, dtUpdate)) {
stopMove();
}
positioned?.call(e);
}
},
notObserved: () {
bool stop = notObserved?.call() ?? true;
if (stop) {
stopMove();
}
},
);
}