From e0d5526a6320ae5411d5de82c10aea6b0ac3debd Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Tue, 5 Sep 2023 18:39:14 +0100 Subject: [PATCH] Move Perception Component from EnemyCharacter to EnemyAIController --- Source/Nakatomi/EnemyAIController.cpp | 30 +++++++++++++++++---------- Source/Nakatomi/EnemyAIController.h | 7 +++---- Source/Nakatomi/EnemyCharacter.cpp | 21 +------------------ Source/Nakatomi/EnemyCharacter.h | 9 -------- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/Source/Nakatomi/EnemyAIController.cpp b/Source/Nakatomi/EnemyAIController.cpp index 0edf346..8aaefa0 100644 --- a/Source/Nakatomi/EnemyAIController.cpp +++ b/Source/Nakatomi/EnemyAIController.cpp @@ -1,10 +1,14 @@ // Fill out your copyright notice in the Description page of Project Settings. #include "EnemyAIController.h" -#include "Engine/EngineTypes.h" -#include "Components/CapsuleComponent.h" -#include "NakatomiGameInstance.h" #include +#include "NakatomiGameInstance.h" +#include "BehaviorTree/BehaviorTree.h" +#include "BehaviorTree/BlackboardComponent.h" +#include "Components/CapsuleComponent.h" +#include "Engine/EngineTypes.h" +#include "GameFramework/CharacterMovementComponent.h" +#include "Perception/AIPerceptionComponent.h" AEnemyAIController::AEnemyAIController(const FObjectInitializer& object_initializer) { @@ -13,12 +17,16 @@ AEnemyAIController::AEnemyAIController(const FObjectInitializer& object_initiali 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; + SightConfig = CreateDefaultSubobject(TEXT("Sight Sense Config")); + SightConfig->SightRadius = 700.0f; + SightConfig->LoseSightRadius = 850.0f; + SightConfig->PeripheralVisionAngleDegrees = 90.0f; + SightConfig->SetMaxAge(5.0f); + SightConfig->DetectionByAffiliation.bDetectEnemies = true; + SightConfig->DetectionByAffiliation.bDetectNeutrals = true; - AIPerception->ConfigureSense(*ConfigSight); + AIPerception->SetDominantSense(SightConfig->GetSenseImplementation()); + AIPerception->ConfigureSense(*SightConfig); } void AEnemyAIController::OnPossess(APawn* InPawn) @@ -28,8 +36,8 @@ void AEnemyAIController::OnPossess(APawn* InPawn) auto enemy = Cast(InPawn); check(enemy); - SetPerceptionComponent(*enemy->GetPerceptionComponent()); - enemy->GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AEnemyAIController::OnPerceptionUpdated); + SetPerceptionComponent(*PerceptionComponent); + PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AEnemyAIController::OnPerceptionUpdated); enemy->GetCharacterMovement()->MaxWalkSpeed = 500.f; enemy->GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); enemy->GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); @@ -149,7 +157,7 @@ bool AEnemyAIController::TryObtainAttackToken() { HasAttackToken = gameInstance->GetAIAttackTokenManager()->RequestToken(); } - + return false; } diff --git a/Source/Nakatomi/EnemyAIController.h b/Source/Nakatomi/EnemyAIController.h index f3a64cd..3122437 100644 --- a/Source/Nakatomi/EnemyAIController.h +++ b/Source/Nakatomi/EnemyAIController.h @@ -4,12 +4,9 @@ #include "CoreMinimal.h" #include "AIController.h" -#include "BehaviorTree/BehaviorTreeComponent.h" -#include "BehaviorTree/BehaviorTree.h" -#include "BehaviorTree/BlackboardComponent.h" #include "EnemyCharacter.h" -#include "GameFramework/CharacterMovementComponent.h" #include "PlayerCharacter.h" +#include "Perception/AISenseConfig_Sight.h" #include "EnemyAIController.generated.h" /** @@ -30,6 +27,8 @@ private: UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) UAIPerceptionComponent* AIPerception; + UAISenseConfig_Sight* SightConfig; + bool HasAttackToken = false; public: diff --git a/Source/Nakatomi/EnemyCharacter.cpp b/Source/Nakatomi/EnemyCharacter.cpp index e849be4..c7b5d01 100644 --- a/Source/Nakatomi/EnemyCharacter.cpp +++ b/Source/Nakatomi/EnemyCharacter.cpp @@ -1,26 +1,12 @@ // Fill out your copyright notice in the Description page of Project Settings. #include "EnemyCharacter.h" -#include "GameFramework/CharacterMovementComponent.h" #include "EnemyAIController.h" #define COLLISION_WEAPON ECC_GameTraceChannel1 AEnemyCharacter::AEnemyCharacter() { - PerceptionComponent = CreateDefaultSubobject(TEXT("Perception Component")); - - SightConfig = CreateDefaultSubobject(TEXT("Sight Configuration")); - SightConfig->SightRadius = 700.0f; - SightConfig->LoseSightRadius = 850.0f; - SightConfig->PeripheralVisionAngleDegrees = 90.0f; - SightConfig->SetMaxAge(5.0f); - SightConfig->DetectionByAffiliation.bDetectEnemies = true; - SightConfig->DetectionByAffiliation.bDetectNeutrals = true; - - PerceptionComponent->SetDominantSense(SightConfig->GetSenseImplementation()); - PerceptionComponent->ConfigureSense(*SightConfig); - RandomWeaponParameters = CreateDefaultSubobject(TEXT("Random Weapon Parameters")); GetHealthComponent()->SetMaxHealth(100.0f); @@ -33,11 +19,6 @@ UBehaviorTree* AEnemyCharacter::GetBehaviourTree() return BehaviourTree; } -UAIPerceptionComponent* AEnemyCharacter::GetPerceptionComponent() -{ - return PerceptionComponent; -} - void AEnemyCharacter::OnFire() { CurrentWeapon->SetCurrentWeaponStatus(Firing); @@ -53,7 +34,7 @@ void AEnemyCharacter::OnFire() CurrentWeapon->SetCurrentWeaponStatus(Cooldown); GetWorldTimerManager().SetTimer(CooldownTimerHandle, this, &AEnemyCharacter::WeaponCooldownHandler, - CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true); + CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true); } void AEnemyCharacter::WeaponCooldownHandler() diff --git a/Source/Nakatomi/EnemyCharacter.h b/Source/Nakatomi/EnemyCharacter.h index 3f50307..6bd79fe 100644 --- a/Source/Nakatomi/EnemyCharacter.h +++ b/Source/Nakatomi/EnemyCharacter.h @@ -5,8 +5,6 @@ #include "CoreMinimal.h" #include "NakatomiCharacter.h" #include "BehaviorTree/BehaviorTreeComponent.h" -#include "Perception/AIPerceptionComponent.h" -#include #include "RandomWeaponParameters.h" #include "EnemyCharacter.generated.h" @@ -20,11 +18,6 @@ class NAKATOMI_API AEnemyCharacter : public ANakatomiCharacter GENERATED_BODY() private: - UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) - UAIPerceptionComponent* PerceptionComponent; - - UAISenseConfig_Sight* SightConfig; - UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true")) UBehaviorTree* BehaviourTree; @@ -38,8 +31,6 @@ public: UBehaviorTree* GetBehaviourTree(); - UAIPerceptionComponent* GetPerceptionComponent(); - virtual void OnFire() override; void WeaponCooldownHandler();