-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTTargetPointSelection.cpp
95 lines (79 loc) · 2.98 KB
/
BTTargetPointSelection.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
93
94
95
// Fill out your copyright notice in the Description page of Project Settings.
#include "Project.h"
#include "BotTargetPoint.h"
#include "MyAIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BTTargetPointSelection.h"
EBTNodeResult::Type UBTTargetPointSelection::ExecuteTask(UBehaviorTreeComponent & OwnerComp, uint8 * NodeMemory)
{
AMyAIController* AICon = Cast<AMyAIController>(OwnerComp.GetAIOwner());
/*If the Controller is valid:
1)Get the Blackboard Component and the Current Point of the bot
2)Search for the next point, which will be different from the Current Point*/
if (AICon)
{
UBlackboardComponent* BlackboardComp = AICon->GetBlackboardComp();
ABotTargetPoint* CurrentPoint = Cast<ABotTargetPoint>(BlackboardComp->GetValueAsObject("LocationToGo"));
TArray<AActor*> AvailableTargetPoints = AICon->GetAvailableTargetPoints();
max = AvailableTargetPoints.Num()-1;
//These variables will contain indices in order to determine the next possible point
int32 currentIndex,nextIndex;
//Here, we store the possible next target point
ABotTargetPoint* NextTargetPoint = nullptr;
//Find a next point which is different from the current one
do
{
if (CurrentPoint)
{
currentIndex = CurrentPoint->position;
if (currentIndex == max) {
nextIndex = currentIndex--;
down = true;
}
else if (currentIndex == 0) {
down = false;
nextIndex = currentIndex++;
}
else{
if (down)
{
nextIndex = currentIndex--;
}
else {
nextIndex = currentIndex++;
}
}
}
else {
nextIndex = 0;
}
NextTargetPoint = Cast<ABotTargetPoint>(AvailableTargetPoints[nextIndex]);
} while (CurrentPoint == NextTargetPoint);
//Update the next location in the Blackboard so the bot can move to the next Blackboard value
BlackboardComp->SetValueAsObject("LocationToGo", AvailableTargetPoints[nextIndex]);
//At this point, the task has been successfully completed
return EBTNodeResult::Succeeded;
}
return EBTNodeResult::Failed;
}
UBTTargetPointSelection::UBTTargetPointSelection()
{
down = false;
}
/*
//This variable will contain a random index in order to determine the next possible point
int32 RandomIndex;
//Here, we store the possible next target point
ABotTargetPoint* NextTargetPoint = nullptr;
//Find a next point which is different from the current one
do
{
RandomIndex = FMath::RandRange(0, AvailableTargetPoints.Num() - 1);
//Remember that the Array provided by the Controller function contains AActor* objects so we need to cast.
NextTargetPoint = Cast<ABotTargetPoint>(AvailableTargetPoints[RandomIndex]);
} while (CurrentPoint == NextTargetPoint);
//Update the next location in the Blackboard so the bot can move to the next Blackboard value
BlackboardComp->SetValueAsObject("LocationToGo", NextTargetPoint);
//At this point, the task has been successfully completed
return EBTNodeResult::Succeeded;
*/