simpleAttackMeleeByAngle method

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

Execute simple attack melee using animation

Implementation

void simpleAttackMeleeByAngle({
  dynamic id,

  /// use animation facing right.
  Future<SpriteAnimation>? animation,
  required double damage,

  /// Use radians angle
  required double angle,
  required AttackOriginEnum attackFrom,
  required Vector2 size,
  bool withPush = true,
  double marginFromCenter = 0,
  Vector2? centerOffset,
  void Function(Attackable attackable)? onDamage,
}) {
  var initPosition = rectCollision;

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

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

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

  startPosition.add(diffBase);

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

  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,
          );
        }
      },
    ),
  );
}