D1/Source/D1/AI/BehaviorTreeNodes/T_TryGetRandomPatrolPoint.cpp

45 lines
1.4 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/BehaviorTreeNodes/T_TryGetRandomPatrolPoint.h"
#include "AIController.h"
#include "NavigationData.h"
#include "NavigationSystem.h"
#include "BehaviorTree/BlackboardComponent.h"
UT_TryGetRandomPatrolPoint::UT_TryGetRandomPatrolPoint()
{
Radius = 500.f;
QueryExtent = {300.f, 300.f, 300.f};
}
EBTNodeResult::Type UT_TryGetRandomPatrolPoint::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
EBTNodeResult::Type result = Super::ExecuteTask(OwnerComp, NodeMemory);
if(result == EBTNodeResult::Failed)
return EBTNodeResult::Failed;
APawn* ControllingPawn = OwnerComp.GetAIOwner()->GetPawn();
if(ControllingPawn == nullptr)
return EBTNodeResult::Failed;
UNavigationSystemV1* NavSystem = UNavigationSystemV1::GetNavigationSystem(ControllingPawn->GetWorld());
if(NavSystem == nullptr)
return EBTNodeResult::Failed;
FVector CurLocation = ControllingPawn->GetActorLocation();
FNavLocation RandomLocation, ProjectLocation;
if(NavSystem->GetRandomReachablePointInRadius(CurLocation, Radius, RandomLocation))
{
if(NavSystem->ProjectPointToNavigation(RandomLocation.Location, ProjectLocation, QueryExtent))
OwnerComp.GetBlackboardComponent()->SetValueAsVector(BlackboardKey_TargetLocation.SelectedKeyName, ProjectLocation.Location);
else
return EBTNodeResult::Failed;
}
else
return EBTNodeResult::Failed;
return EBTNodeResult::Succeeded;
}