-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNPCAIController.cpp
33 lines (26 loc) · 1.16 KB
/
NPCAIController.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "RunForLife.h"
#include "NPCAIController.h"
// AI module
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
ANPCAIController::ANPCAIController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
// Create behavior tree and blackboard components
BehaviorComp = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorComp"));
BlackboardComp = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComp"));
// Set blackboard key names
IsHearingNoiseKey = "IsHearingNoise";
NoiseLocationKey = "NoiseLocation";
PatrolLocationKey = "PatrolLocation";
}
bool ANPCAIController::IsNewLocationCloser(const FVector& NewKeyLocation, const FName& Key)
{
if (BlackboardComp && GetCharacter())
{
const FVector CurrentKeyLocation = BlackboardComp->GetValueAsVector(Key);
const FVector ActorLocation = GetCharacter()->GetActorLocation();
return (FVector::DistSquaredXY(NewKeyLocation, ActorLocation) < FVector::DistSquaredXY(CurrentKeyLocation, ActorLocation));
}
return false;
}