KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
Food.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
4#include "Food.h"
5
6#include "CityNameWidget.h"
7#include "FListenData.h"
9#include "Components/WidgetComponent.h"
10#include "Net/UnrealNetwork.h"
11
12
13// Sets default values
15{
16 // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
17 PrimaryActorTick.bCanEverTick = true;
18
19 Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
20 SetRootComponent(Mesh);
21
22 FoodMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FoodMesh"));
23 FoodMesh->SetupAttachment(GetRootComponent());
24
25 CityName = CreateDefaultSubobject<UWidgetComponent>(TEXT("FoodName"));
26
27 InteractableComp = CreateDefaultSubobject<UInteractableComponent>(TEXT("Interactable"));
28 InteractableComp->InteractionType = EInteractionType::PickUp;
29 InteractableComp->InteractionPrompt = TEXT("Pick Up");
30
31 // Initial settings (Physics 설정은 BeginPlay에서 수행)
32 Mesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
33 Mesh->SetCollisionProfileName(TEXT("PhysicsActor"));
34
35 // 물리 복제 설정
36 Mesh->SetIsReplicated(true);
37
38 // Replication
39 bReplicates = true;
40 SetNetUpdateFrequency(100.0f); // 높은 업데이트 빈도로 부드러운 네트워크 동기화
41 SetMinNetUpdateFrequency(33.0f); // 최소 30fps 업데이트 보장
42
43}
44
45// Called when the game starts or when spawned
47{
48 Super::BeginPlay();
49
50 // Physics 설정 (생성자에서 이동)
51 if (Mesh)
52 {
53 Mesh->SetSimulatePhysics(true);
54 Mesh->SetEnableGravity(true);
55 Mesh->SetMassOverrideInKg(NAME_None, 50.f, true);
56 }
57
58 CityName->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
59 CityName->SetRelativeLocation(FVector(0, 0, 0));
60
61 // 델리게이트 바인딩
63 {
64 InteractableComp->OnOutlineStateChanged.AddDynamic(this, &AFood::OnOutlineStateChanged);
65 }
66}
67
68void AFood::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const
69{
70 Super::GetLifetimeReplicatedProps(OutLifetimeProps);
71
72 // FoodMesh는 Component이므로 자동 복제됨
73 DOREPLIFETIME(AFood, FoodMesh);
74 DOREPLIFETIME(AFood, CurrentFoodData);
75}
76
77// Called every frame
78void AFood::Tick(float DeltaTime)
79{
80 Super::Tick(DeltaTime);
81
82}
83
85{
86 CurrentFoodData.word1 = InWord;
87 // 서버에서도 Widget 업데이트 (클라이언트는 OnRep_FoodName에서 호출됨)
88 if (HasAuthority())
89 {
91 }
92}
93
99
101{
102 // 게임 스레드가 아니면 게임 스레드로 dispatch
103 if (!IsInGameThread())
104 {
105 AsyncTask(ENamedThreads::GameThread, [this]()
106 {
108 });
109 return;
110 }
111
112 // Widget이 아직 초기화되지 않았을 수 있으므로 체크
113 if (!CityName || !CityName->GetWidget())
114 return;
115
116 UCityNameWidget* NameWidget = Cast<UCityNameWidget>(CityName->GetWidget());
117 if (NameWidget)
118 {
119 NameWidget->SetCityName(FString::Printf(TEXT("%s %s"), *CurrentFoodData.word1.name, *CurrentFoodData.word2.name));
120 }
121}
122
124{
125 // 게임 스레드가 아니면 게임 스레드로 dispatch
126 if (!IsInGameThread())
127 {
128 AsyncTask(ENamedThreads::GameThread, [this]()
129 {
130 UpdateMesh();
131 });
132 return;
133 }
134
135 if (!ListenDataTable)
136 {
137 UE_LOG(LogTemp, Error, TEXT("[AFood::UpdateMesh] ListenDataTable is null!"));
138 return;
139 }
140
141 if (!FoodMesh)
142 {
143 UE_LOG(LogTemp, Error, TEXT("[AFood::UpdateMesh] FoodMesh component is null!"));
144 return;
145 }
146
147 TArray<FListenData*> AllRows;
148 ListenDataTable->GetAllRows<FListenData>(TEXT("UpdateMesh"), AllRows);
149
150 for (FListenData* Row : AllRows)
151 {
152 if (Row && Row->Word == CurrentFoodData.word2.name)
153 {
154 if (Row->FoodPath)
155 {
156 FoodMesh->SetStaticMesh(Row->FoodPath);
157 return;
158 }
159 }
160 }
161
162 // 일치하는 데이터를 찾지 못함
163 UE_LOG(LogTemp, Warning, TEXT("[AFood::UpdateMesh] No matching ListenData found for: %s"),
165}
166
167void AFood::SetFoodMesh(FWordInfo InWord, UStaticMesh* InMesh)
168{
169 CurrentFoodData.word2 = InWord;
170
171 // 서버에서도 UpdateMesh 호출 (클라이언트는 OnRep_CurrentFoodData에서 호출됨)
172 if (HasAuthority())
173 {
174 UpdateMesh();
175 }
176}
177
178void AFood::OnOutlineStateChanged(bool bShouldShowOutline)
179{
180 if (Mesh)
181 {
182 Mesh->SetRenderCustomDepth(bShouldShowOutline);
183 }
184 if (FoodMesh)
185 {
186 FoodMesh->SetRenderCustomDepth(bShouldShowOutline);
187 }
188}
189
190
191// HACK, 해줘요. 모델링 찾았고, ListenData에서 모델링도 빼오는데, AddChildActor 까지도 되는데.
192// 객체 생성쪽에서 뭔가 이상한데. 디버깅을 다 못했음
193// void AFood::SetFoodInfo(int32 InIndex, FString InName)
194// {
195// Name = InName;
196// Index = InIndex;
197//
198// UCityNameWidget* NameWidget = Cast<UCityNameWidget>(FoodName->GetWidget());
199// if (NameWidget)
200// {
201// NameWidget->SetCityName(InName);
202// }
203// }
204
205// void AFood::SetFoodInfo(const FFoodData& InFoodData)
206// {
207// Name = FString::Printf(TEXT("%s / %s"), *InFoodData.word1.name, *InFoodData.word2.name);
208// Index = InFoodData.SpawnIndex;
209//
210// UCityNameWidget* NameWidget = Cast<UCityNameWidget>(FoodName->GetWidget());
211// if (NameWidget)
212// {
213// NameWidget->SetCityName(Name);
214// }
215//
216// int32 PatternIdx = FCString::Atoi(*InFoodData.word2.code);
217// FListenData Listen02Data;
218// if (UGameDataManager::Get(GetWorld())->GetListenData(PatternIdx, Listen02Data))
219// {
220// SetFoodModel(Listen02Data.FoodPath);
221// }
222// }
FListenData 구조체를 선언합니다.
Definition Food.h:37
TObjectPtr< class UInteractableComponent > InteractableComp
Definition Food.h:65
void UpdateFoodWidget()
Widget에 음식 이름 업데이트
Definition Food.cpp:100
class UStaticMeshComponent * Mesh
Definition Food.h:56
void SetFoodMesh(FWordInfo InWord, UStaticMesh *InMesh)
Definition Food.cpp:167
void UpdateMesh()
음식 메시 업데이트 (DataTable에서 로드)
Definition Food.cpp:123
class UWidgetComponent * CityName
Definition Food.h:62
virtual void GetLifetimeReplicatedProps(TArray< class FLifetimeProperty > &OutLifetimeProps) const override
Definition Food.cpp:68
void OnOutlineStateChanged(bool bShouldShowOutline)
Definition Food.cpp:178
virtual void Tick(float DeltaTime) override
Definition Food.cpp:78
void OnRep_CurrentFoodData()
Definition Food.cpp:94
AFood()
Definition Food.cpp:14
class UStaticMeshComponent * FoodMesh
Definition Food.h:59
FFoodCapsuleData CurrentFoodData
Definition Food.h:77
virtual void BeginPlay() override
Definition Food.cpp:46
void SetCityName(FWordInfo InWord)
Definition Food.cpp:84
class UDataTable * ListenDataTable
Definition Food.h:73
void SetCityName(FString InCityName)
FWordInfo word1
시나리오 단어 정보
Definition Food.h:22
FWordInfo word2
Definition Food.h:25
듣기 학습 데이터를 정의하는 구조체
Definition FListenData.h:19
단어 정보 구조체
FString name