seeAndMoveToPlayer method

Shape? seeAndMoveToPlayer({
  1. required dynamic closePlayer(
    1. Player
    ),
  2. VoidCallback? notObserved,
  3. VoidCallback? observed,
  4. VoidCallback? notCanMove,
  5. double radiusVision = 32,
  6. double margin = 10,
  7. double? visionAngle,
  8. double? angle,
  9. bool runOnlyVisibleInScreen = true,
})

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

Shape? seeAndMoveToPlayer({
  required Function(Player) closePlayer,
  VoidCallback? notObserved,
  VoidCallback? observed,
  VoidCallback? notCanMove,
  double radiusVision = 32,
  double margin = 10,
  double? visionAngle,
  double? angle,
  bool runOnlyVisibleInScreen = true,
}) {
  if (runOnlyVisibleInScreen && !isVisible) return null;

  return seePlayer(
    radiusVision: radiusVision,
    visionAngle: visionAngle,
    angle: angle,
    observed: (player) {
      observed?.call();
      bool move = followComponent(
        player,
        dtUpdate,
        closeComponent: (comp) => closePlayer(comp as Player),
        margin: margin,
      );
      if (!move) {
        notCanMove?.call();
      }
    },
    notObserved: () {
      if (!isIdle) {
        idle();
      }
      notObserved?.call();
    },
  );
}