Move Perception Component from EnemyCharacter to EnemyAIController

This commit is contained in:
Louis Hobbs 2023-09-05 18:39:14 +01:00
parent 0dc524e02d
commit e0d5526a63
4 changed files with 23 additions and 44 deletions

View File

@ -1,10 +1,14 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyAIController.h" #include "EnemyAIController.h"
#include "Engine/EngineTypes.h"
#include "Components/CapsuleComponent.h"
#include "NakatomiGameInstance.h"
#include <Kismet/GameplayStatics.h> #include <Kismet/GameplayStatics.h>
#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) AEnemyAIController::AEnemyAIController(const FObjectInitializer& object_initializer)
{ {
@ -13,12 +17,16 @@ AEnemyAIController::AEnemyAIController(const FObjectInitializer& object_initiali
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")); PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
AIPerception = PerceptionComponent; AIPerception = PerceptionComponent;
UAISenseConfig_Sight* ConfigSight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Sense Config")); SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Sense Config"));
ConfigSight->SightRadius = 1000.0f; SightConfig->SightRadius = 700.0f;
ConfigSight->LoseSightRadius = 1000.0f; SightConfig->LoseSightRadius = 850.0f;
ConfigSight->PeripheralVisionAngleDegrees = 30.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) void AEnemyAIController::OnPossess(APawn* InPawn)
@ -28,8 +36,8 @@ void AEnemyAIController::OnPossess(APawn* InPawn)
auto enemy = Cast<AEnemyCharacter>(InPawn); auto enemy = Cast<AEnemyCharacter>(InPawn);
check(enemy); check(enemy);
SetPerceptionComponent(*enemy->GetPerceptionComponent()); SetPerceptionComponent(*PerceptionComponent);
enemy->GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AEnemyAIController::OnPerceptionUpdated); PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AEnemyAIController::OnPerceptionUpdated);
enemy->GetCharacterMovement()->MaxWalkSpeed = 500.f; enemy->GetCharacterMovement()->MaxWalkSpeed = 500.f;
enemy->GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); enemy->GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
enemy->GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); enemy->GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath");
@ -149,7 +157,7 @@ bool AEnemyAIController::TryObtainAttackToken()
{ {
HasAttackToken = gameInstance->GetAIAttackTokenManager()->RequestToken(); HasAttackToken = gameInstance->GetAIAttackTokenManager()->RequestToken();
} }
return false; return false;
} }

View File

@ -4,12 +4,9 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "AIController.h" #include "AIController.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "EnemyCharacter.h" #include "EnemyCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "PlayerCharacter.h" #include "PlayerCharacter.h"
#include "Perception/AISenseConfig_Sight.h"
#include "EnemyAIController.generated.h" #include "EnemyAIController.generated.h"
/** /**
@ -30,6 +27,8 @@ private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UAIPerceptionComponent* AIPerception; UAIPerceptionComponent* AIPerception;
UAISenseConfig_Sight* SightConfig;
bool HasAttackToken = false; bool HasAttackToken = false;
public: public:

View File

@ -1,26 +1,12 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyCharacter.h" #include "EnemyCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "EnemyAIController.h" #include "EnemyAIController.h"
#define COLLISION_WEAPON ECC_GameTraceChannel1 #define COLLISION_WEAPON ECC_GameTraceChannel1
AEnemyCharacter::AEnemyCharacter() AEnemyCharacter::AEnemyCharacter()
{ {
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(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<URandomWeaponParameters>(TEXT("Random Weapon Parameters")); RandomWeaponParameters = CreateDefaultSubobject<URandomWeaponParameters>(TEXT("Random Weapon Parameters"));
GetHealthComponent()->SetMaxHealth(100.0f); GetHealthComponent()->SetMaxHealth(100.0f);
@ -33,11 +19,6 @@ UBehaviorTree* AEnemyCharacter::GetBehaviourTree()
return BehaviourTree; return BehaviourTree;
} }
UAIPerceptionComponent* AEnemyCharacter::GetPerceptionComponent()
{
return PerceptionComponent;
}
void AEnemyCharacter::OnFire() void AEnemyCharacter::OnFire()
{ {
CurrentWeapon->SetCurrentWeaponStatus(Firing); CurrentWeapon->SetCurrentWeaponStatus(Firing);
@ -53,7 +34,7 @@ void AEnemyCharacter::OnFire()
CurrentWeapon->SetCurrentWeaponStatus(Cooldown); CurrentWeapon->SetCurrentWeaponStatus(Cooldown);
GetWorldTimerManager().SetTimer(CooldownTimerHandle, this, &AEnemyCharacter::WeaponCooldownHandler, GetWorldTimerManager().SetTimer(CooldownTimerHandle, this, &AEnemyCharacter::WeaponCooldownHandler,
CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true); CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true);
} }
void AEnemyCharacter::WeaponCooldownHandler() void AEnemyCharacter::WeaponCooldownHandler()

View File

@ -5,8 +5,6 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "NakatomiCharacter.h" #include "NakatomiCharacter.h"
#include "BehaviorTree/BehaviorTreeComponent.h" #include "BehaviorTree/BehaviorTreeComponent.h"
#include "Perception/AIPerceptionComponent.h"
#include <Perception/AISenseConfig_Sight.h>
#include "RandomWeaponParameters.h" #include "RandomWeaponParameters.h"
#include "EnemyCharacter.generated.h" #include "EnemyCharacter.generated.h"
@ -20,11 +18,6 @@ class NAKATOMI_API AEnemyCharacter : public ANakatomiCharacter
GENERATED_BODY() GENERATED_BODY()
private: private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UAIPerceptionComponent* PerceptionComponent;
UAISenseConfig_Sight* SightConfig;
UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true")) UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true"))
UBehaviorTree* BehaviourTree; UBehaviorTree* BehaviourTree;
@ -38,8 +31,6 @@ public:
UBehaviorTree* GetBehaviourTree(); UBehaviorTree* GetBehaviourTree();
UAIPerceptionComponent* GetPerceptionComponent();
virtual void OnFire() override; virtual void OnFire() override;
void WeaponCooldownHandler(); void WeaponCooldownHandler();