-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBTTask_GetRandomPatrolPoint.cpp
39 lines (28 loc) · 1.42 KB
/
BTTask_GetRandomPatrolPoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Fill out your copyright notice in the Description page of Project Settings.
#include "RunForLife.h"
#include "BTTask_GetRandomPatrolPoint.h"
#include "NPCAIController.h"
// AI module
#include "BehaviorTree/BlackboardComponent.h"
UBTTask_GetRandomPatrolPoint::UBTTask_GetRandomPatrolPoint(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
NodeName = "Get Random Patrol Point";
SearchRadius = 100.0f;
}
EBTNodeResult::Type UBTTask_GetRandomPatrolPoint::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
ANPCAIController* NPCAIController = Cast<ANPCAIController>(OwnerComp.GetAIOwner());
UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
check(NPCAIController && BlackboardComp);
const TArray<AActor*> PatrolTargetPoints = NPCAIController->GetPatrolTargetPoints();
if (PatrolTargetPoints.Num() > 0)
{
// Find a random location that is close to a random patrol target point
const int32 RandomIndex = FMath::RandRange(0, PatrolTargetPoints.Num() - 1);
const FVector SearchOrigin = PatrolTargetPoints[RandomIndex]->GetActorLocation();
const FVector RandomLocation = UNavigationSystem::GetRandomReachablePointInRadius(NPCAIController, SearchOrigin, SearchRadius);
BlackboardComp->SetValueAsVector(NPCAIController->PatrolLocationKey, RandomLocation);
return EBTNodeResult::Succeeded;
}
return EBTNodeResult::Failed;
}