simpleAttackMeleeByDirection method

void simpleAttackMeleeByDirection({
  1. Future<SpriteAnimation>? animationRight,
  2. Future<SpriteAnimation>? animationDown,
  3. Future<SpriteAnimation>? animationLeft,
  4. Future<SpriteAnimation>? animationUp,
  5. dynamic id,
  6. required double damage,
  7. required Direction direction,
  8. required Vector2 size,
  9. required AttackFromEnum attackFrom,
  10. bool withPush = true,
  11. double? sizePush,
})

Execute simple attack melee using animation

Implementation

void simpleAttackMeleeByDirection({
  Future<SpriteAnimation>? animationRight,
  Future<SpriteAnimation>? animationDown,
  Future<SpriteAnimation>? animationLeft,
  Future<SpriteAnimation>? animationUp,
  dynamic id,
  required double damage,
  required Direction direction,
  required Vector2 size,
  required AttackFromEnum attackFrom,
  bool withPush = true,
  double? sizePush,
}) {
  Vector2 positionAttack;
  Future<SpriteAnimation>? anim;
  double pushLeft = 0;
  double pushTop = 0;
  Direction attackDirection = direction;

  Rect rectBase = rectConsideringCollision;

  switch (attackDirection) {
    case Direction.up:
      positionAttack = Vector2(
        rectBase.center.dx - size.x / 2,
        rectBase.top - size.y,
      );
      if (animationUp != null) anim = animationUp;
      pushTop = (sizePush ?? height) * -1;
      break;
    case Direction.right:
      positionAttack = Vector2(
        rectBase.right,
        rectBase.center.dy - size.y / 2,
      );
      if (animationRight != null) anim = animationRight;
      pushLeft = (sizePush ?? width);
      break;
    case Direction.down:
      positionAttack = Vector2(
        rectBase.center.dx - size.x / 2,
        rectBase.bottom,
      );
      if (animationDown != null) anim = animationDown;
      pushTop = (sizePush ?? height);
      break;
    case Direction.left:
      positionAttack = Vector2(
        rectBase.left - size.x,
        rectBase.center.dy - size.y / 2,
      );
      if (animationLeft != null) anim = animationLeft;
      pushLeft = (sizePush ?? width) * -1;
      break;
    case Direction.upLeft:
      positionAttack = Vector2(
        rectBase.left - size.x,
        rectBase.center.dy - size.y / 2,
      );
      if (animationLeft != null) anim = animationLeft;
      pushLeft = (sizePush ?? width) * -1;
      break;
    case Direction.upRight:
      positionAttack = Vector2(
        rectBase.right,
        rectBase.center.dy - size.y / 2,
      );
      if (animationRight != null) anim = animationRight;
      pushLeft = (sizePush ?? width);
      break;
    case Direction.downLeft:
      positionAttack = Vector2(
        rectBase.left - size.x,
        rectBase.center.dy - size.y / 2,
      );
      if (animationLeft != null) anim = animationLeft;
      pushLeft = (sizePush ?? width) * -1;
      break;
    case Direction.downRight:
      positionAttack = Vector2(
        rectBase.right,
        rectBase.center.dy - size.y / 2,
      );
      if (animationRight != null) anim = animationRight;
      pushLeft = (sizePush ?? width);
      break;
  }

  if (anim != null) {
    gameRef.add(
      AnimatedObjectOnce(
        animation: anim,
        position: positionAttack,
        size: size,
      ),
    );
  }

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