// Multiplayer By Caleb


#include "Animations/AN_SendTargetGroup.h"

#include "AbilitySystemBlueprintLibrary.h"

void UAN_SendTargetGroup::Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation,
                                 const FAnimNotifyEventReference& EventReference)
{
	Super::Notify(MeshComp, Animation, EventReference);

	if (!MeshComp || !MeshComp->GetOwner()) return;
	if (!TargetSocketNames.Num()) return;
	if (!UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(MeshComp->GetOwner())) return;

	FGameplayEventData Data;
	for (int i = 1; i < TargetSocketNames.Num(); ++i)
	{
		// const auto LocationInfo{NewObject<FGameplayAbilityTargetData_LocationInfo>()};
		const auto LocationInfo{new FGameplayAbilityTargetData_LocationInfo()}; // heap allocation
		FVector StartLoc{MeshComp->GetSocketLocation(TargetSocketNames[i - 1])};
		FVector EndLoc{MeshComp->GetSocketLocation(TargetSocketNames[i])};
		LocationInfo->SourceLocation.LiteralTransform.SetLocation(StartLoc);
		LocationInfo->TargetLocation.LiteralTransform.SetLocation(EndLoc);

		Data.TargetData.Add(LocationInfo);
	}
	UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(MeshComp->GetOwner(), EventTag, Data);
}
