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; +};