Create TaskCreateLineOfSight

This commit is contained in:
baz 2023-10-24 22:56:47 +01:00
parent 2ab85e21d7
commit e001931bb6
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,62 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "TaskCheckLineOfSight.h"
#include <BehaviorTree/BlackboardComponent.h>
#include "NakatomiCharacter.h"
#include "PlayerCharacter.h"
EBTNodeResult::Type UTaskCheckLineOfSight::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory)
{
auto BlackboardComponent = owner.GetBlackboardComponent();
if (!BlackboardComponent)
{
return EBTNodeResult::Failed;
}
auto selfActor = Cast<ANakatomiCharacter>(
BlackboardComponent->GetValueAsObject(SelfActorKey.SelectedKeyName));
if (!selfActor)
{
return EBTNodeResult::Failed;
}
AWeapon* CurrentWeapon = selfActor->GetCurrentWeapon();
if (!CurrentWeapon)
{
return EBTNodeResult::Failed;
}
// Calculate starting position and direction
FVector TraceStart = selfActor->GetTransform().GetLocation();
FRotator PlayerRot = selfActor->GetTransform().GetRotation().Rotator();
TraceStart = selfActor->GetRootComponent()->GetComponentLocation();
FVector AimDir = PlayerRot.Vector();
AimDir.Z = 0.0;
TraceStart = TraceStart + AimDir * ((selfActor->GetInstigator()->GetActorLocation() - TraceStart) | AimDir);
// Calculate the hit results from the trace
TArray<FHitResult> HitResults;
// Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, selfActor->GetInstigator());
TraceParams.bReturnPhysicalMaterial = true;
FVector MaxHitLoc = TraceStart + (AimDir * CurrentWeapon->GetWeaponProperties()->ProjectileRange);
GetWorld()->LineTraceMultiByChannel(HitResults, TraceStart, MaxHitLoc, ECC_GameTraceChannel1, TraceParams);
for (FHitResult Result : HitResults)
{
if (Cast<APlayerCharacter>(Result.GetActor()))
{
return EBTNodeResult::Succeeded;
}
}
return EBTNodeResult::Failed;
}

View File

@ -0,0 +1,24 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "TaskCheckLineOfSight.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UTaskCheckLineOfSight : public UBTTaskNode
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, Category = "Options",
Meta = (AllowPrivateAccess = "true", DisplayName = "Self Actor Key"))
FBlackboardKeySelector SelfActorKey;
public:
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override;
};