// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "NetworkGhost.h"
#include "Shrine.h"


// Sets default values
AShrine::AShrine()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	displayMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	displayMesh->bHiddenInGame = true;
	displayMesh->bGenerateOverlapEvents = false;
	displayMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	RootComponent = displayMesh;

	shrineTrigger = CreateDefaultSubobject<USphereComponent>(TEXT("RespawnArea"));
	shrineTrigger->SetCollisionProfileName(TEXT("GhostOverlap"));
	shrineTrigger->AttachTo(RootComponent);
	shrineTrigger->OnComponentBeginOverlap.AddDynamic(this, &AShrine::OnOverlapBegin);
	shrineTrigger->OnComponentEndOverlap.AddDynamic(this, &AShrine::OnOverlapEnd);
	shrineTrigger->SetSphereRadius(300);
	shrineTrigger->AttachTo(RootComponent);
}


void AShrine::OnOverlapBegin(AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	ANetworkGhost* ghost = Cast<ANetworkGhost>(OtherActor);
	if (!IsValid(ghost)) return;

	ghost->shrinesInRange++;
}
void AShrine::OnOverlapEnd(AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	ANetworkGhost* ghost = Cast<ANetworkGhost>(OtherActor);
	if (!IsValid(ghost)) return;

	if (ghost->shrinesInRange > 0)
		ghost->shrinesInRange--;
}