From b10311e3d11018a38acdb45fe6359933859f5025 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Wed, 23 Aug 2023 15:15:30 +0100 Subject: [PATCH] Add TaskGetLocationNearLocation AI Task --- .../Nakatomi/TaskGetLocationNearLocation.cpp | 27 +++++++++++++++ Source/Nakatomi/TaskGetLocationNearLocation.h | 33 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Source/Nakatomi/TaskGetLocationNearLocation.cpp create mode 100644 Source/Nakatomi/TaskGetLocationNearLocation.h diff --git a/Source/Nakatomi/TaskGetLocationNearLocation.cpp b/Source/Nakatomi/TaskGetLocationNearLocation.cpp new file mode 100644 index 0000000..6febcd0 --- /dev/null +++ b/Source/Nakatomi/TaskGetLocationNearLocation.cpp @@ -0,0 +1,27 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "TaskGetLocationNearLocation.h" +#include "EnemyAIController.h" +#include "EnemyCharacter.h" +#include + +EBTNodeResult::Type UTaskGetLocationNearLocation::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) +{ + auto enemyController = Cast(owner.GetAIOwner()); + auto enemyPawn = Cast(enemyController->GetPawn()); + auto blackboardComponent = owner.GetBlackboardComponent(); + auto navigationSystem = FNavigationSystem::GetCurrent(GetWorld()); + FVector sourceLocation = blackboardComponent->GetValueAsVector(SourceLocationKey.SelectedKeyName); + + if (blackboardComponent && navigationSystem && sourceLocation != FVector::ZeroVector) + { + FNavLocation navLocation; + navigationSystem->GetRandomReachablePointInRadius(sourceLocation, MaximumDistance, navLocation); + + blackboardComponent->SetValueAsVector(TargetLocationKey.SelectedKeyName, navLocation.Location); + return EBTNodeResult::Succeeded; + } + + return EBTNodeResult::Failed; +} diff --git a/Source/Nakatomi/TaskGetLocationNearLocation.h b/Source/Nakatomi/TaskGetLocationNearLocation.h new file mode 100644 index 0000000..a0b8e95 --- /dev/null +++ b/Source/Nakatomi/TaskGetLocationNearLocation.h @@ -0,0 +1,33 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BehaviorTree/BTTaskNode.h" +#include "TaskGetLocationNearLocation.generated.h" + +/** + * + */ +UCLASS() +class NAKATOMI_API UTaskGetLocationNearLocation : public UBTTaskNode +{ + GENERATED_BODY() + + +public: + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Target Location Key")) + FBlackboardKeySelector TargetLocationKey; + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Source Location Key")) + FBlackboardKeySelector SourceLocationKey; + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Maximum Distance")) + float MaximumDistance = 500.0f; + +public: + virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override; +};