Add IsWithinRange Decorator

This commit is contained in:
baz 2024-02-19 21:53:33 +00:00
parent 265561dffb
commit eb9178f4e3
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,29 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "../Decorators/BTDIsWithinRange.h"
#include "AIController.h"
#include "navigationSystem.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Nakatomi/NakatomiCharacter.h"
bool UBTDIsWithinRange::CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
{
UBlackboardComponent* BlackboardComponent = OwnerComp.GetBlackboardComponent();
float Dist = BlackboardComponent->GetValueAsFloat(DistanceKey.SelectedKeyName);
UObject* Target = BlackboardComponent->GetValueAsObject(TargetActorKey.SelectedKeyName);
auto TargetLocation = Cast<AActor>(Target)->GetActorLocation();
UNavigationSystemV1* NavigationSystem = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
APawn* pawn = OwnerComp.GetAIOwner()->GetPawn();
if (pawn && NavigationSystem && TargetLocation != FVector::ZeroVector)
{
double Distance = -1.0;
NavigationSystem->GetPathLength(TargetLocation, pawn->GetTransform().GetLocation(), Distance);
return Distance <= Dist ? true : false;
}
return false;
}

View File

@ -0,0 +1,31 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTDecorator.h"
#include "BTDIsWithinRange.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UBTDIsWithinRange : public UBTDecorator
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, Category = "Options",
Meta = (AllowPrivateAccess = "true", DisplayName = "Target Actor Key"))
FBlackboardKeySelector TargetActorKey;
UPROPERTY(EditAnywhere, Category = "Options",
Meta = (AllowPrivateAccess = "true", DisplayName = "Distance Key"))
FBlackboardKeySelector DistanceKey;
public:
virtual bool CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const override;
};