Add perception component
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "AI/CAIController.h"
|
||||
|
||||
#include "BehaviorTree/BlackboardComponent.h"
|
||||
#include "Character/CCharacter.h"
|
||||
#include "Perception/AIPerceptionComponent.h"
|
||||
#include "Perception/AISenseConfig_Sight.h"
|
||||
@@ -15,7 +16,7 @@ ACAIController::ACAIController()
|
||||
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>("Sight config");
|
||||
|
||||
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
|
||||
SightConfig->DetectionByAffiliation.bDetectFriendlies = false;
|
||||
SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
|
||||
SightConfig->DetectionByAffiliation.bDetectNeutrals = false;
|
||||
|
||||
SightConfig->SightRadius = 1000.f;
|
||||
@@ -24,6 +25,7 @@ ACAIController::ACAIController()
|
||||
SightConfig->PeripheralVisionAngleDegrees = 100.f;
|
||||
|
||||
AIPerceptionComponent->ConfigureSense(*SightConfig);
|
||||
AIPerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &ThisClass::TargetPerceptionUpdated);
|
||||
}
|
||||
|
||||
void ACAIController::OnPossess(APawn* InPawn)
|
||||
@@ -40,9 +42,46 @@ void ACAIController::OnPossess(APawn* InPawn)
|
||||
void ACAIController::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
RunBehaviorTree(BehaviorTree);
|
||||
}
|
||||
|
||||
void ACAIController::Tick(float DeltaTime)
|
||||
{
|
||||
Super::Tick(DeltaTime);
|
||||
}
|
||||
|
||||
void ACAIController::TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("Saw you"))
|
||||
// GEngine->AddOnScreenDebugMessage(
|
||||
// -1, 12.f, FColor::Blue,
|
||||
// FString::Printf(TEXT("saw you")));
|
||||
|
||||
if (Stimulus.WasSuccessfullySensed())
|
||||
{
|
||||
if (!GetCurrentTarget()) SetCurrentTarget(TargetActor);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GetCurrentTarget() == TargetActor) SetCurrentTarget(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
const UObject* ACAIController::GetCurrentTarget() const
|
||||
{
|
||||
if (const auto BlackboardComponent{GetBlackboardComponent()})
|
||||
{
|
||||
const auto Target{BlackboardComponent->GetValueAsObject(BlackboardTargetName)};
|
||||
return Target;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ACAIController::SetCurrentTarget(AActor* NewTarget)
|
||||
{
|
||||
const auto BlackboardComponent{GetBlackboardComponent()};
|
||||
if (!BlackboardComponent) return;
|
||||
|
||||
if (NewTarget) BlackboardComponent->SetValueAsObject(BlackboardTargetName, NewTarget);
|
||||
else BlackboardComponent->ClearValue(BlackboardTargetName);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "AIController.h"
|
||||
#include "Perception/AIPerceptionTypes.h"
|
||||
#include "CAIController.generated.h"
|
||||
|
||||
UCLASS()
|
||||
@@ -22,9 +23,21 @@ public:
|
||||
virtual void Tick(float DeltaTime) override;
|
||||
|
||||
private:
|
||||
|
||||
UPROPERTY(EditDefaultsOnly, Category="AI Behavior")
|
||||
FName BlackboardTargetName {"Target"};
|
||||
UFUNCTION()
|
||||
void TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus);
|
||||
|
||||
UPROPERTY(EditDefaultsOnly, Category="AI Behavior")
|
||||
UBehaviorTree* BehaviorTree;
|
||||
|
||||
UPROPERTY(VisibleDefaultsOnly, Category="Perception")
|
||||
UAIPerceptionComponent* AIPerceptionComponent;
|
||||
|
||||
UPROPERTY(VisibleDefaultsOnly, Category="Perception")
|
||||
class UAISenseConfig_Sight* SightConfig;
|
||||
UFUNCTION()
|
||||
const UObject* GetCurrentTarget() const ;
|
||||
void SetCurrentTarget(AActor* NewTarget);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user