82 lines
2.4 KiB
C++
82 lines
2.4 KiB
C++
// © 2025 Naked People Team. All Rights Reserved.
|
|
|
|
|
|
#include "NPC.h"
|
|
#include "NPCTypeDefinition.h"
|
|
#include "NPCAIController.h"
|
|
#include "Components/SkeletalMeshComponent.h"
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
|
|
ANPC::ANPC()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = false;
|
|
|
|
GetCharacterMovement()->MaxWalkSpeed = 200.0f;
|
|
GetCharacterMovement()->bUseControllerDesiredRotation = true;
|
|
GetCharacterMovement()->bOrientRotationToMovement = false;
|
|
bUseControllerRotationYaw = false;
|
|
AutoPossessAI = EAutoPossessAI::PlacedInWorldOrSpawned;
|
|
|
|
// Crowd anim hygiene (mesh-agnostic): only evaluate the pose while the NPC is on screen and
|
|
// throttle the eval rate by distance. CharacterMovement still ticks off-screen so pooled-in
|
|
// NPCs can keep walking; only the visible pose freezes when unrendered.
|
|
if (USkeletalMeshComponent* MeshComp = GetMesh())
|
|
{
|
|
MeshComp->VisibilityBasedAnimTickOption = EVisibilityBasedAnimTickOption::OnlyTickPoseWhenRendered;
|
|
MeshComp->bEnableUpdateRateOptimizations = true;
|
|
}
|
|
}
|
|
|
|
ENPCType ANPC::GetNPCType() const
|
|
{
|
|
return NPCTypeDefinition ? NPCTypeDefinition->Type : ENPCType::Walker;
|
|
}
|
|
|
|
float ANPC::GetObservationWeight() const
|
|
{
|
|
return NPCTypeDefinition ? NPCTypeDefinition->ObservationWeight : 1.0f;
|
|
}
|
|
|
|
bool ANPC::ShouldStopToObserve() const
|
|
{
|
|
return NPCTypeDefinition ? NPCTypeDefinition->bStopsToObserve : false;
|
|
}
|
|
|
|
float ANPC::GetObserveDuration() const
|
|
{
|
|
return NPCTypeDefinition ? NPCTypeDefinition->ObserveDurationSeconds : 0.0f;
|
|
}
|
|
|
|
void ANPC::ActivateFromPool(const FVector& Location, const FRotator& Rotation)
|
|
{
|
|
SetActorLocationAndRotation(Location, Rotation, false, nullptr, ETeleportType::TeleportPhysics);
|
|
SetActorHiddenInGame(false);
|
|
SetActorEnableCollision(true);
|
|
|
|
if (UCharacterMovementComponent* Move = GetCharacterMovement())
|
|
{
|
|
Move->SetComponentTickEnabled(true);
|
|
Move->SetMovementMode(MOVE_Walking);
|
|
}
|
|
|
|
OnActivatedFromPool();
|
|
}
|
|
|
|
void ANPC::DeactivateToPool()
|
|
{
|
|
// Drop any active observation so a pooled-out NPC stops contributing to the player's embarrassment.
|
|
if (ANPCAIController* NPCController = Cast<ANPCAIController>(GetController()))
|
|
NPCController->ClearObservation();
|
|
|
|
if (UCharacterMovementComponent* Move = GetCharacterMovement())
|
|
{
|
|
Move->StopMovementImmediately();
|
|
Move->DisableMovement();
|
|
Move->SetComponentTickEnabled(false);
|
|
}
|
|
|
|
SetActorHiddenInGame(true);
|
|
SetActorEnableCollision(false);
|
|
|
|
OnDeactivatedToPool();
|
|
} |