Add AIControlHub for AI Alert Level Synchronisation

This commit is contained in:
Philip W 2023-10-09 03:35:42 +01:00
parent a86eb14b35
commit 13e86b5288
6 changed files with 124 additions and 1 deletions

Binary file not shown.

View File

@ -0,0 +1,59 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "AIControlHub.h"
#include "EnemyCharacter.h"
// Sets default values
AAIControlHub::AAIControlHub()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AAIControlHub::BeginPlay()
{
Super::BeginPlay();
for (AEnemyCharacter* EnemyActor : EnemyActors)
{
EnemyActor->SubscribeToAlertLevelEvent(this);
}
}
// Called every frame
void AAIControlHub::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AAIControlHub::IncreaseAlertLevel()
{
AlertLevel = FMath::Clamp(AlertLevel + 1, 0, 3);
OnAlertLevelChanged();
}
void AAIControlHub::DecreaseAlertLevel()
{
AlertLevel = FMath::Clamp(AlertLevel - 1, 0, 3);
OnAlertLevelChanged();
}
void AAIControlHub::SetAlertLevel(int NewAlertLevel)
{
AlertLevel = FMath::Clamp(NewAlertLevel, 0, 3);
OnAlertLevelChanged();
}
int AAIControlHub::GetAlertLevel() const
{
return AlertLevel;
}
void AAIControlHub::OnAlertLevelChanged() const
{
AlertLevelEvent.Broadcast(AlertLevel);
}

View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "EnemyCharacter.h"
#include "GameFramework/Actor.h"
#include "AIControlHub.generated.h"
UCLASS()
class ENDLESSVENDETTA_API AAIControlHub : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AAIControlHub();
DECLARE_EVENT_OneParam(AAI_EnemyController, FAlertLevelEvent, int);
FAlertLevelEvent AlertLevelEvent;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void IncreaseAlertLevel();
void DecreaseAlertLevel();
void SetAlertLevel(int NewAlertLevel);
int GetAlertLevel() const;
void OnAlertLevelChanged() const;
private:
int AlertLevel = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true"))
TArray<AEnemyCharacter*> EnemyActors;
};

View File

@ -26,11 +26,12 @@ public:
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY()
class UAISenseConfig_Sight* SightConfig;
UPROPERTY()
class UAISenseConfig_Hearing* HearingConfig;
void SetupPerceptionSystem();
UFUNCTION()
void OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus);
};

View File

@ -3,6 +3,10 @@
#include "EnemyCharacter.h"
#include "AIControlHub.h"
#include "AIController.h"
#include "BehaviorTree/BlackboardComponent.h"
class UAISense_Sight;
// Sets default values
@ -24,3 +28,13 @@ void AEnemyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AEnemyCharacter::SubscribeToAlertLevelEvent(AAIControlHub* ControlHub)
{
AlertLevelDelegateHandle = ControlHub->AlertLevelEvent.AddUObject(this, &AEnemyCharacter::SetAlertLevel);
}
void AEnemyCharacter::SetAlertLevel(const int NewAlertLevel) const
{
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsInt("AlertLevel", NewAlertLevel);
}

View File

@ -24,4 +24,9 @@ protected:
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void SubscribeToAlertLevelEvent(class AAIControlHub* ControlHub);
private:
FDelegateHandle AlertLevelDelegateHandle;
void SetAlertLevel(int NewAlertLevel) const;
};