// Project Lab - NHTV Igad

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

UBTTaskNodeJumpFence::UBTTaskNodeJumpFence()
{
	NodeName = "Jump Fence";
	bNotifyTick = true;
	BlackboardKey.AddObjectFilter(this, GET_MEMBER_NAME_CHECKED(UBTTaskNodeJumpFence, BlackboardKey), AActor::StaticClass());
	BlackboardKey.AddVectorFilter(this, GET_MEMBER_NAME_CHECKED(UBTTaskNodeJumpFence, BlackboardKey));
}
void UBTTaskNodeJumpFence::InitializeFromAsset(UBehaviorTree& asset)
{
	Super::InitializeFromAsset(asset);
	UBlackboardData& BBAsset = *GetBlackboardAsset();
	BlackboardKey.ResolveSelectedKey(BBAsset);
}
EBTNodeResult::Type UBTTaskNodeJumpFence::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	EBTNodeResult::Type NodeResult = EBTNodeResult::InProgress;
	NodeResult = PefromJumpFenceTask(OwnerComp, NodeMemory);
	return NodeResult;
}
EBTNodeResult::Type UBTTaskNodeJumpFence::PefromJumpFenceTask(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;
		if (cont->JumpFence(TargetLocation))
		{
			NodeResult = EBTNodeResult::Succeeded;
		}
		else NodeResult = EBTNodeResult::InProgress;
	}
	return NodeResult;

}
void UBTTaskNodeJumpFence::TickTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	const EBTNodeResult::Type NodeResult = PefromJumpFenceTask(OwnerComp, NodeMemory);
	if (NodeResult != EBTNodeResult::InProgress)
	{
		FinishLatentTask(OwnerComp, NodeResult);
	}
}