From 6a30ddae07ce781320ba0195a6bc82948d3b2b07 Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 27 Feb 2024 23:18:07 +0000 Subject: [PATCH] Refine Ranged Enemy functionality and hide on low health --- Content/Enemy/BT_Attacking_State.uasset | 4 +-- Content/Enemy/BT_Find_Cover.uasset | 4 +-- Content/Enemy/EQS/AttackTarget.uasset | 2 +- Content/Enemy/EQS/EQS_FindIdealRange.uasset | 3 ++ Content/Enemy/Worker/AIC_Worker.uasset | 4 +-- Content/Enemy/Worker/BB_Worker.uasset | 4 +-- Content/Enemy/Worker/BT_Worker.uasset | 4 +-- Content/Enemy/Worker/C_Worker.uasset | 4 +-- .../Enemy/Worker/PatrolMovementSpeed.uasset | 3 -- .../Nakatomi/Decorators/BTDCanSeeTarget.cpp | 27 +++++++++++++++++ Source/Nakatomi/Decorators/BTDCanSeeTarget.h | 24 +++++++++++++++ .../Decorators/BTDIsHealthBelowThreshold.cpp | 14 +++++++++ .../Decorators/BTDIsHealthBelowThreshold.h | 29 +++++++++++++++++++ Source/Nakatomi/EnemyCharacter.cpp | 18 +++++++++++- Source/Nakatomi/EnemyCharacter.h | 3 ++ 15 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 Content/Enemy/EQS/EQS_FindIdealRange.uasset delete mode 100644 Content/Enemy/Worker/PatrolMovementSpeed.uasset create mode 100644 Source/Nakatomi/Decorators/BTDCanSeeTarget.cpp create mode 100644 Source/Nakatomi/Decorators/BTDCanSeeTarget.h create mode 100644 Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.cpp create mode 100644 Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.h diff --git a/Content/Enemy/BT_Attacking_State.uasset b/Content/Enemy/BT_Attacking_State.uasset index ad8ad93..4d6bcdc 100644 --- a/Content/Enemy/BT_Attacking_State.uasset +++ b/Content/Enemy/BT_Attacking_State.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d2d10560f8fc5d39c97cae7f1db8463469d5723c0966a4ee8a69fe5f986a9f0a -size 25779 +oid sha256:046a90ce16e45a46b81d069cd5947962e106ec8eabdefd48afcf3b1e7c5504b2 +size 29454 diff --git a/Content/Enemy/BT_Find_Cover.uasset b/Content/Enemy/BT_Find_Cover.uasset index f3f852f..dc3d704 100644 --- a/Content/Enemy/BT_Find_Cover.uasset +++ b/Content/Enemy/BT_Find_Cover.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bb8c2676663aaf0507e907e488cdb92ebc960b6ca60376058c9d51d1bf6e2935 -size 11350 +oid sha256:f73ce2850790ae0b631e6065d74cb62fb9f0c22945ff072d9783b4d9fec0d185 +size 11379 diff --git a/Content/Enemy/EQS/AttackTarget.uasset b/Content/Enemy/EQS/AttackTarget.uasset index 5283654..e25ef2e 100644 --- a/Content/Enemy/EQS/AttackTarget.uasset +++ b/Content/Enemy/EQS/AttackTarget.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b4878cf302d1af0c0940717a91289392ddf7cae9a41fa70fd214c5a2fecf47a3 +oid sha256:c301262f435afa726497425b119c5f5aab80501dd20bee5476a7cfa4dcf4ba3a size 31452 diff --git a/Content/Enemy/EQS/EQS_FindIdealRange.uasset b/Content/Enemy/EQS/EQS_FindIdealRange.uasset new file mode 100644 index 0000000..efb8de4 --- /dev/null +++ b/Content/Enemy/EQS/EQS_FindIdealRange.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69da4619195e312747cc99b0a65cebe3cb6dba7cff6d7f85d65a5bf630b177a6 +size 9598 diff --git a/Content/Enemy/Worker/AIC_Worker.uasset b/Content/Enemy/Worker/AIC_Worker.uasset index 105a3ce..517c5f0 100644 --- a/Content/Enemy/Worker/AIC_Worker.uasset +++ b/Content/Enemy/Worker/AIC_Worker.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:50deb580132c8059cab1d6b9cd9556977ece12cd3300827cb696e1940801377a -size 19654 +oid sha256:eabfe5d0b7d9fb24fd797f5ab6c6b16b90c2dcdfd1d630b7f62645bec170d357 +size 20894 diff --git a/Content/Enemy/Worker/BB_Worker.uasset b/Content/Enemy/Worker/BB_Worker.uasset index bfc4c59..ca26bb3 100644 --- a/Content/Enemy/Worker/BB_Worker.uasset +++ b/Content/Enemy/Worker/BB_Worker.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f11f9cbf3cdff3fad52087639d39c89321a8092ec520bf7be61fec2fed2d2844 -size 4955 +oid sha256:bfe99d758ae52a5cdc8c067a64fb4deb766eb1229663686533360cf4f1c8079a +size 5247 diff --git a/Content/Enemy/Worker/BT_Worker.uasset b/Content/Enemy/Worker/BT_Worker.uasset index 4827207..6dd4eb7 100644 --- a/Content/Enemy/Worker/BT_Worker.uasset +++ b/Content/Enemy/Worker/BT_Worker.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:16efaab8807056f5c92464bd376657dc4a4edcea1b1ed26ad5f77d6f01ccd879 -size 35221 +oid sha256:2a3f2eb952d460100efe964df20f5576e6ea4d788a2a6d32b5442efaba3bf5dc +size 33603 diff --git a/Content/Enemy/Worker/C_Worker.uasset b/Content/Enemy/Worker/C_Worker.uasset index 22843bf..cdcd13d 100644 --- a/Content/Enemy/Worker/C_Worker.uasset +++ b/Content/Enemy/Worker/C_Worker.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cc57648ca59ab9181814d46afa44701b6ab3dd1d1114d60963cda8729b1aee7c -size 40112 +oid sha256:1307c6303ccede36903875211f758077933ef495e0fb8aebe8c45147e9cfb66d +size 38588 diff --git a/Content/Enemy/Worker/PatrolMovementSpeed.uasset b/Content/Enemy/Worker/PatrolMovementSpeed.uasset deleted file mode 100644 index e0d6963..0000000 --- a/Content/Enemy/Worker/PatrolMovementSpeed.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:e9cf595f36b15dce976ce9e9506d74974b44264319c5a4da0a9f76dc7feb8eac -size 2770 diff --git a/Source/Nakatomi/Decorators/BTDCanSeeTarget.cpp b/Source/Nakatomi/Decorators/BTDCanSeeTarget.cpp new file mode 100644 index 0000000..75302e2 --- /dev/null +++ b/Source/Nakatomi/Decorators/BTDCanSeeTarget.cpp @@ -0,0 +1,27 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "../Decorators/BTDCanSeeTarget.h" + +#include "AIController.h" +#include "BehaviorTree/BlackboardComponent.h" + +bool UBTDCanSeeTarget::CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const +{ + FHitResult HitResult; + + FVector Start = OwnerComp.GetAIOwner()->GetPawn()->GetActorLocation(); + + UBlackboardComponent* BlackboardComponent = OwnerComp.GetBlackboardComponent(); + AActor* TargetActor = Cast(BlackboardComponent->GetValueAsObject(TargetActorKey.SelectedKeyName)); + FVector End = TargetActor->GetActorLocation(); + + GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Pawn); + + if (HitResult.GetActor()) + { + return true; + } + + return false; +} diff --git a/Source/Nakatomi/Decorators/BTDCanSeeTarget.h b/Source/Nakatomi/Decorators/BTDCanSeeTarget.h new file mode 100644 index 0000000..e926c3b --- /dev/null +++ b/Source/Nakatomi/Decorators/BTDCanSeeTarget.h @@ -0,0 +1,24 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BehaviorTree/BTDecorator.h" +#include "BTDCanSeeTarget.generated.h" + +/** + * + */ +UCLASS() +class NAKATOMI_API UBTDCanSeeTarget : public UBTDecorator +{ + GENERATED_BODY() + +public: + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Target Actor Key")) + FBlackboardKeySelector TargetActorKey; + +protected: + virtual bool CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const override; +}; diff --git a/Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.cpp b/Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.cpp new file mode 100644 index 0000000..6e21503 --- /dev/null +++ b/Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.cpp @@ -0,0 +1,14 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "../Decorators/BTDIsHealthBelowThreshold.h" + +#include "BehaviorTree/BlackboardComponent.h" + +bool UBTDIsHealthBelowThreshold::CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const +{ + UBlackboardComponent* BlackboardComponent = OwnerComp.GetBlackboardComponent(); + float currentHealth = BlackboardComponent->GetValueAsFloat(CurrentHealthKey.SelectedKeyName); + + return currentHealth <= HealthThreshold ? true : false; +} diff --git a/Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.h b/Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.h new file mode 100644 index 0000000..a65081d --- /dev/null +++ b/Source/Nakatomi/Decorators/BTDIsHealthBelowThreshold.h @@ -0,0 +1,29 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BehaviorTree/BTDecorator.h" +#include "BTDIsHealthBelowThreshold.generated.h" + +/** + * + */ +UCLASS() +class NAKATOMI_API UBTDIsHealthBelowThreshold : public UBTDecorator +{ + GENERATED_BODY() + +public: + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Current Health Key")) + FBlackboardKeySelector CurrentHealthKey; + + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Health Threshold")) + float HealthThreshold = 25.0f; + +protected: + virtual bool CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const override; +}; diff --git a/Source/Nakatomi/EnemyCharacter.cpp b/Source/Nakatomi/EnemyCharacter.cpp index 8018d9e..b23e46f 100644 --- a/Source/Nakatomi/EnemyCharacter.cpp +++ b/Source/Nakatomi/EnemyCharacter.cpp @@ -4,6 +4,9 @@ #include "EnemyAIController.h" #include "EnemyHealthComponent.h" #include "InteractableComponent.h" +#include "BehaviorTree/BehaviorTree.h" +#include "BehaviorTree/BlackboardComponent.h" +#include "BehaviorTree/BlackboardData.h" #define COLLISION_WEAPON ECC_GameTraceChannel1 @@ -16,7 +19,7 @@ AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer) : GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); GetHealthComponent()->SetMaxHealth(100.0f); - + this->Tags.Add(FName("Enemy")); } @@ -54,6 +57,10 @@ void AEnemyCharacter::WeaponCooldownHandler() void AEnemyCharacter::BeginPlay() { Super::BeginPlay(); + + AEnemyAIController* controller = Cast(GetController()); + controller->GetBlackboardComponent()->SetValueAsFloat("CurrentHealth", GetHealthComponent()->GetCurrentHealth()); + GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); } void AEnemyCharacter::PlayOnFireAnimations() @@ -130,3 +137,12 @@ void AEnemyCharacter::ProcessHits(TArray hits) } } } + +void AEnemyCharacter::OnDamaged() +{ + Super::OnDamaged(); + + AEnemyAIController* controller = Cast(GetController()); + controller->GetBlackboardComponent()->SetValueAsFloat("CurrentHealth", GetHealthComponent()->GetCurrentHealth()); +} + diff --git a/Source/Nakatomi/EnemyCharacter.h b/Source/Nakatomi/EnemyCharacter.h index 2f7bbc3..3e24291 100644 --- a/Source/Nakatomi/EnemyCharacter.h +++ b/Source/Nakatomi/EnemyCharacter.h @@ -55,4 +55,7 @@ private: virtual void CalculateHits(TArray* hits) override; virtual void ProcessHits(TArray hits) override; + +protected: + virtual void OnDamaged() override; };