// Fill out your copyright notice in the Description page of Project Settings.


#include "InteractNPC.h"
#include "DialogueNPC.h"


// Sets default values for this component's properties
UInteractNPC::UInteractNPC()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UInteractNPC::BeginPlay()
{
	Super::BeginPlay();

	// ...
	
}


// Called every frame
void UInteractNPC::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UInteractNPC::Interact()
{
	FVector Start = GetOwner()->GetActorLocation();
	FVector End = GetOwner()->GetActorForwardVector() * 100.0f + Start;
	FCollisionQueryParams CollisionParams;
	CollisionParams.AddIgnoredActor(GetOwner());
	if (FHitResult HitResult; GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Pawn, CollisionParams))
	{
		//UE_LOG(LogTemp, Warning, TEXT("Hit: %s"), *HitResult.GetActor()->GetName());
		if (HitResult.GetActor()->Tags.Contains("NPC"))
		{
			//DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 5.0f, 0, 10.0f);
			HitResult.GetActor()->FindComponentByClass<UDialogueNPC>()->StartDialogue();
		}
		//DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 5.0f, 0, 10.0f);
	}
}