From 6127b5cd813fd0557de9043841e26939ce1f4b87 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Mon, 13 Mar 2023 23:01:12 +0000 Subject: [PATCH] Add PerceptionComponent to `PlayerCharacter` and `EnemyCharacter` --- Source/Nakatomi/EnemyAIController.cpp | 13 ++++++++++++- Source/Nakatomi/EnemyAIController.h | 3 +++ Source/Nakatomi/EnemyCharacter.h | 2 ++ Source/Nakatomi/PlayerCharacter.h | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Source/Nakatomi/EnemyAIController.cpp b/Source/Nakatomi/EnemyAIController.cpp index d432c4a..d4623c5 100644 --- a/Source/Nakatomi/EnemyAIController.cpp +++ b/Source/Nakatomi/EnemyAIController.cpp @@ -8,6 +8,15 @@ AEnemyAIController::AEnemyAIController(const FObjectInitializer& object_initiali { Blackboard = CreateDefaultSubobject(TEXT("Blackboard")); BehaviorTree = CreateDefaultSubobject(TEXT("Behavior Tree")); + PerceptionComponent = CreateDefaultSubobject(TEXT("Perception Component")); + AIPerception = PerceptionComponent; + + UAISenseConfig_Sight* ConfigSight = CreateDefaultSubobject(TEXT("Sight Sense Config")); + ConfigSight->SightRadius = 1000.0f; + ConfigSight->LoseSightRadius = 1000.0f; + ConfigSight->PeripheralVisionAngleDegrees = 30.0f; + + AIPerception->ConfigureSense(*ConfigSight); } void AEnemyAIController::OnPossess(APawn* InPawn) @@ -98,7 +107,7 @@ void AEnemyAIController::OnPerceptionUpdated(const TArray& actors) } FActorPerceptionBlueprintInfo perceptionInfo; - GetPerceptionComponent()->GetActorsPerception(actor, perceptionInfo); + PerceptionComponent->GetActorsPerception(actor, perceptionInfo); for (auto& stimulus : perceptionInfo.LastSensedStimuli) { @@ -107,6 +116,8 @@ void AEnemyAIController::OnPerceptionUpdated(const TArray& actors) continue; } + Blackboard->SetValueAsObject("TargetActor", actor); + if (stimulus.IsActive()) { PlayerCharacter = Cast(actor); diff --git a/Source/Nakatomi/EnemyAIController.h b/Source/Nakatomi/EnemyAIController.h index 4b0e154..d9622cc 100644 --- a/Source/Nakatomi/EnemyAIController.h +++ b/Source/Nakatomi/EnemyAIController.h @@ -28,6 +28,9 @@ private: APlayerCharacter* PlayerCharacter; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) + UAIPerceptionComponent* AIPerception; + public: AEnemyAIController(const FObjectInitializer& object_initializer); diff --git a/Source/Nakatomi/EnemyCharacter.h b/Source/Nakatomi/EnemyCharacter.h index 5bcd4ea..585dc2e 100644 --- a/Source/Nakatomi/EnemyCharacter.h +++ b/Source/Nakatomi/EnemyCharacter.h @@ -20,6 +20,8 @@ class NAKATOMI_API AEnemyCharacter : public ANakatomiCharacter GENERATED_BODY() private: + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) UAIPerceptionComponent* PerceptionComponent; UAISenseConfig_Sight* SightConfig; diff --git a/Source/Nakatomi/PlayerCharacter.h b/Source/Nakatomi/PlayerCharacter.h index f8cffec..d9b48ad 100644 --- a/Source/Nakatomi/PlayerCharacter.h +++ b/Source/Nakatomi/PlayerCharacter.h @@ -81,6 +81,7 @@ private: class UUserWidget* currentPlayerHUD; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) UAIPerceptionStimuliSourceComponent* PerceptionSource; public: