simpleAttackMelee method

void simpleAttackMelee({
  1. required Future<SpriteAnimation> attackEffectTopAnim,
  2. required double damage,
  3. required Vector2 size,
  4. int? id,
  5. bool withPush = false,
  6. double? radAngleDirection,
  7. VoidCallback? execute,
  8. int interval = 1000,
})

Execute simple attack melee using animation

Implementation

void simpleAttackMelee({
  required Future<SpriteAnimation> attackEffectTopAnim,
  required double damage,
  required Vector2 size,
  int? id,
  bool withPush = false,
  double? radAngleDirection,
  VoidCallback? execute,
  int interval = 1000,
}) {
  if (!this.checkInterval('attackMelee', interval, dtUpdate)) return;

  if (isDead) return;

  double angle = radAngleDirection ?? this.angle;

  double nextX = this.height * cos(angle);
  double nextY = this.height * sin(angle);
  Offset nextPoint = Offset(nextX, nextY);

  Vector2 diffBase =
      Vector2(this.center.x + nextPoint.dx, this.position.y + nextPoint.dy) -
          this.center;

  Rect positionAttack = this.toRect().shift(diffBase.toOffset());

  gameRef.add(
    AnimatedObjectOnce(
      animation: attackEffectTopAnim,
      position: positionAttack.positionVector2,
      size: size,
      rotateRadAngle: angle,
    ),
  );

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

  if (execute != null) execute();
}