-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoPickSystem.cpp
204 lines (167 loc) · 6.15 KB
/
AutoPickSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "pch.h"
#include "AutoPickSystem.h"
#include "VisPredComponents.h"
#include "EventSubscriber.h"
#include <limits>
#include <cmath>
#include <algorithm>
AutoPickSystem::AutoPickSystem(std::shared_ptr<SceneManager> scenemnager) :System(scenemnager)
{
EventManager::GetInstance().Subscribe("OnAutoPickup", CreateSubscriber(&AutoPickSystem::OnAutoPickup));
}
void AutoPickSystem::EnterTrigger(std::shared_ptr<Entity> first, std::shared_ptr<Entity> second)
{
if (first->HasComponent<AutoPickComponent>() && second->HasComponent<GunComponent>())
AddPickup(first->GetComponent<AutoPickComponent>(), second->GetComponent<GunComponent>());
else if (first->HasComponent<GunComponent>() && second->HasComponent<AutoPickComponent>())
AddPickup(second->GetComponent<AutoPickComponent>(), first->GetComponent<GunComponent>());
}
void AutoPickSystem::ExitTrigger(std::shared_ptr<Entity> first, std::shared_ptr<Entity> second)
{
if (first->HasComponent<AutoPickComponent>() && second->HasComponent<GunComponent>())
ReleasePickup(first->GetComponent<AutoPickComponent>(), second->GetComponent<GunComponent>());
else if (first->HasComponent<GunComponent>() && second->HasComponent<AutoPickComponent>())
ReleasePickup(second->GetComponent<AutoPickComponent>(), first->GetComponent<GunComponent>());
}
void AutoPickSystem::AddPickup(AutoPickComponent* autopick, GunComponent* pickuped)
{
if (pickuped->IsEmpty)
return;
// ID가 이미 PickUps 리스트에 있는지 확인
uint32_t entityID = pickuped->GetEntityID();
if (std::find(autopick->PickUps.begin(), autopick->PickUps.end(), entityID) == autopick->PickUps.end())
{
autopick->PickUps.push_back(entityID);
}
}
void AutoPickSystem::ReleasePickup(AutoPickComponent* autopick, GunComponent* pickuped)
{
auto it = std::find(autopick->PickUps.begin(), autopick->PickUps.end(), pickuped->GetEntityID());
if (it != autopick->PickUps.end())
{
autopick->PickUps.erase(it);
}
}
void AutoPickSystem::OnAutoPickup(std::any pcomp)
{
AutoPickComponent* autopick = std::any_cast<AutoPickComponent*>(pcomp);
AutoPickUp(autopick);
}
void AutoPickSystem::FixedUpdate(float deltaTime)
{
COMPLOOP(AutoPickComponent, pickupcomp)
{
if (pickupcomp.ParentPlayer
&& pickupcomp.IsAuto
&& !pickupcomp.ParentPlayer->HasGun
&& !pickupcomp.ParentPlayer->IsVPMode
&& (pickupcomp.ParentPlayer->HandEntity.lock()->GetComponent<AnimationComponent>()->curAni == static_cast<int>(VisPred::Game::PlayerAni::ToIdle02_Sword)
|| pickupcomp.ParentPlayer->HandEntity.lock()->GetComponent<AnimationComponent>()->curAni == static_cast<int>(VisPred::Game::PlayerAni::ToIdle01_Sword)
|| pickupcomp.ParentPlayer->HandEntity.lock()->GetComponent<AnimationComponent>()->curAni == static_cast<int>(VisPred::Game::PlayerAni::ToAttack1_Sword)
|| pickupcomp.ParentPlayer->HandEntity.lock()->GetComponent<AnimationComponent>()->curAni == static_cast<int>(VisPred::Game::PlayerAni::ToAttack2_Sword)
|| pickupcomp.ParentPlayer->HandEntity.lock()->GetComponent<AnimationComponent>()->curAni == static_cast<int>(VisPred::Game::PlayerAni::ToAttack3_Sword) )
&& !pickupcomp.ParentPlayer->HandEntity.lock()->GetComponent<AnimationComponent>()->IsBlending)
{
pickupcomp.ParentPlayer->IsAttacking = false;
AutoPickUp(&pickupcomp);
}
}
}
void AutoPickSystem::Update(float deltaTime)
{
COMPLOOP(AutoPickComponent, pickupcomp)
{
if (INPUTKEYDOWN(KEYBOARDKEY::F1))
{
pickupcomp.IsAuto = !pickupcomp.IsAuto;
}
}
}
void AutoPickSystem::Initialize()
{
COMPLOOP(AutoPickComponent, autopick)
{
Start(autopick.GetEntityID());
}
}
void AutoPickSystem::Start(uint32_t gameObjectId)
{
if (!GetSceneManager()->HasComponent<AutoPickComponent>(gameObjectId))
return;
auto pickup = GetSceneManager()->GetComponent<AutoPickComponent>(gameObjectId);
pickup->ParentPlayer = GetSceneManager()->GetParentEntityComp_HasComp<PlayerComponent>(gameObjectId);
}
void AutoPickSystem::Finish(uint32_t gameObjectId)
{
}
void AutoPickSystem::Finalize()
{
}
void AutoPickSystem::AutoPickUp(AutoPickComponent* autopick)
{
if (autopick->PickUps.empty())
{
autopick->ParentPlayer->LongswordEntity.lock().get()->GetComponent<MeshComponent>()->IsVisible = true;
return;
}
// PickUps 리스트에서 존재하지 않는 엔티티를 제거
RemoveInvalidEntities(autopick);
// 거리 계산 및 가장 가까운 유효한 엔티티 찾기
auto closestEntity = FindClosestValidEntity(autopick);
if (closestEntity != 0)
EventManager::GetInstance().ImmediateEvent("OnInterective", std::make_pair(autopick->ParentPlayer->GetEntityID(), closestEntity));
else
autopick->ParentPlayer->LongswordEntity.lock().get()->GetComponent<MeshComponent>()->IsVisible = true;
}
void AutoPickSystem::RemoveInvalidEntities(AutoPickComponent* autopick)
{
for (auto it = autopick->PickUps.begin(); it != autopick->PickUps.end(); )
{
auto entity = GetSceneManager()->GetEntity(*it);
if (!entity)
{
it = autopick->PickUps.erase(it);
}
else if (entity->HasComponent<GunComponent>() && entity->GetComponent<GunComponent>()->IsEmpty)
{
it = autopick->PickUps.erase(it);
}
else
{
++it;
}
}
}
bool AutoPickSystem::IsValidPickupEntity(uint32_t entityID)
{
auto entity = GetSceneManager()->GetEntity(entityID);
if (!entity || !m_PhysicsEngine->IsDynamic(entityID))
return false;
auto guncomp = entity->GetComponent<GunComponent>();
if ((guncomp && !guncomp->IsEmpty))
return true;
return false;
}
uint32_t AutoPickSystem::FindClosestValidEntity(AutoPickComponent* autopick) {
auto playerLocation = autopick->ParentPlayer->GetComponent<TransformComponent>()->World_Location;
std::vector<std::pair<float, uint32_t>> distanceEntities;
// 거리 계산 및 벡터에 추가
for (auto gunid : autopick->PickUps)
{
auto gunEntity = GetSceneManager()->GetEntity(gunid);
if (gunEntity) {
float distance = (playerLocation - gunEntity->GetComponent<TransformComponent>()->World_Location).Length();
distanceEntities.emplace_back(distance, gunid);
}
}
// 거리순으로 정렬
std::sort(distanceEntities.begin(), distanceEntities.end());
// 유효한 가장 가까운 엔티티 찾기
for (const auto& [distance, entityID] : distanceEntities) {
if (IsValidPickupEntity(entityID))
{
return entityID;
}
}
return 0; // 유효한 엔티티가 없으면 0을 반환
}