// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "BTTaskNodeClimb.h" 
#include "EnemyAIController.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/Blackboard/BlackboardKeyType_Object.h"
#include "BehaviorTree/Blackboard/BlackboardKeyType_Vector.h"


UBTTaskNodeClimb::UBTTaskNodeClimb()
{
	NodeName = "Climb";
	bNotifyTick = true;
	// accept only actors and vectors
	BlackboardKey.AddObjectFilter(this, GET_MEMBER_NAME_CHECKED(UBTTaskNodeClimb, BlackboardKey), AActor::StaticClass());
	BlackboardKey.AddVectorFilter(this, GET_MEMBER_NAME_CHECKED(UBTTaskNodeClimb, BlackboardKey));
	directionKey.AddVectorFilter(this, GET_MEMBER_NAME_CHECKED(UBTTaskNodeClimb, directionKey));
	directionKey.AddObjectFilter(this, GET_MEMBER_NAME_CHECKED(UBTTaskNodeClimb, directionKey), AActor::StaticClass());
}
void UBTTaskNodeClimb::InitializeFromAsset(UBehaviorTree& Asset)
{
	Super::InitializeFromAsset(Asset);

	UBlackboardData &BBAsset = *GetBlackboardAsset();
	BlackboardKey.ResolveSelectedKey(BBAsset);
	directionKey.ResolveSelectedKey(BBAsset);
}

EBTNodeResult::Type UBTTaskNodeClimb::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	EBTNodeResult::Type NodeResult = EBTNodeResult::InProgress;
	NodeResult = PeformClimbTask(OwnerComp, NodeMemory);
	return NodeResult;
}
EBTNodeResult::Type UBTTaskNodeClimb::PeformClimbTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	const UBlackboardComponent* MyBlackboard = OwnerComp.GetBlackboardComponent();
	AAIController* MyController = OwnerComp.GetAIOwner();
	AEnemyAIController* cont = Cast<AEnemyAIController>(OwnerComp.GetAIOwner());
	EBTNodeResult::Type NodeResult = EBTNodeResult::Failed;
	if (cont && MyBlackboard)
	{
		const FVector TargetLocation = MyBlackboard->GetValue<UBlackboardKeyType_Vector>(BlackboardKey.GetSelectedKeyID());
		FVector TargetDirection = FVector::ZeroVector;
		MyBlackboard->GetLocationFromEntry(directionKey.GetSelectedKeyID(), TargetDirection);

		if (cont->VerticleMoveTo(TargetLocation, TargetDirection,AcceptableRadius ))
		{
			NodeResult = EBTNodeResult::Succeeded;
		}
		else NodeResult = EBTNodeResult::InProgress;
	}
	return NodeResult;
}
void UBTTaskNodeClimb::TickTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	const EBTNodeResult::Type NodeResult = PeformClimbTask(OwnerComp, NodeMemory);
	if (NodeResult != EBTNodeResult::InProgress)
	{
		FinishLatentTask(OwnerComp, NodeResult);
	}
}