// Multiplayer By Caleb


#include "GAS/CGameplayAbility.h"

#include "Kismet/KismetSystemLibrary.h"

UAnimInstance* UCGameplayAbility::GetOwnerAnimInstance() const
{
	if (const auto OwnerSkeletalMeshComp{GetOwningComponentFromActorInfo()})
	{
		return OwnerSkeletalMeshComp->GetAnimInstance();
	}
	return nullptr;
}

TArray<FHitResult> UCGameplayAbility::GetHitResultFromSweepLocationTargetData(
	const FGameplayAbilityTargetDataHandle& TargetDataHandle, float SphereSweepRadius, bool bDrawDebug,
	bool bIgnoreSelf) const
{
	TArray<FHitResult> OutResults;
	TSet<AActor*> HitActors;
	for (const auto TargetData : TargetDataHandle.Data)
	{
		const FVector StartLoc{TargetData->GetOrigin().GetTranslation()};
		const FVector EndLoc{TargetData->GetEndPoint()};

		TArray<TEnumAsByte<EObjectTypeQuery>> ObjectTypes;
		ObjectTypes.Add(UEngineTypes::ConvertToObjectType(ECC_Pawn));

		TArray<AActor*> ActorsToIgnore;
		if (bIgnoreSelf) ActorsToIgnore.Add(GetAvatarActorFromActorInfo());
		const auto DrawDebugTrace{bDrawDebug ? EDrawDebugTrace::ForDuration : EDrawDebugTrace::None};

		TArray<FHitResult> Results;
		UKismetSystemLibrary::SphereTraceMultiForObjects(
			this,
			StartLoc,
			EndLoc,
			SphereSweepRadius,
			ObjectTypes,
			false,
			ActorsToIgnore,
			DrawDebugTrace,
			Results,
			false
		);

		for (const auto Result : Results)
		{
			if (HitActors.Contains(Result.GetActor())) continue;
			HitActors.Add(Result.GetActor());
			OutResults.Add(Result);
		}
	}

	return OutResults;
}
