9#include "Kismet/KismetMathLibrary.h"
11USightSystem::USightSystem()
13 PrimaryComponentTick.bCanEverTick =
true;
16void USightSystem::BeginPlay()
21void USightSystem::TickComponent(
float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
23 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
25 if ( !IsValid(TargetActor) || !IsValid(OwnerActor) )
28 this->SightLineTrace(SightLength, SightAngle);
30 if ( DetectSightLength(SightLength) && DetectSightAngle(SightAngle) )
36 this->AddElapsedTime();
45 OnSightDetect.Broadcast(DetectTarget);
57 OnSightDetect.Broadcast(DetectTarget);
62 this->AddElapsedTime();
69void USightSystem::InitSightSystem(AActor* InTargetActor,
const float InSightLength,
const float InSightAngle)
71 this->OwnerActor = GetOwner();
72 this->TargetActor = InTargetActor;
73 this->SightLength = InSightLength;
74 this->SightAngle = InSightAngle;
77void USightSystem::SightLineTrace(
float InLength,
float InAngle)
const
79 if ( !bDrawDebugLine )
82 UWorld* OwnerWorld = OwnerActor->GetWorld();
87 const auto Start = OwnerActor->GetActorLocation();
88 const auto EndDirection = OwnerActor->GetActorForwardVector();
89 const auto End = Start + EndDirection * InLength;
92 OwnerWorld, Start, End, LineColor,
101 const auto Start = OwnerActor->GetActorLocation();
102 const FRotator YawRotation(0.f, -InAngle, 0.f);
103 const FVector EndDirection = OwnerActor->GetActorForwardVector().RotateAngleAxis(YawRotation.Yaw, FVector::UpVector);
104 auto End = Start + EndDirection * InLength;
107 OwnerWorld, Start, End, LineColor,
116 const auto Start = OwnerActor->GetActorLocation();
117 const FRotator YawRotation(0.f, InAngle, 0.f);
118 const FVector EndDirection = OwnerActor->GetActorForwardVector().RotateAngleAxis(YawRotation.Yaw, FVector::UpVector);
119 auto End = Start + EndDirection * InLength;
122 OwnerWorld, Start, End, LineColor,
131bool USightSystem::DetectSightLength(
float InLength)
const
133 const float Dist = FVector::Dist( TargetActor->GetActorLocation(), OwnerActor->GetActorLocation());
134 return Dist < InLength;
137bool USightSystem::DetectSightAngle(
float InAngle)
const
139 auto Direction = TargetActor->GetActorLocation() - OwnerActor->GetActorLocation();
140 Direction.Normalize();
142 const float Dot = FVector::DotProduct(OwnerActor->GetActorForwardVector(), Direction);
143 const float AngleDeg = FMath::RadiansToDegrees(FMath::Acos(Dot));
146 return 0.0f <= AngleDeg && AngleDeg <= InAngle;
149void USightSystem::AddElapsedTime()
151 ElapsedTime += GetWorld()->GetDeltaSeconds();
154float USightSystem::LerpAlpha()
const
156 return UKismetMathLibrary::FClamp( ElapsedTime / Duration, 0.0f, 1.0f );
YiSan 전반에서 사용하는 공용 인터페이스를 선언합니다.
static const FColor Green(50, 255, 50, 255)
static const FColor Pink(255, 0, 108, 255)