-
Notifications
You must be signed in to change notification settings - Fork 0
/
SBTService_CheckAttackRange.cpp
47 lines (34 loc) · 1.23 KB
/
SBTService_CheckAttackRange.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/SBTService_CheckAttackRange.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "AIController.h"
USBTService_CheckAttackRange::USBTService_CheckAttackRange()
{
MaxAttackRange = 2000.f;
}
void USBTService_CheckAttackRange::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
// Check distance between ai pawn and target actor
UBlackboardComponent* BlackBoardComp = OwnerComp.GetBlackboardComponent();
if (ensure(BlackBoardComp))
{
AActor* TargetActor = Cast<AActor>(BlackBoardComp->GetValueAsObject("TargetActor"));
if (TargetActor)
{
AAIController* MyController = OwnerComp.GetAIOwner();
APawn* AIPawn = MyController->GetPawn();
if (ensure(AIPawn))
{
float DistanceTo = FVector::Distance(TargetActor->GetActorLocation(), AIPawn->GetActorLocation());
bool bWithinRange = DistanceTo < MaxAttackRange;
bool bHasLOS = false;
if (bWithinRange)
{
bHasLOS = MyController->LineOfSightTo(TargetActor);
}
BlackBoardComp->SetValueAsBool(AttackRangeKey.SelectedKeyName, (bWithinRange && bHasLOS));
}
}
}
}