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 double height,
  9. required double width,
  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 double height,
  required double width,
  bool withPush = true,
  double? sizePush,
}) {
  Rect positionAttack;
  Future<SpriteAnimation>? anim;
  double pushLeft = 0;
  double pushTop = 0;
  Direction attackDirection = direction;

  Vector2Rect rectBase = (this.isObjectCollision())
      ? (this as ObjectCollision).rectCollision
      : position;

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

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

  gameRef.visibleAttackables().where((a) {
    return (this is Player
            ? a.receivesAttackFromPlayer()
            : a.receivesAttackFromEnemy()) &&
        a.rectAttackable().rect.overlaps(positionAttack);
  }).forEach(
    (enemy) {
      enemy.receiveDamage(damage, id);
      final rectAfterPush =
          enemy.position.position.translate(pushLeft, pushTop);
      if (withPush &&
          (enemy is ObjectCollision &&
              !(enemy as ObjectCollision)
                  .isCollision(displacement: rectAfterPush)
                  .isNotEmpty)) {
        enemy.translate(pushLeft, pushTop);
      }
    },
  );
}