simpleAttackMeleeByAngle method

void simpleAttackMeleeByAngle({
  1. dynamic id,
  2. Future<SpriteAnimation>? animation,
  3. required double damage,
  4. required double angle,
  5. required AttackFromEnum attackFrom,
  6. required Vector2 size,
  7. bool withPush = true,
  8. double marginFromOrigin = 16,
  9. Vector2? centerOffset,
})

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 AttackFromEnum attackFrom,
  required Vector2 size,
  bool withPush = true,
  double marginFromOrigin = 16,
  Vector2? centerOffset,
}) {
  var initPosition = rectConsideringCollision;

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

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

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

  startPosition.add(diffBase);
  startPosition.add(Vector2(-size.x / 2, -size.y / 2));

  if (animation != null) {
    gameRef.add(
      AnimatedObjectOnce(
        animation: animation,
        position: startPosition,
        size: size,
        rotateRadAngle: angle,
      ),
    );
  }

  Rect positionAttack = Rect.fromLTWH(
    startPosition.x,
    startPosition.y,
    size.x,
    size.y,
  );

  gameRef
      .visibleAttackables()
      .where((a) => a.rectAttackable().overlaps(positionAttack) && a != this)
      .forEach((enemy) {
    enemy.receiveDamage(attackFrom, damage, id);
    final rectAfterPush = enemy.position.translate(diffBase.x, diffBase.y);
    if (withPush &&
        (enemy is ObjectCollision &&
            !(enemy as ObjectCollision)
                .isCollision(displacement: rectAfterPush)
                .isNotEmpty)) {
      enemy.translate(diffBase.x, diffBase.y);
    }
  });
}