69 lines
2.1 KiB
C++
69 lines
2.1 KiB
C++
// © 2025 Naked People Team. All Rights Reserved.
|
|
|
|
|
|
#include "NPCAIController.h"
|
|
|
|
#include "NavigationSystem.h"
|
|
#include "NPCTargetLocation.h"
|
|
#include "AI/NavigationSystemBase.h"
|
|
#include "BehaviorTree/BlackboardComponent.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Perception/AISense_Sight.h"
|
|
#include "NakedDesire/Global/NakedDesireGameMode.h"
|
|
#include "NakedDesire/Player/NakedDesireCharacter.h"
|
|
#include "NakedDesire/Stats/StatsManager.h"
|
|
|
|
void ANPCAIController::SetShouldReactToPlayer(const bool Value)
|
|
{
|
|
Blackboard->SetValueAsBool(FName(TEXT("ShouldReactToPlayer")), Value);
|
|
}
|
|
|
|
void ANPCAIController::OnPossess(APawn* InPawn)
|
|
{
|
|
Super::OnPossess(InPawn);
|
|
|
|
NavigationSystem = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
|
|
GameMode = Cast<ANakedDesireGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
|
|
|
|
RunBehaviorTree(BehaviorTreeAsset);
|
|
|
|
const FVector SpawnLocation = InPawn->GetActorLocation();
|
|
|
|
TArray<AActor*> TargetActors;
|
|
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ANPCTargetLocation::StaticClass(), TargetActors);
|
|
|
|
const int RandomIndex = FMath::RandRange(0, TargetActors.Num() - 1);
|
|
Blackboard->SetValueAsVector("TargetLocation", TargetActors[RandomIndex]->GetActorLocation());
|
|
Blackboard->SetValueAsVector("SpawnLocation", SpawnLocation);
|
|
|
|
PlayerCharacter = Cast<ANakedDesireCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
|
|
Blackboard->SetValueAsObject("Player", PlayerCharacter);
|
|
}
|
|
|
|
void ANPCAIController::OnUnpossess()
|
|
{
|
|
if (bCurrentlyObserving && PlayerCharacter)
|
|
{
|
|
PlayerCharacter->StatsManager->SetObserved(false);
|
|
bCurrentlyObserving = false;
|
|
}
|
|
Super::OnUnpossess();
|
|
}
|
|
|
|
void ANPCAIController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
|
|
{
|
|
Super::OnTargetPerceptionUpdated(Actor, Stimulus);
|
|
|
|
if (Actor != PlayerCharacter)
|
|
return;
|
|
if (Stimulus.Type != UAISense::GetSenseID<UAISense_Sight>())
|
|
return;
|
|
|
|
const bool bSensed = Stimulus.WasSuccessfullySensed();
|
|
if (bSensed == bCurrentlyObserving)
|
|
return;
|
|
|
|
bCurrentlyObserving = bSensed;
|
|
PlayerCharacter->StatsManager->SetObserved(bSensed);
|
|
}
|