From e001931bb691156f39d5f14dd1d6087bad66b7a8 Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 24 Oct 2023 22:56:47 +0100 Subject: [PATCH] Create TaskCreateLineOfSight --- Source/Nakatomi/TaskCheckLineOfSight.cpp | 62 ++++++++++++++++++++++++ Source/Nakatomi/TaskCheckLineOfSight.h | 24 +++++++++ 2 files changed, 86 insertions(+) create mode 100644 Source/Nakatomi/TaskCheckLineOfSight.cpp create mode 100644 Source/Nakatomi/TaskCheckLineOfSight.h diff --git a/Source/Nakatomi/TaskCheckLineOfSight.cpp b/Source/Nakatomi/TaskCheckLineOfSight.cpp new file mode 100644 index 0000000..a0e6942 --- /dev/null +++ b/Source/Nakatomi/TaskCheckLineOfSight.cpp @@ -0,0 +1,62 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "TaskCheckLineOfSight.h" +#include +#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( + 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 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(Result.GetActor())) + { + return EBTNodeResult::Succeeded; + } + } + + return EBTNodeResult::Failed; +} diff --git a/Source/Nakatomi/TaskCheckLineOfSight.h b/Source/Nakatomi/TaskCheckLineOfSight.h new file mode 100644 index 0000000..e9680e2 --- /dev/null +++ b/Source/Nakatomi/TaskCheckLineOfSight.h @@ -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; +};