meleeByAngle method

void meleeByAngle({
  1. required double damage,
  2. required double angle,
  3. required AttackOriginEnum attackFrom,
  4. required Vector2 size,
  5. dynamic id,
  6. Future<SpriteAnimation>? animation,
  7. bool withPush = true,
  8. double marginFromCenter = 0,
  9. Vector2? centerOffset,
  10. void onDamage(
    1. WithLife attackable
    )?,
})

Executes a melee attack by angle (radians).

Implementation

void meleeByAngle({
  required double damage,
  required double angle,
  required AttackOriginEnum attackFrom,
  required Vector2 size,
  dynamic id,
  Future<SpriteAnimation>? animation,
  bool withPush = true,
  double marginFromCenter = 0,
  Vector2? centerOffset,
  void Function(WithLife attackable)? onDamage,
}) {
  final initPosition = comp.rectCollision;

  final startPosition =
      initPosition.center.toVector2() + (centerOffset ?? Vector2.zero());

  final displacement =
      max(initPosition.width, initPosition.height) / 2 + marginFromCenter;

  final diffBase = BonfireUtil.diffMovePointByAngle(
    startPosition,
    displacement,
    angle,
  );

  startPosition.add(diffBase);

  if (animation != null) {
    comp.gameRef.add(
      AnimatedGameObject(
        animation: animation,
        position: startPosition,
        size: size,
        angle: angle,
        anchor: Anchor.center,
        loop: false,
        renderAboveComponents: true,
      ),
    );
  }

  comp.gameRef.add(
    DamageHitbox(
      position: startPosition,
      damage: damage,
      origin: attackFrom,
      size: size,
      angle: angle,
      id: id,
      onDamage: (attackable) {
        onDamage?.call(attackable);
        if (withPush && attackable is Movement) {
          _doPush(
            attackable as Movement,
            BonfireUtil.getDirectionFromAngle(angle),
            diffBase,
          );
        }
      },
    ),
  );
}