From 1c1c56ea46987099aa80e866905fd38e0c6fe2e6 Mon Sep 17 00:00:00 2001 From: Caleb Buhungiro Date: Sun, 14 Sep 2025 19:47:24 +0800 Subject: [PATCH] Add perception component --- Config/DefaultEngine.ini | 8 +++++ Content/AI/Minions/AIC_Minion_BP.uasset | 4 +-- Content/AI/Minions/Minion_BB.uasset | 3 ++ Content/AI/Minions/Minion_BT.uasset | 3 ++ Source/Crunch/Private/AI/CAIController.cpp | 41 +++++++++++++++++++++- Source/Crunch/Public/AI/CAIController.h | 13 +++++++ 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Content/AI/Minions/Minion_BB.uasset create mode 100644 Content/AI/Minions/Minion_BT.uasset diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index a830ed9..2d98a8d 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -93,4 +93,12 @@ ManualIPAddress= [/Script/GameplayDebugger.GameplayDebuggerConfig] CategorySlot5=Five +CategorySlot1=One +CategorySlot2=Two +CategorySlot3=Three +CategorySlot4=Four +CategorySlot6=Six +CategorySlot7=Seven +CategorySlot8=Eight +CategorySlot9=Nine diff --git a/Content/AI/Minions/AIC_Minion_BP.uasset b/Content/AI/Minions/AIC_Minion_BP.uasset index 3ed57bd..2c24988 100644 --- a/Content/AI/Minions/AIC_Minion_BP.uasset +++ b/Content/AI/Minions/AIC_Minion_BP.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:34bdfb6050bbcdf26195ecf4a39574e0ea3a8a9f1b2de2fa2a1d33d6cd762858 -size 20715 +oid sha256:6b50d48e024b6e6efffec66bffe3504f45cfab96c286388b23f8888bc77aa0a2 +size 21493 diff --git a/Content/AI/Minions/Minion_BB.uasset b/Content/AI/Minions/Minion_BB.uasset new file mode 100644 index 0000000..3e385b3 --- /dev/null +++ b/Content/AI/Minions/Minion_BB.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0cda61b43e0330017b3aecddbd59e1b231acb3c648908984724b4a8da27d0459 +size 2325 diff --git a/Content/AI/Minions/Minion_BT.uasset b/Content/AI/Minions/Minion_BT.uasset new file mode 100644 index 0000000..ebd1668 --- /dev/null +++ b/Content/AI/Minions/Minion_BT.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d1723b37fb0124e2cec168ce8fe74504871fed3e54bf5c4118eff9f28404f9d +size 3296 diff --git a/Source/Crunch/Private/AI/CAIController.cpp b/Source/Crunch/Private/AI/CAIController.cpp index 0f72779..4f12204 100644 --- a/Source/Crunch/Private/AI/CAIController.cpp +++ b/Source/Crunch/Private/AI/CAIController.cpp @@ -3,6 +3,7 @@ #include "AI/CAIController.h" +#include "BehaviorTree/BlackboardComponent.h" #include "Character/CCharacter.h" #include "Perception/AIPerceptionComponent.h" #include "Perception/AISenseConfig_Sight.h" @@ -15,7 +16,7 @@ ACAIController::ACAIController() SightConfig = CreateDefaultSubobject("Sight config"); SightConfig->DetectionByAffiliation.bDetectEnemies = true; - SightConfig->DetectionByAffiliation.bDetectFriendlies = false; + SightConfig->DetectionByAffiliation.bDetectFriendlies = true; SightConfig->DetectionByAffiliation.bDetectNeutrals = false; SightConfig->SightRadius = 1000.f; @@ -24,6 +25,7 @@ ACAIController::ACAIController() SightConfig->PeripheralVisionAngleDegrees = 100.f; AIPerceptionComponent->ConfigureSense(*SightConfig); + AIPerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &ThisClass::TargetPerceptionUpdated); } void ACAIController::OnPossess(APawn* InPawn) @@ -40,9 +42,46 @@ void ACAIController::OnPossess(APawn* InPawn) void ACAIController::BeginPlay() { Super::BeginPlay(); + RunBehaviorTree(BehaviorTree); } void ACAIController::Tick(float DeltaTime) { Super::Tick(DeltaTime); } + +void ACAIController::TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus) +{ + UE_LOG(LogTemp, Warning, TEXT("Saw you")) + // GEngine->AddOnScreenDebugMessage( + // -1, 12.f, FColor::Blue, + // FString::Printf(TEXT("saw you"))); + + if (Stimulus.WasSuccessfullySensed()) + { + if (!GetCurrentTarget()) SetCurrentTarget(TargetActor); + } + else + { + if (GetCurrentTarget() == TargetActor) SetCurrentTarget(nullptr); + } +} + +const UObject* ACAIController::GetCurrentTarget() const +{ + if (const auto BlackboardComponent{GetBlackboardComponent()}) + { + const auto Target{BlackboardComponent->GetValueAsObject(BlackboardTargetName)}; + return Target; + } + return nullptr; +} + +void ACAIController::SetCurrentTarget(AActor* NewTarget) +{ + const auto BlackboardComponent{GetBlackboardComponent()}; + if (!BlackboardComponent) return; + + if (NewTarget) BlackboardComponent->SetValueAsObject(BlackboardTargetName, NewTarget); + else BlackboardComponent->ClearValue(BlackboardTargetName); +} diff --git a/Source/Crunch/Public/AI/CAIController.h b/Source/Crunch/Public/AI/CAIController.h index 199d0c0..a0d4b9b 100644 --- a/Source/Crunch/Public/AI/CAIController.h +++ b/Source/Crunch/Public/AI/CAIController.h @@ -4,6 +4,7 @@ #include "CoreMinimal.h" #include "AIController.h" +#include "Perception/AIPerceptionTypes.h" #include "CAIController.generated.h" UCLASS() @@ -22,9 +23,21 @@ public: virtual void Tick(float DeltaTime) override; private: + + UPROPERTY(EditDefaultsOnly, Category="AI Behavior") + FName BlackboardTargetName {"Target"}; + UFUNCTION() + void TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus); + + UPROPERTY(EditDefaultsOnly, Category="AI Behavior") + UBehaviorTree* BehaviorTree; + UPROPERTY(VisibleDefaultsOnly, Category="Perception") UAIPerceptionComponent* AIPerceptionComponent; UPROPERTY(VisibleDefaultsOnly, Category="Perception") class UAISenseConfig_Sight* SightConfig; + UFUNCTION() + const UObject* GetCurrentTarget() const ; + void SetCurrentTarget(AActor* NewTarget); };