diff --git a/Source/Nakatomi/TaskIsWithinRange.cpp b/Source/Nakatomi/TaskIsWithinRange.cpp new file mode 100644 index 0000000..c75eba0 --- /dev/null +++ b/Source/Nakatomi/TaskIsWithinRange.cpp @@ -0,0 +1,34 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "TaskIsWithinRange.h" +#include "EnemyAIController.h" +#include +#include + +EBTNodeResult::Type UTaskIsWithinRange::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) +{ + auto BlackboardComponent = owner.GetBlackboardComponent(); + + if (!BlackboardComponent) + { + return EBTNodeResult::Failed; + } + + auto NavigationSystem = FNavigationSystem::GetCurrent(GetWorld()); + FVector SourceLocation = BlackboardComponent->GetValueAsVector(SourceLocationKey.SelectedKeyName); + ANakatomiCharacter* selfActor = Cast( + BlackboardComponent->GetValueAsObject(SelfActorKey.SelectedKeyName)); + + if (NavigationSystem && SourceLocation != FVector::ZeroVector) + { + double Distance = -1.0; + FNavLocation NavLocation; + + NavigationSystem->GetPathLength(SourceLocation, selfActor->GetTransform().GetLocation(), Distance); + + return Distance <= MaximumDistance ? EBTNodeResult::Succeeded : EBTNodeResult::Failed; + } + + return EBTNodeResult::Failed; +} diff --git a/Source/Nakatomi/TaskIsWithinRange.h b/Source/Nakatomi/TaskIsWithinRange.h new file mode 100644 index 0000000..f2b7504 --- /dev/null +++ b/Source/Nakatomi/TaskIsWithinRange.h @@ -0,0 +1,35 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BehaviorTree/BTTaskNode.h" +#include "TaskIsWithinRange.generated.h" + +/** + * + */ +UCLASS() +class NAKATOMI_API UTaskIsWithinRange : public UBTTaskNode +{ + GENERATED_BODY() + +public: + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Source Location Key")) + FBlackboardKeySelector SourceLocationKey; + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Self Actor Key")) + FBlackboardKeySelector SelfActorKey; + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Maximum Distance")) + float MaximumDistance = 500.0f; + +public: + virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override; + + +};