Create Distance To Player Task

I should have given this a better name, maybe in the future
This commit is contained in:
Louis Hobbs 2023-06-12 22:11:02 +01:00
parent 11c88c6dd9
commit 50cba81295
2 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,39 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "GetDistanceToPlayerTask.h"
#include "EnemyAIController.h"
#include "DemolitionCharacter.h"
UGetDistanceToPlayerTask::UGetDistanceToPlayerTask()
{
}
EBTNodeResult::Type UGetDistanceToPlayerTask::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory)
{
auto enemyController = Cast<AEnemyAIController>(owner.GetAIOwner());
auto enemyPawn = Cast<ADemolitionCharacter>(enemyController->GetPawn());
if (!enemyPawn || enemyPawn->GetHealthComponent()->GetIsDead())
{
return EBTNodeResult::Failed;
}
auto blackboardComponent = owner.GetBlackboardComponent();
auto playerCharacter = Cast<ANakatomiCharacter>(blackboardComponent->GetValueAsObject(TargetActor.SelectedKeyName));
auto distance = FVector::Distance(enemyPawn->GetActorLocation(), playerCharacter->GetActorLocation());
if (distance < DistanceThreshold)
{
enemyPawn->Explode();
enemyPawn->GetHealthComponent()->TakeDamage(enemyPawn, enemyPawn->GetHealthComponent()->GetMaxHealth(), nullptr, nullptr, nullptr);
return EBTNodeResult::Succeeded;
}
return EBTNodeResult::Failed;
}
EBTNodeResult::Type UGetDistanceToPlayerTask::AbortTask(UBehaviorTreeComponent& owner, uint8* memory)
{
return EBTNodeResult::Aborted;
}

View File

@ -0,0 +1,36 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "GetDistanceToPlayerTask.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UGetDistanceToPlayerTask : public UBTTaskNode
{
GENERATED_BODY()
private:
UPROPERTY()
UBehaviorTreeComponent* behaviourTreeOwner = nullptr;
UPROPERTY(EditAnywhere, Meta = (AllowPrivateAccess = "true"))
FBlackboardKeySelector TargetActor;
UPROPERTY(EditAnywhere, Meta = (AllowPrivateAccess = "true"))
float DistanceThreshold = 5.0f;
public:
UGetDistanceToPlayerTask();
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override;
virtual EBTNodeResult::Type AbortTask(UBehaviorTreeComponent& owner, uint8* memory) override;
};