-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHunterAIController.cpp
92 lines (75 loc) · 2.62 KB
/
HunterAIController.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Fill out your copyright notice in the Description page of Project Settings.
#include "RunForLife.h"
#include "HunterAIController.h"
#include "HunterCharacter.h"
#include "PatrolTargetPoint.h"
// AI module
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
AHunterAIController::AHunterAIController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
// Set blackboard key names
IsSeeingPreyKey = "IsSeeingPrey";
PreyKey = "Prey";
PreyLocationKey = "PreyLocation";
LastSeenTime = 0.0f;
SeenExpirationTime = 2.0f;
}
void AHunterAIController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (GetWorld()->TimeSeconds - LastSeenTime >= SeenExpirationTime)
{
BlackboardComp->SetValueAsBool(IsSeeingPreyKey, false);
}
}
void AHunterAIController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
AHunterCharacter* HunterCharacter = Cast<AHunterCharacter>(InPawn);
if (HunterCharacter && HunterCharacter->BehaviorTree->BlackboardAsset)
{
// Initialize blackboard
BlackboardComp->InitializeBlackboard(*HunterCharacter->BehaviorTree->BlackboardAsset);
// Start behavior tree
BehaviorComp->StartTree(*HunterCharacter->BehaviorTree);
}
// Store all patrol target points
UGameplayStatics::GetAllActorsOfClass(this, APatrolTargetPoint::StaticClass(), PatrolTargetPoints);
}
void AHunterAIController::SetPreyLocation(APawn* Prey)
{
if (BlackboardComp && Prey)
{
// Unset noise information
BlackboardComp->SetValueAsBool(IsHearingNoiseKey, false);
const FVector NewPreyLocation = Prey->GetActorLocation();
// No prey record or the new prey is closer
if (!BlackboardComp->GetValueAsBool(IsSeeingPreyKey) || IsNewLocationCloser(NewPreyLocation, PreyLocationKey))
{
LastSeenTime = GetWorld()->TimeSeconds;
// Set prey information
BlackboardComp->SetValueAsBool(IsSeeingPreyKey, true);
BlackboardComp->SetValueAsObject(PreyKey, Prey);
BlackboardComp->SetValueAsVector(PreyLocationKey, NewPreyLocation);
}
}
}
void AHunterAIController::SetNoiseLocation(const FVector& Location)
{
if (BlackboardComp)
{
// Not seeing a prey
if (!BlackboardComp->GetValueAsBool(IsSeeingPreyKey))
{
// No noise record or the new noise is closer
if (!BlackboardComp->GetValueAsBool(IsHearingNoiseKey) || IsNewLocationCloser(Location, NoiseLocationKey))
{
// Set noise information
BlackboardComp->SetValueAsBool(IsHearingNoiseKey, true);
BlackboardComp->SetValueAsVector(NoiseLocationKey, Location);
}
}
}
}