diff --git a/Source/Nakatomi/Decorators/BTDIsWithinRange.cpp b/Source/Nakatomi/Decorators/BTDIsWithinRange.cpp new file mode 100644 index 0000000..53328ce --- /dev/null +++ b/Source/Nakatomi/Decorators/BTDIsWithinRange.cpp @@ -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(Target)->GetActorLocation(); + UNavigationSystemV1* NavigationSystem = FNavigationSystem::GetCurrent(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; +} diff --git a/Source/Nakatomi/Decorators/BTDIsWithinRange.h b/Source/Nakatomi/Decorators/BTDIsWithinRange.h new file mode 100644 index 0000000..dce4bc0 --- /dev/null +++ b/Source/Nakatomi/Decorators/BTDIsWithinRange.h @@ -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; + +};