KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
AHolder.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
3#include "AHolder.h"
4#include "luggage.h"
5#include "GameLogging.h"
7#include "Onepiece/Onepiece.h"
8#include "Components/BoxComponent.h"
9#include "Components/StaticMeshComponent.h"
10#include "Components/SkeletalMeshComponent.h"
11#include "Animation/AnimationAsset.h"
12#include "Net/UnrealNetwork.h"
13
15{
16 PrimaryActorTick.bCanEverTick = true;
17
18 // Replication
19 bReplicates = true;
20
21 // Root component
22 USceneComponent* Root = CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneRoot"));
23 RootComponent = Root;
24
25 // Mesh component
26 MeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComponent"));
27 MeshComponent->SetupAttachment(RootComponent);
28
29 // Box collision component
30 BoxCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollision"));
31 BoxCollision->SetupAttachment(MeshComponent);
32 BoxCollision->SetGenerateOverlapEvents(true);
33 BoxCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
34 BoxCollision->SetCollisionResponseToAllChannels(ECR_Overlap);
35
36 // HoldPos component
37 HoldPos = CreateDefaultSubobject<USceneComponent>(TEXT("HoldPos"));
38 HoldPos->SetupAttachment(MeshComponent);
39}
40
42{
43 Super::BeginPlay();
44
45 BoxCollision->OnComponentBeginOverlap.AddDynamic(this, &AHolder::OnBoxOverlapBegin);
46
47 // 머티리얼 파라미터 초기화 (비활성화 상태)
49}
50
51void AHolder::Tick(float DeltaTime)
52{
53 Super::Tick(DeltaTime);
54
55 // CurrentLuggage가 유효하고 활성화된 상태라면 회전
57 {
58 FRotator CurrentRotation = CurTarget->GetActorRotation();
59 CurrentRotation.Yaw += RotationSpeed * DeltaTime;
60 CurTarget->SetActorRotation(CurrentRotation);
61 }
62}
63
64void AHolder::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const
65{
66 Super::GetLifetimeReplicatedProps(OutLifetimeProps);
67
68 DOREPLIFETIME(AHolder, bIsActivated);
69 DOREPLIFETIME(AHolder, CurTarget);
70}
71
73{
74 // bIsActivated가 복제될 때 머티리얼 업데이트
76}
77
79{
80 // CurTarget이 복제될 때 클라이언트에서도 충돌 비활성화
81 if (CurTarget)
82 {
83 if (Aluggage* Luggage = Cast<Aluggage>(CurTarget))
84 {
85 Luggage->SetAllCollision(false);
86 PRINTLOG(TEXT("AHolder::OnRep_CurTarget - Disabled collision for Luggage on client"));
87 }
88 }
89}
90
91void AHolder::SetAnswerData(const int32 InAnswerColorIdx, const int32 InAnswerPatternIdx)
92{
93 this->AnswerColorIdx = InAnswerColorIdx;
94 this->AnswerPatternIdx = InAnswerPatternIdx;
95}
96
98 UPrimitiveComponent* OverlappedComponent,
99 AActor* OtherActor,
100 UPrimitiveComponent* OtherComp,
101 int32 OtherBodyIndex,
102 bool bFromSweep,
103 const FHitResult& SweepResult)
104{
105 if (!OtherActor)
106 return;
107
108 // 서버에서만 실행
109 if (!HasAuthority())
110 return;
111
112 if ( bIsActivated )
113 {
114 // 이미 Activate 되었으면 작동 안 함
115 return;
116 }
117
118 // Luggage인지 확인
119 if (auto Luggage = Cast<Aluggage>(OtherActor))
120 {
121 bool bSuccess = CheckLuggage(Luggage);
122
123 // 블루프린트 이벤트 호출
124 OnActivate(bSuccess);
125
126 // NetworkBroadcastActor를 통해 모든 클라이언트에 메시지 브로드캐스트
127 if (auto DM = ANetworkBroadcastActor::Get(this))
128 {
129 DM->SendTutorMessage(FText::FromString(bSuccess ? GameMessage::Holder_Success : GameMessage::Holder_Fail ), this);
130 }
131 }
132}
133
134bool AHolder::CheckLuggage(Aluggage* TargetLuggage)
135{
136 if (!TargetLuggage)
137 return false;
138
139 // ColorIdx와 PatternIdx 비교
140 bool bColorMatch = (AnswerColorIdx == -1) || (TargetLuggage->ColorIdx == AnswerColorIdx);
141 bool bPatternMatch = (AnswerPatternIdx == -1) || (TargetLuggage->PatternIdx == AnswerPatternIdx);
142 bool bSuccess = bColorMatch && bPatternMatch;
143
144 PRINTLOG(TEXT("ColorIdx: %d (Answer: %d), PatternIdx: %d (Answer: %d), Result: %s"),
145 TargetLuggage->ColorIdx, AnswerColorIdx,
146 TargetLuggage->PatternIdx, AnswerPatternIdx,
147 bSuccess ? TEXT("Success") : TEXT("Fail"));
148
149 if (bSuccess)
150 {
151 // Success: Luggage를 HoldPos 위치보다 살짝 위에 배치
152 if (HoldPos)
153 {
154 FVector ActivatedLocation = HoldPos->GetComponentLocation();
155 ActivatedLocation.Z += ActivatedHeightOffset;
156 TargetLuggage->SetActorLocation(ActivatedLocation);
157 TargetLuggage->SetActorRotation(HoldPos->GetComponentRotation());
158 }
159
160 // Luggage의 모든 충돌 비활성화 (pickup, hook 등 모든 상호작용 차단)
161 TargetLuggage->SetAllCollision(false);
162
163 // Activate 상태로 전환
164 bIsActivated = true;
165 CurTarget = TargetLuggage;
166
167 // 서버에서도 머티리얼 업데이트 (클라이언트는 OnRep_IsActivated에서 호출됨)
169 }
170 else
171 {
172 // Fail: Luggage 제거
173 bIsActivated = false;
174 TargetLuggage->Destroy();
175
176 // 서버에서 머티리얼 업데이트 (오답)
177 UpdateActivateState(false);
178 }
179
180 return bSuccess;
181}
182
184{
185 // 머티리얼 파라미터 설정 (비활성화)
186 if (MeshComponent && MeshComponent->GetNumMaterials() > 0)
187 {
188 UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(MeshComponent->GetMaterial(0));
189 if (!DynamicMaterial)
190 DynamicMaterial = MeshComponent->CreateDynamicMaterialInstance(0);
191
192 if (DynamicMaterial)
193 DynamicMaterial->SetScalarParameterValue(FName("Activate"), State ? 1.0f : 0.0f);
194 }
195}
네트워크 복제를 위한 전역 브로드캐스트 Actor
YiSan 전반에서 사용하는 공용 인터페이스를 선언합니다.
#define PRINTLOG(fmt,...)
Definition GameLogging.h:30
void OnBoxOverlapBegin(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
Definition AHolder.cpp:97
void OnRep_CurTarget()
Definition AHolder.cpp:78
virtual void GetLifetimeReplicatedProps(TArray< class FLifetimeProperty > &OutLifetimeProps) const override
Definition AHolder.cpp:64
int32 AnswerPatternIdx
Definition AHolder.h:79
virtual void Tick(float DeltaTime) override
Definition AHolder.cpp:51
virtual void BeginPlay() override
Definition AHolder.cpp:41
int32 AnswerColorIdx
Definition AHolder.h:78
TObjectPtr< class UBoxComponent > BoxCollision
Definition AHolder.h:53
bool CheckLuggage(class Aluggage *TargetLuggage)
Definition AHolder.cpp:134
void UpdateActivateState(bool State)
Definition AHolder.cpp:183
AHolder()
Definition AHolder.cpp:14
float RotationSpeed
Definition AHolder.h:75
void OnRep_IsActivated()
Definition AHolder.cpp:72
float ActivatedHeightOffset
Definition AHolder.h:72
TObjectPtr< class AActor > CurTarget
Definition AHolder.h:64
bool bIsActivated
Definition AHolder.h:68
void OnActivate(bool bSuccess)
void SetAnswerData(const int32 InAnswerColorIdx, const int32 InAnswerPatternIdx)
Definition AHolder.cpp:91
TObjectPtr< class USkeletalMeshComponent > MeshComponent
Definition AHolder.h:56
TObjectPtr< class USceneComponent > HoldPos
Definition AHolder.h:59
static ANetworkBroadcastActor * Get(const UObject *WorldContextObject)
싱글톤 인스턴스 가져오기
상호작용 가능한 수하물 액터
Definition luggage.h:15
int32 PatternIdx
Definition luggage.h:106
int32 ColorIdx
Definition luggage.h:103
void SetAllCollision(bool bEnable)
모든 컴포넌트의 충돌과 물리를 설정합니다.
Definition luggage.cpp:373
static const FString Holder_Fail
Definition Onepiece.h:130
static const FString Holder_Success
Definition Onepiece.h:129