Add Minimum Distance Check to GetLocationNearLocation Task

This commit is contained in:
Louis Hobbs 2023-09-05 14:59:52 +01:00
parent 7aa0c0b002
commit 681641672c
4 changed files with 27 additions and 14 deletions

BIN
Content/Enemy/Worker/BB_Worker.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Enemy/Worker/BT_Worker.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -3,23 +3,32 @@
#include "TaskGetLocationNearLocation.h" #include "TaskGetLocationNearLocation.h"
#include "EnemyAIController.h" #include "EnemyAIController.h"
#include "EnemyCharacter.h"
#include <NavigationSystem.h> #include <NavigationSystem.h>
EBTNodeResult::Type UTaskGetLocationNearLocation::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) EBTNodeResult::Type UTaskGetLocationNearLocation::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory)
{ {
auto enemyController = Cast<AEnemyAIController>(owner.GetAIOwner()); auto BlackboardComponent = owner.GetBlackboardComponent();
auto enemyPawn = Cast<AEnemyCharacter>(enemyController->GetPawn());
auto blackboardComponent = owner.GetBlackboardComponent();
auto navigationSystem = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
FVector sourceLocation = blackboardComponent->GetValueAsVector(SourceLocationKey.SelectedKeyName);
if (blackboardComponent && navigationSystem && sourceLocation != FVector::ZeroVector) if (!BlackboardComponent)
{ {
FNavLocation navLocation; return EBTNodeResult::Failed;
navigationSystem->GetRandomReachablePointInRadius(sourceLocation, MaximumDistance, navLocation); }
blackboardComponent->SetValueAsVector(TargetLocationKey.SelectedKeyName, navLocation.Location); auto NavigationSystem = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
FVector SourceLocation = BlackboardComponent->GetValueAsVector(SourceLocationKey.SelectedKeyName);
if (BlackboardComponent && NavigationSystem && SourceLocation != FVector::ZeroVector)
{
double Distance = -1.0;
FNavLocation NavLocation;
while (Distance < MinimumDistance)
{
NavigationSystem->GetRandomReachablePointInRadius(SourceLocation, MaximumDistance, NavLocation);
NavigationSystem->GetPathLength(SourceLocation, NavLocation.Location, Distance);
}
BlackboardComponent->SetValueAsVector(TargetLocationKey.SelectedKeyName, NavLocation.Location);
return EBTNodeResult::Succeeded; return EBTNodeResult::Succeeded;
} }

View File

@ -28,6 +28,10 @@ public:
Meta = (AllowPrivateAccess = "true", DisplayName = "Maximum Distance")) Meta = (AllowPrivateAccess = "true", DisplayName = "Maximum Distance"))
float MaximumDistance = 500.0f; float MaximumDistance = 500.0f;
UPROPERTY(EditAnywhere, Category = "Options",
Meta = (AllowPrivateAccess = "true", DisplayName = "Minimum Distance"))
double MinimumDistance = 0.0f;
public: public:
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override; virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override;
}; };