84 lines
2.8 KiB
C++
84 lines
2.8 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "AI/CombatAIController.h"
|
|
|
|
#include "GameplayTagAssetInterface.h"
|
|
#include "MasterAI.h"
|
|
#include "BehaviorTree/BlackboardComponent.h"
|
|
#include "Perception/AIPerceptionComponent.h"
|
|
#include "Perception/AISenseConfig_Damage.h"
|
|
#include "Perception/AISenseConfig_Sight.h"
|
|
|
|
ACombatAIController::ACombatAIController()
|
|
{
|
|
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComponent"));
|
|
|
|
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightConfig"));
|
|
SightConfig->SightRadius = 3000.f;
|
|
SightConfig->LoseSightRadius = 3500.f;
|
|
SightConfig->PeripheralVisionAngleDegrees = 45.f;
|
|
FAISenseAffiliationFilter AffiliationFilter;
|
|
AffiliationFilter.bDetectEnemies = true;
|
|
AffiliationFilter.bDetectFriendlies = true;
|
|
AffiliationFilter.bDetectNeutrals = true;
|
|
SightConfig->DetectionByAffiliation = AffiliationFilter;
|
|
SightConfig->AutoSuccessRangeFromLastSeenLocation = 500.f;
|
|
PerceptionComponent->ConfigureSense(*SightConfig);
|
|
|
|
DamageConfig = CreateDefaultSubobject<UAISenseConfig_Damage>(TEXT("DamageConfig"));
|
|
DamageConfig->SetMaxAge(3.f); // Expired 3 Seconds
|
|
PerceptionComponent->ConfigureSense(*DamageConfig);
|
|
|
|
PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &ACombatAIController::OnUpdatePerception);
|
|
}
|
|
|
|
void ACombatAIController::OnPossess(APawn* InPawn)
|
|
{
|
|
Super::OnPossess(InPawn);
|
|
|
|
AMasterAI* AIpawn = Cast<AMasterAI>(InPawn);
|
|
if(AIpawn)
|
|
{
|
|
MasterAI = AIpawn;
|
|
RunBehaviorTree(MasterAI->GetBeHaviorTree());
|
|
}
|
|
}
|
|
|
|
void ACombatAIController::OnUpdatePerception(const TArray<AActor*>& PerceivedActors)
|
|
{
|
|
for (auto SensoredActor : PerceivedActors)
|
|
{
|
|
FActorPerceptionBlueprintInfo PerceptionInfo;
|
|
GetPerceptionComponent()->GetActorsPerception(SensoredActor, PerceptionInfo);
|
|
|
|
for(int i = 0; i < PerceptionInfo.LastSensedStimuli.Num(); i++)
|
|
{
|
|
auto Info = PerceptionInfo.LastSensedStimuli[i];
|
|
auto SensedClass = UAIPerceptionSystem::GetSenseClassForStimulus(this, Info);
|
|
if(SensedClass == UAISense_Sight::StaticClass())
|
|
{
|
|
if(Info.WasSuccessfullySensed())
|
|
{
|
|
if(Cast<IGameplayTagAssetInterface>(SensoredActor)->HasMatchingGameplayTag(FCombatGameplayTags::Get().Character_Player))
|
|
{
|
|
Blackboard->SetValueAsObject(TEXT("Target"), SensoredActor);
|
|
}
|
|
}
|
|
else
|
|
Blackboard->SetValueAsObject(TEXT("Target"), nullptr);
|
|
}
|
|
else if(SensedClass == UAISense_Damage::StaticClass())
|
|
{
|
|
if(Info.WasSuccessfullySensed() && !Info.IsExpired()) //After DamageConfig->GetMaxAge then Expired
|
|
{
|
|
if(Cast<IGameplayTagAssetInterface>(SensoredActor)->HasMatchingGameplayTag(FCombatGameplayTags::Get().Character_Player))
|
|
{
|
|
Blackboard->SetValueAsObject(TEXT("Target"), SensoredActor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|