diff --git a/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset b/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset index 9b2d8e60..6d6bc456 100644 --- a/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset +++ b/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6030578986ba57b3d604b31bedc71a3abf0cb4de5dc6658d58c37679ba4c440a -size 5988 +oid sha256:1aff4919ba640ff5f41aabd18e3f45977e9d63d434f7e93cdab5d84b3b7df8f6 +size 6288 diff --git a/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset b/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset index 014a302f..45c0d2a1 100644 --- a/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset +++ b/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9d20759902d270772c9aad0fa9521dfd3e343b82eb5818e82a39b1b11280ec96 -size 37942 +oid sha256:b818b3645e40ca126811ffdc0a3d59b41fec38fc50f493cf9a7f9b78aa41554e +size 60604 diff --git a/EndlessVendetta/Content/__ExternalActors__/Levels/EnemyAITest/6/VW/F67BYXWE15N6HDB8XL4DCA.uasset b/EndlessVendetta/Content/__ExternalActors__/Levels/EnemyAITest/6/VW/F67BYXWE15N6HDB8XL4DCA.uasset new file mode 100644 index 00000000..09906e45 --- /dev/null +++ b/EndlessVendetta/Content/__ExternalActors__/Levels/EnemyAITest/6/VW/F67BYXWE15N6HDB8XL4DCA.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:904e9dbfb5067e107d30cced28a7a621781ffdf8644f45aceba52a9d78264b47 +size 4413 diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp index 688ede79..75f4a7f1 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp @@ -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; + } } diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h index 95a84534..d75b7fd6 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h @@ -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 EnemyActors; + + UPROPERTY() + FVector PlayerLastKnownLocation; }; diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp index e971fab8..aa84cb9b 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp @@ -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(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(GetController())->GetBlackboardComponent()->SetValueAsVector("LastKnownLocation", PlayerLastKnownLocation); +} diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h index 7a0278e6..6d3deab7 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h +++ b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h @@ -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); }; diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_SetLocalAlertLevel.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_SetLocalAlertLevel.cpp new file mode 100644 index 00000000..3cd9990b --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_SetLocalAlertLevel.cpp @@ -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(OwnerComp.GetAIOwner())) + { + if (const AEnemyCharacter* const EnemyCharacter = Cast(AIEnemyController->GetPawn())) + { + EnemyCharacter->SetLocalAlertLevel(AlertLevel); + + FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded); + return EBTNodeResult::Succeeded; + } + } + return EBTNodeResult::Failed; +} diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_SetLocalAlertLevel.h b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_SetLocalAlertLevel.h new file mode 100644 index 00000000..770472ed --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_SetLocalAlertLevel.h @@ -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; +};