From 22e615d7cf51bc9ab8455c01c97d7ac8eb683345 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Thu, 28 Sep 2023 21:29:04 +0100 Subject: [PATCH] Add AI Task for Getting the Player Location or an Area Around Them --- .../AI/BTTask_FindPlayerLocation.cpp | 39 +++++++++++++++++++ .../AI/BTTask_FindPlayerLocation.h | 27 +++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.cpp create mode 100644 EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.h diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.cpp new file mode 100644 index 00000000..7607af23 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.cpp @@ -0,0 +1,39 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "BTTask_FindPlayerLocation.h" + +#include "NavigationSystem.h" +#include "BehaviorTree/BlackboardComponent.h" +#include "GameFramework/Character.h" + +UBTTask_FindPlayerLocation::UBTTask_FindPlayerLocation(FObjectInitializer const& ObjectInitializer) +{ + NodeName = TEXT("Find Player Location"); +} + +EBTNodeResult::Type UBTTask_FindPlayerLocation::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) +{ + if (const ACharacter* const Player = Cast(GetWorld()->GetFirstPlayerController()->GetPawn())) + { + if (UBlackboardComponent* const Blackboard = OwnerComp.GetBlackboardComponent()) + { + if (SearchRandom) + { + FVector const Origin = Player->GetActorLocation(); + const UNavigationSystemV1* const NavSystem = UNavigationSystemV1::GetCurrent(GetWorld()); + if (FNavLocation RandomLocation; NavSystem->GetRandomPointInNavigableRadius(Origin, SearchRadius, RandomLocation)) + { + Blackboard->SetValueAsVector(GetSelectedBlackboardKey(), RandomLocation.Location); + } + } + else + { + Blackboard->SetValueAsVector(GetSelectedBlackboardKey(), Player->GetActorLocation()); + } + FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded); + return EBTNodeResult::Succeeded; + } + } + return EBTNodeResult::Failed; +} diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.h b/EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.h new file mode 100644 index 00000000..6161bd90 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/AI/BTTask_FindPlayerLocation.h @@ -0,0 +1,27 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BehaviorTree/Tasks/BTTask_BlackboardBase.h" +#include "BTTask_FindPlayerLocation.generated.h" + +/** + * + */ +UCLASS() +class ENDLESSVENDETTA_API UBTTask_FindPlayerLocation : public UBTTask_BlackboardBase +{ + GENERATED_BODY() + +public: + explicit UBTTask_FindPlayerLocation(FObjectInitializer const& ObjectInitializer); + virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override; + +private: + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true")) + float SearchRadius = 150.f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true")) + bool SearchRandom = false; +};