Add Get Random Location task

This commit is contained in:
Louis Hobbs 2023-02-16 18:28:25 +00:00
parent 2f8da66aa4
commit 83d5ba31f4
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,24 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "TaskGetRandomLocation.h"
#include "EnemyAIController.h"
#include "navigationSystem.h"
EBTNodeResult::Type UTaskGetRandomLocation::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory)
{
auto enemyController = Cast<AEnemyAIController>(owner.GetAIOwner());
auto enemyPawn = Cast<AEnemyCharacter>(enemyController->GetPawn());
auto blackboardComponent = owner.GetBlackboardComponent();
auto navigationSystem = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
if (blackboardComponent && navigationSystem)
{
FNavLocation navLocation;
navigationSystem->GetRandomReachablePointInRadius(enemyPawn->GetActorLocation(), MaximumDistance, navLocation);
blackboardComponent->SetValueAsVector(PatrolLocationKey.SelectedKeyName, navLocation.Location);
return EBTNodeResult::Succeeded;
}
return EBTNodeResult::Failed;
}

View File

@ -0,0 +1,27 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "TaskGetRandomLocation.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UTaskGetRandomLocation : public UBTTaskNode
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, Category = "Options", Meta = (AllowPrivateAccess = "true", DisplayName = "Patrol Location Key"))
FBlackboardKeySelector PatrolLocationKey;
UPROPERTY(EditAnywhere, Category = "Options", Meta = (AllowPrivateAccess = "true", DisplayName = "Maximum Distance"))
float MaximumDistance = 500.0f;
public:
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override;
};