simpleAttackRangeByDirection method

void simpleAttackRangeByDirection({
  1. required Future<SpriteAnimation> animationRight,
  2. required Future<SpriteAnimation> animationLeft,
  3. required Future<SpriteAnimation> animationUp,
  4. required Future<SpriteAnimation> animationDown,
  5. required double width,
  6. required double height,
  7. required Direction direction,
  8. dynamic id,
  9. double speed = 150,
  10. double damage = 1,
  11. bool withCollision = true,
  12. bool enableDiagonal = true,
  13. VoidCallback? destroy,
  14. CollisionConfig? collision,
  15. LightingConfig? lightingConfig,
  16. Future<SpriteAnimation>? animationDestroy,
})

Execute the ranged attack using a component with animation

Implementation

void simpleAttackRangeByDirection({
  required Future<SpriteAnimation> animationRight,
  required Future<SpriteAnimation> animationLeft,
  required Future<SpriteAnimation> animationUp,
  required Future<SpriteAnimation> animationDown,
  required double width,
  required double height,
  required Direction direction,
  dynamic id,
  double speed = 150,
  double damage = 1,
  bool withCollision = true,
  bool enableDiagonal = true,
  VoidCallback? destroy,
  CollisionConfig? collision,
  LightingConfig? lightingConfig,
  Future<SpriteAnimation>? animationDestroy,
}) {
  Vector2 startPosition;
  Future<SpriteAnimation> attackRangeAnimation;

  Direction attackDirection = direction;

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

  switch (attackDirection) {
    case Direction.left:
      attackRangeAnimation = animationLeft;
      startPosition = Vector2(
        rectBase.rect.left - width,
        (rectBase.rect.top + (rectBase.rect.height - height) / 2),
      );
      break;
    case Direction.right:
      attackRangeAnimation = animationRight;
      startPosition = Vector2(
        rectBase.rect.right,
        (rectBase.rect.top + (rectBase.rect.height - height) / 2),
      );
      break;
    case Direction.up:
      attackRangeAnimation = animationUp;
      startPosition = Vector2(
        (rectBase.rect.left + (rectBase.rect.width - width) / 2),
        rectBase.rect.top - height,
      );
      break;
    case Direction.down:
      attackRangeAnimation = animationDown;
      startPosition = Vector2(
        (rectBase.rect.left + (rectBase.rect.width - width) / 2),
        rectBase.rect.bottom,
      );
      break;
    case Direction.upLeft:
      attackRangeAnimation = animationLeft;
      startPosition = Vector2(
        rectBase.rect.left - width,
        (rectBase.rect.top + (rectBase.rect.height - height) / 2),
      );
      break;
    case Direction.upRight:
      attackRangeAnimation = animationRight;
      startPosition = Vector2(
        rectBase.rect.right,
        (rectBase.rect.top + (rectBase.rect.height - height) / 2),
      );
      break;
    case Direction.downLeft:
      attackRangeAnimation = animationLeft;
      startPosition = Vector2(
        rectBase.rect.left - width,
        (rectBase.rect.top + (rectBase.rect.height - height) / 2),
      );
      break;
    case Direction.downRight:
      attackRangeAnimation = animationRight;
      startPosition = Vector2(
        rectBase.rect.right,
        (rectBase.rect.top + (rectBase.rect.height - height) / 2),
      );
      break;
  }

  gameRef.add(
    FlyingAttackObject(
      id: id,
      direction: attackDirection,
      flyAnimation: attackRangeAnimation,
      destroyAnimation: animationDestroy,
      position: startPosition,
      height: height,
      width: width,
      damage: damage,
      speed: speed,
      enableDiagonal: enableDiagonal,
      attackFrom:
          this is Player ? AttackFromEnum.PLAYER : AttackFromEnum.ENEMY,
      onDestroyedObject: destroy,
      withDecorationCollision: withCollision,
      collision: collision,
      lightingConfig: lightingConfig,
    ),
  );
}