Update Enemy AI for Alert Level 2 - Hunt Player

This commit is contained in:
Philip W 2023-10-12 11:35:12 +01:00
parent 32a5e64d00
commit 280644da78
9 changed files with 105 additions and 11 deletions

Binary file not shown.

Binary file not shown.

View File

@ -20,7 +20,7 @@ void AAIControlHub::BeginPlay()
for (AEnemyCharacter* EnemyActor : EnemyActors)
{
EnemyActor->SubscribeToAlertLevelEvent(this);
EnemyActor->SubscribeToGroupAIEvents(this);
}
}
@ -53,7 +53,24 @@ int AAIControlHub::GetAlertLevel() const
return AlertLevel;
}
void AAIControlHub::OnAlertLevelChanged() const
void AAIControlHub::OnAlertLevelChanged()
{
AlertLevelEvent.Broadcast(AlertLevel);
if (AlertLevel == 2)
{
SetPlayerLastKnownLocation();
HuntPlayerEvent.Broadcast(PlayerLastKnownLocation);
}
}
void AAIControlHub::SetPlayerLastKnownLocation(FVector Location)
{
if (Location == FVector(0, 0, 0))
{
PlayerLastKnownLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
}
else
{
PlayerLastKnownLocation = Location;
}
}

View File

@ -16,8 +16,10 @@ public:
// Sets default values for this actor's properties
AAIControlHub();
DECLARE_EVENT_OneParam(AAI_EnemyController, FAlertLevelEvent, int);
DECLARE_EVENT_OneParam(AAIControlHub, FAlertLevelEvent, int);
FAlertLevelEvent AlertLevelEvent;
DECLARE_EVENT_OneParam(AAIControlHub, FHuntPlayer, FVector);
FHuntPlayer HuntPlayerEvent;
protected:
// Called when the game starts or when spawned
@ -31,11 +33,15 @@ public:
void DecreaseAlertLevel();
void SetAlertLevel(int NewAlertLevel);
int GetAlertLevel() const;
void OnAlertLevelChanged() const;
void OnAlertLevelChanged();
void SetPlayerLastKnownLocation(FVector Location = FVector(0, 0, 0));
private:
int AlertLevel = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true"))
TArray<AEnemyCharacter*> EnemyActors;
UPROPERTY()
FVector PlayerLastKnownLocation;
};

View File

@ -29,12 +29,26 @@ void AEnemyCharacter::Tick(float DeltaTime)
Super::Tick(DeltaTime);
}
void AEnemyCharacter::SubscribeToAlertLevelEvent(AAIControlHub* ControlHub)
void AEnemyCharacter::SubscribeToGroupAIEvents(AAIControlHub* ControlHub)
{
DelegatedControlHub = ControlHub;
AlertLevelDelegateHandle = ControlHub->AlertLevelEvent.AddUObject(this, &AEnemyCharacter::SetAlertLevel);
HuntPlayerDelegateHandle = ControlHub->HuntPlayerEvent.AddUObject(this, &AEnemyCharacter::HuntPlayer);
}
void AEnemyCharacter::SetAlertLevel(const int NewAlertLevel) const
{
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsInt("AlertLevel", NewAlertLevel);
}
void AEnemyCharacter::SetLocalAlertLevel(int NewAlertLevel) const
{
if (!IsValid(DelegatedControlHub)) return;
DelegatedControlHub->SetAlertLevel(NewAlertLevel);
}
void AEnemyCharacter::HuntPlayer(FVector PlayerLastKnownLocation)
{
SetAlertLevel(2);
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsVector("LastKnownLocation", PlayerLastKnownLocation);
}

View File

@ -20,13 +20,17 @@ public:
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
class AAIControlHub* DelegatedControlHub;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void SubscribeToAlertLevelEvent(class AAIControlHub* ControlHub);
void SubscribeToGroupAIEvents(class AAIControlHub* ControlHub);
void SetLocalAlertLevel(int NewAlertLevel) const;
private:
FDelegateHandle AlertLevelDelegateHandle;
FDelegateHandle HuntPlayerDelegateHandle;
void SetAlertLevel(int NewAlertLevel) const;
void HuntPlayer(FVector PlayerLastKnowLocation);
};

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "BTTask_SetLocalAlertLevel.h"
#include "EndlessVendetta/AI/AI_EnemyController.h"
#include "EndlessVendetta/AI/EnemyCharacter.h"
UBTTask_SetLocalAlertLevel::UBTTask_SetLocalAlertLevel()
{
NodeName = "Set Local Alert Level";
}
EBTNodeResult::Type UBTTask_SetLocalAlertLevel::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
if (const AAI_EnemyController* const AIEnemyController = Cast<AAI_EnemyController>(OwnerComp.GetAIOwner()))
{
if (const AEnemyCharacter* const EnemyCharacter = Cast<AEnemyCharacter>(AIEnemyController->GetPawn()))
{
EnemyCharacter->SetLocalAlertLevel(AlertLevel);
FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
return EBTNodeResult::Succeeded;
}
}
return EBTNodeResult::Failed;
}

View File

@ -0,0 +1,24 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/Tasks/BTTask_BlackboardBase.h"
#include "BTTask_SetLocalAlertLevel.generated.h"
/**
*
*/
UCLASS()
class ENDLESSVENDETTA_API UBTTask_SetLocalAlertLevel : public UBTTask_BlackboardBase
{
GENERATED_BODY()
public:
UBTTask_SetLocalAlertLevel();
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true"))
int AlertLevel = 0;
};