seeAndMoveToEnemy method
void
seeAndMoveToEnemy({
- required dynamic closeEnemy(),
- BoolCallback? notObserved,
- VoidCallback? observed,
- VoidCallback? notCanMove,
- double radiusVision = 32,
- double? visionAngle,
- double? angle,
- double margin = 10,
- bool runOnlyVisibleInScreen = true,
- MovementAxis movementAxis = MovementAxis.all,
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 seeAndMoveToEnemy({
required Function(Enemy) closeEnemy,
// return true to stop move.
BoolCallback? notObserved,
VoidCallback? observed,
VoidCallback? notCanMove,
double radiusVision = 32,
double? visionAngle,
double? angle,
double margin = 10,
bool runOnlyVisibleInScreen = true,
MovementAxis movementAxis = MovementAxis.all,
}) {
if (runOnlyVisibleInScreen && !isVisible) return;
seeComponentType<Enemy>(
radiusVision: radiusVision,
visionAngle: visionAngle,
angle: angle ?? lastDirection.toRadians(),
observed: (enemy) {
observed?.call();
bool move = moveTowardsTarget(
target: enemy.first,
close: () {
closeEnemy(enemy.first);
},
margin: margin,
movementAxis: movementAxis,
);
if (!move) {
notCanMove?.call();
}
},
notObserved: () {
bool stop = notObserved?.call() ?? true;
if (stop) {
stopMove();
}
},
);
}