-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAIManager.cpp
More file actions
187 lines (159 loc) · 5.11 KB
/
Copy pathAIManager.cpp
File metadata and controls
187 lines (159 loc) · 5.11 KB
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
#include "AIManager.h"
#include "BehaviorTreeComponent.h"
#include "StateMachineComponent.h"
#include "SceneManager.h"
#include "Scene.h"
#include "MeshRenderer.h"
#include "Mathematics.Intersect.h"
#include <mathematics/transform.hpp>
//#include <execution>
BlackBoard* AIManager::CreateBlackBoard(const std::string& aiName)
{
// 블랙보드가 이미 존재하는지 확인
if (m_blackBoardFind.find(aiName) != m_blackBoardFind.end())
return m_blackBoardFind[aiName];
auto it = m_blackBoards.emplace();
auto* bb = &(*it);
m_blackBoardFind.emplace(aiName, bb);
return bb;
}
void AIManager::RemoveBlackBoard(const std::string& aiName)
{
auto it = m_blackBoardFind.find(aiName);
if (it != m_blackBoardFind.end())
{
std::erase_if(m_blackBoards, [&](const BlackBoard& bb) { return &bb == it->second; });
m_blackBoardFind.erase(it);
}
}
void AIManager::InternalAIUpdate(float deltaSeconds,
const std::optional<math::bounding_frustum>& cameraFrustum)
{
std::vector<IAIComponent*> compVec{};
Scene* activeScene = SceneManagers->GetActiveScene();
if (!activeScene) return;
std::vector<std::pair<EntityHandle, IAIComponent*>> snapshot;
{
std::scoped_lock lock(m_aiComponentMutex);
snapshot.reserve(m_aiComponentMap.size());
for (const auto& entry : m_aiComponentMap)
snapshot.push_back(entry);
}
for (const auto& [handle, comp] : snapshot)
{
Entity* obj = activeScene->Resolve(handle);
if (!obj || !comp) continue;
math::aabb objBox{
math::vector3{}, math::vector3{ 3.f, 3.f, 3.f } };
auto meshComp = obj->GetComponent<MeshRenderer>();
if (meshComp)
{
objBox = meshComp->GetBoundingBox();
}
else
{
objBox = math::transform(
objBox, obj->Transform_().GetWorldMatrix());
}
if (objBox.is_empty() || !cameraFrustum ||
math::intersects(*cameraFrustum, objBox))
{
compVec.push_back(comp);
}
}
auto updateFunc = [deltaSeconds](IAIComponent* comp)
{
try
{
comp->InternalAIUpdate(deltaSeconds);
}
catch (const std::exception& e)
{
std::cerr << "InternalAIUpdate Exception : " << e.what() << std::endl;
}
};
std::ranges::for_each(compVec.begin(), compVec.end(), updateFunc);
}
void AIManager::RegisterAIComponent(Entity* gameObject, IAIComponent* aiComponent)
{
if (!gameObject || !aiComponent)
return;
Scene* scene = gameObject->GetScene();
const EntityHandle handle = scene ? scene->HandleOf(gameObject->m_index) : EntityHandle{};
if (!handle.IsValid()) return;
std::scoped_lock lock(m_aiComponentMutex);
std::erase_if(m_aiComponentMap, [aiComponent](const auto& entry) noexcept
{
return entry.second == aiComponent;
});
m_aiComponentMap.emplace(handle, aiComponent);
}
void AIManager::UnRegisterAIComponent(Entity* gameObject, IAIComponent* aiComponent)
{
if (!gameObject || !aiComponent)
return;
std::scoped_lock lock(m_aiComponentMutex);
std::erase_if(m_aiComponentMap, [aiComponent](const auto& pair) noexcept
{
return nullptr == pair.second || pair.second == aiComponent;
});
}
size_t AIManager::GetRegisteredAIComponentCount() const
{
std::scoped_lock lock(m_aiComponentMutex);
return m_aiComponentMap.size();
}
bool AIManager::IsAIComponentRegistered(const IAIComponent* aiComponent) const
{
if (!aiComponent) return false;
std::scoped_lock lock(m_aiComponentMutex);
return std::ranges::any_of(m_aiComponentMap,
[aiComponent](const auto& entry) { return entry.second == aiComponent; });
}
// AIManager::CreateNode媛 ?ш린 ?덉뿀?? PHASE 9-8 B7?먯꽌 ?쒓굅?덈떎.
// ?몃뱶 ?앹꽦? 愿由?痢?BTNodeFactory媛 ?섍퀬, ?ㅼ씠?곕툕?먮뒗 NodeFactory ?먯껜媛 ?녿떎.
// ?몄텧泥섎룄 ?대? 0?댁뿀??
void AIManager::ClearTreeInAIComponent()
{
std::vector<std::pair<EntityHandle, IAIComponent*>> snapshot;
{
std::scoped_lock lock(m_aiComponentMutex);
for (const auto& entry : m_aiComponentMap) snapshot.push_back(entry);
}
for (const auto& [gameObject, aiComponent] : snapshot)
{
if (!gameObject.IsValid() || !aiComponent)
continue;
if (aiComponent->GetAIType() == AIType::BT)
{
BehaviorTreeComponent* ptr = static_cast<BehaviorTreeComponent*>(aiComponent);
ptr->ClearTree();
}
}
}
void AIManager::InitalizeBehaviorTreeSystem()
{
// Behavior Tree 노드 팩토리 초기화
// ?ㅼ씠?곕툕 ?몃뱶 ?⑺넗由?珥덇린?붽? ?ш린 ?덉뿀?? PHASE 9-8 B7?먯꽌 ?쒓굅?덈떎.
//
// 鍮뚰듃???몃뱶(Sequence쨌Selector쨌WeightedSelector쨌Inverter)??愿由?痢≪쑝濡?
// ?댁떇?먭퀬(B1), ?ъ슜???몃뱶???앹꽦湲곌? 留뚮뱺 ?깅줉?쒓? ?좊떎(B5). ?ㅼ씠?곕툕??
// ?щ낯???④린硫?"?ш린???덈뒗???湲곗뿏 ?녿뒗 ?몃뱶"媛 ?앷릿??
std::vector<std::pair<EntityHandle, IAIComponent*>> snapshot;
{
std::scoped_lock lock(m_aiComponentMutex);
for (const auto& entry : m_aiComponentMap) snapshot.push_back(entry);
}
// 모든 AI 컴포넌트 초기화
for (const auto& [gameObject, aiComponent] : snapshot)
{
if (!gameObject.IsValid() || !aiComponent)
continue;
//지금은 BT 컴포넌트만 초기화
if (aiComponent->GetAIType() == AIType::BT)
{
BehaviorTreeComponent* ptr = static_cast<BehaviorTreeComponent*>(aiComponent);
ptr->GraphToBuild();
}
}
}