KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
USightSystem.cpp
이 파일의 문서화 페이지로 가기
1// Copyright (c) 2025 Doppleddiggong. All rights reserved. Unauthorized copying, modification, or distribution of this file, via any medium is strictly prohibited. Proprietary and confidential.
2
7#include "USightSystem.h"
8#include "GameColor.h"
9#include "Kismet/KismetMathLibrary.h"
10
11USightSystem::USightSystem()
12{
13 PrimaryComponentTick.bCanEverTick = true;
14}
15
16void USightSystem::BeginPlay()
17{
18 Super::BeginPlay();
19}
20
21void USightSystem::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
22{
23 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
24
25 if ( !IsValid(TargetActor) || !IsValid(OwnerActor) )
26 return;
27
28 this->SightLineTrace(SightLength, SightAngle);
29
30 if ( DetectSightLength(SightLength) && DetectSightAngle(SightAngle) )
31 {
32 // 탐지된 상태
33 if ( DetectTarget )
34 {
35 // 현재 탐지 상태값과 동일함
36 this->AddElapsedTime();
37 // NOTE. 탐지된 시간에 비례해서 무엇인가 하고 싶다면..
38 }
39 else
40 {
41 // 현재 탐지 상태값과 다름
42 DetectTarget = true;
43 ElapsedTime = 0.0f;
44
45 OnSightDetect.Broadcast(DetectTarget);
46 }
47 }
48 else
49 {
50 // 탐지되지 않은 상태
51 if ( DetectTarget )
52 {
53 // 현재 탐지 상태값과 다름
54 DetectTarget = false;
55 ElapsedTime = 0.0f;
56
57 OnSightDetect.Broadcast(DetectTarget);
58 }
59 else
60 {
61 // 현재 탐지 상태값과 동일함
62 this->AddElapsedTime();
63
64 // NOTE. 탐지되지않은 시간에 비례해서 무엇인가 하고 싶다면..
65 }
66 }
67}
68
69void USightSystem::InitSightSystem(AActor* InTargetActor, const float InSightLength, const float InSightAngle)
70{
71 this->OwnerActor = GetOwner();
72 this->TargetActor = InTargetActor;
73 this->SightLength = InSightLength;
74 this->SightAngle = InSightAngle;
75}
76
77void USightSystem::SightLineTrace(float InLength, float InAngle) const
78{
79 if ( !bDrawDebugLine )
80 return;
81
82 UWorld* OwnerWorld = OwnerActor->GetWorld();
83 auto LineColor = DetectTarget ? GameColor::Pink : GameColor::Green;
84
85 {
86 {
87 const auto Start = OwnerActor->GetActorLocation();
88 const auto EndDirection = OwnerActor->GetActorForwardVector();
89 const auto End = Start + EndDirection * InLength;
90
91 DrawDebugLine(
92 OwnerWorld, Start, End, LineColor,
93 false,
94 0.0f,
95 0,
96 1.0f );
97 }
98
99 {
100 // Left
101 const auto Start = OwnerActor->GetActorLocation();
102 const FRotator YawRotation(0.f, -InAngle, 0.f); // -각도만큼 Yaw 회전
103 const FVector EndDirection = OwnerActor->GetActorForwardVector().RotateAngleAxis(YawRotation.Yaw, FVector::UpVector);
104 auto End = Start + EndDirection * InLength;
105
106 DrawDebugLine(
107 OwnerWorld, Start, End, LineColor,
108 false,
109 0.0f,
110 0,
111 1.0f );
112 }
113
114 {
115 // Right
116 const auto Start = OwnerActor->GetActorLocation();
117 const FRotator YawRotation(0.f, InAngle, 0.f); // -각도만큼 Yaw 회전
118 const FVector EndDirection = OwnerActor->GetActorForwardVector().RotateAngleAxis(YawRotation.Yaw, FVector::UpVector);
119 auto End = Start + EndDirection * InLength;
120
121 DrawDebugLine(
122 OwnerWorld, Start, End, LineColor,
123 false,
124 0.0f,
125 0,
126 1.0f );
127 }
128 }
129}
130
131bool USightSystem::DetectSightLength(float InLength) const
132{
133 const float Dist = FVector::Dist( TargetActor->GetActorLocation(), OwnerActor->GetActorLocation());
134 return Dist < InLength;
135}
136
137bool USightSystem::DetectSightAngle(float InAngle) const
138{
139 auto Direction = TargetActor->GetActorLocation() - OwnerActor->GetActorLocation();
140 Direction.Normalize();
141
142 const float Dot = FVector::DotProduct(OwnerActor->GetActorForwardVector(), Direction);
143 const float AngleDeg = FMath::RadiansToDegrees(FMath::Acos(Dot));
144
145 // 시야각 안에 들어왔는지 여부
146 return 0.0f <= AngleDeg && AngleDeg <= InAngle;
147}
148
149void USightSystem::AddElapsedTime()
150{
151 ElapsedTime += GetWorld()->GetDeltaSeconds();
152}
153
154float USightSystem::LerpAlpha() const
155{
156 return UKismetMathLibrary::FClamp( ElapsedTime / Duration, 0.0f, 1.0f );
157}
YiSan 전반에서 사용하는 공용 인터페이스를 선언합니다.
USightSystem 클래스를 선언합니다.
static const FColor Green(50, 255, 50, 255)
static const FColor Pink(255, 0, 108, 255)