From 83d5ba31f4b6dafbd9a5af223fc0e25c027ae40f Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Thu, 16 Feb 2023 18:28:25 +0000 Subject: [PATCH] Add Get Random Location task --- Source/Nakatomi/TaskGetRandomLocation.cpp | 24 ++++++++++++++++++++ Source/Nakatomi/TaskGetRandomLocation.h | 27 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Source/Nakatomi/TaskGetRandomLocation.cpp create mode 100644 Source/Nakatomi/TaskGetRandomLocation.h diff --git a/Source/Nakatomi/TaskGetRandomLocation.cpp b/Source/Nakatomi/TaskGetRandomLocation.cpp new file mode 100644 index 0000000..157f8c6 --- /dev/null +++ b/Source/Nakatomi/TaskGetRandomLocation.cpp @@ -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(owner.GetAIOwner()); + auto enemyPawn = Cast(enemyController->GetPawn()); + auto blackboardComponent = owner.GetBlackboardComponent(); + auto navigationSystem = FNavigationSystem::GetCurrent(GetWorld()); + + if (blackboardComponent && navigationSystem) + { + FNavLocation navLocation; + navigationSystem->GetRandomReachablePointInRadius(enemyPawn->GetActorLocation(), MaximumDistance, navLocation); + + blackboardComponent->SetValueAsVector(PatrolLocationKey.SelectedKeyName, navLocation.Location); + return EBTNodeResult::Succeeded; + } + + return EBTNodeResult::Failed; +} diff --git a/Source/Nakatomi/TaskGetRandomLocation.h b/Source/Nakatomi/TaskGetRandomLocation.h new file mode 100644 index 0000000..53ff145 --- /dev/null +++ b/Source/Nakatomi/TaskGetRandomLocation.h @@ -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; +};