Update Enemy AI for Hearing Perception

This commit is contained in:
Philip W 2023-10-09 03:01:41 +01:00
parent b6fd4e0d75
commit a86eb14b35
7 changed files with 87 additions and 23 deletions

Binary file not shown.

Binary file not shown.

View File

@ -7,6 +7,7 @@
#include "BehaviorTree/BlackboardComponent.h" #include "BehaviorTree/BlackboardComponent.h"
#include "EndlessVendetta/EndlessVendettaCharacter.h" #include "EndlessVendetta/EndlessVendettaCharacter.h"
#include "Perception/AIPerceptionComponent.h" #include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Hearing.h"
#include "Perception/AISenseConfig_Sight.h" #include "Perception/AISenseConfig_Sight.h"
@ -53,9 +54,6 @@ void AAI_EnemyController::Tick(float DeltaTime)
void AAI_EnemyController::SetupPerceptionSystem() void AAI_EnemyController::SetupPerceptionSystem()
{ {
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config")); SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
if (IsValid(SightConfig))
{
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
SightConfig->SightRadius = 2000.0f; SightConfig->SightRadius = 2000.0f;
SightConfig->LoseSightRadius = 2100.0f; SightConfig->LoseSightRadius = 2100.0f;
SightConfig->PeripheralVisionAngleDegrees = 70.0f; SightConfig->PeripheralVisionAngleDegrees = 70.0f;
@ -64,27 +62,45 @@ void AAI_EnemyController::SetupPerceptionSystem()
SightConfig->DetectionByAffiliation.bDetectEnemies = true; SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectFriendlies = true; SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
SightConfig->DetectionByAffiliation.bDetectNeutrals = true; SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
HearingConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("Hearing Config"));
HearingConfig->HearingRange = 2000.0f;
HearingConfig->DetectionByAffiliation.bDetectEnemies = true;
HearingConfig->DetectionByAffiliation.bDetectFriendlies = true;
HearingConfig->DetectionByAffiliation.bDetectNeutrals = true;
if (!IsValid(SightConfig)) return;
if (!IsValid(HearingConfig)) return;
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation()); GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAI_EnemyController::OnTargetPerceptionUpdated); GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAI_EnemyController::OnTargetPerceptionUpdated);
GetPerceptionComponent()->ConfigureSense(*SightConfig); GetPerceptionComponent()->ConfigureSense(*SightConfig);
} GetPerceptionComponent()->ConfigureSense(*HearingConfig);
} }
void AAI_EnemyController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus) void AAI_EnemyController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus)
{ {
if (AEndlessVendettaCharacter* const PlayerCharacter = Cast<AEndlessVendettaCharacter>(Actor)) if (AEndlessVendettaCharacter* const PlayerCharacter = Cast<AEndlessVendettaCharacter>(Actor))
{ {
if (Stimulus.WasSuccessfullySensed()) if (Stimulus.WasSuccessfullySensed() && Stimulus.Type == SightConfig->GetSenseID())
{ {
GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor); GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor);
GetBlackboardComponent()->SetValueAsVector("TargetLocation", Stimulus.StimulusLocation); GetBlackboardComponent()->SetValueAsVector("TargetLocation", Stimulus.StimulusLocation);
GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", true); GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", true);
} }
else else if (!Stimulus.WasSuccessfullySensed() && Stimulus.Type == SightConfig->GetSenseID())
{ {
GetBlackboardComponent()->ClearValue("TargetActor"); GetBlackboardComponent()->ClearValue("TargetActor");
GetBlackboardComponent()->ClearValue("TargetLocation"); GetBlackboardComponent()->ClearValue("TargetLocation");
GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", false); GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", false);
} }
if (Stimulus.WasSuccessfullySensed() && Stimulus.Type == HearingConfig->GetSenseID())
{
GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor);
GetBlackboardComponent()->SetValueAsVector("InvestigationLocation", Stimulus.StimulusLocation);
GetBlackboardComponent()->SetValueAsBool("IsInvestigating", true);
}
} }
} }

View File

@ -27,6 +27,7 @@ public:
private: private:
class UAISenseConfig_Sight* SightConfig; class UAISenseConfig_Sight* SightConfig;
class UAISenseConfig_Hearing* HearingConfig;
void SetupPerceptionSystem(); void SetupPerceptionSystem();
UFUNCTION() UFUNCTION()

View File

@ -0,0 +1,27 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "BTTask_StopInvestigating.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "EndlessVendetta/AI/AI_EnemyController.h"
#include "EndlessVendetta/AI/EnemyCharacter.h"
UBTTask_StopInvestigating::UBTTask_StopInvestigating()
{
NodeName = TEXT("Stop Investigating");
}
EBTNodeResult::Type UBTTask_StopInvestigating::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
if (AAI_EnemyController* const EnemyController = Cast<AAI_EnemyController>(OwnerComp.GetOwner()))
{
if (UBlackboardComponent* const BlackboardComponent = OwnerComp.GetBlackboardComponent())
{
BlackboardComponent->ClearValue(GetSelectedBlackboardKey());
FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
return EBTNodeResult::Succeeded;
}
}
return EBTNodeResult::Failed;
}

View File

@ -0,0 +1,20 @@
// 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_StopInvestigating.generated.h"
/**
*
*/
UCLASS()
class ENDLESSVENDETTA_API UBTTask_StopInvestigating : public UBTTask_BlackboardBase
{
GENERATED_BODY()
public:
UBTTask_StopInvestigating();
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
};