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


#include "CheckpointClass.h"

// Sets default values
ACheckpointClass::ACheckpointClass()
{
 	// 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;
	BountyTitle = "";

}

// Called when the game starts or when spawned
void ACheckpointClass::BeginPlay()
{
	Super::BeginPlay();
}

void ACheckpointClass::SpawnWaypoint(const FString& CurrentBountyTitle)
{
	if (!IsValid(WaypointActorClass)) return;

	BountyTitle = CurrentBountyTitle;
	FActorSpawnParameters SpawnParams;
	SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	WaypointActor = Cast<AWaypointActor>(GetWorld()->SpawnActor<AActor>(WaypointActorClass, WaypointLoc, GetActorRotation(), SpawnParams));
	WaypointActor->SetupWaypoint(WaypointIcon, BountyTitle);
}

void ACheckpointClass::UpdateChecpointWaypoint(FVector WaypointNewLoc)
{
	FActorSpawnParameters SpawnParams;
 	SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	if (WaypointActor) WaypointActor->Destroy();
	WaypointActor = Cast<AWaypointActor>(GetWorld()->SpawnActor<AActor>(WaypointActorClass, WaypointNewLoc, GetActorRotation(), SpawnParams));
	WaypointActor->SetupWaypoint(WaypointIcon, BountyTitle);
}

// Called every frame
void ACheckpointClass::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ACheckpointClass::Destroyed()
{
	if (WaypointActor) WaypointActor->Destroy();
	Super::Destroyed();
}


