-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameManager.cpp
More file actions
343 lines (303 loc) · 9.45 KB
/
Copy pathGameManager.cpp
File metadata and controls
343 lines (303 loc) · 9.45 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "GameManager.h"
#include "World.h"
#include "GameObject.h"
#include "ComponentModule.h"
#include "ImGUImpl.h"
#include "Component/Render/Camera.h"
#include "AssetLoaderFactory.h"
#include "Asset/WorldAsset.h"
#include "Asset/ShaderAsset.h"
#include "Asset/MaterialAsset.h"
#include "Core/Logger.h"
#include "Core/UUID.h"
#include "Core/GarbageCollection.h"
#include "Core/SObjectManager.h"
#include "Core/FileSystem.h"
#include "Core/ModuleLoader.h"
#include "Core/ThreadSyncManager.h"
#include "Render/Renderer.h"
#include "Sound/SoundSystem.h"
#include <algorithm>
namespace sh::game
{
SH_GAME_API void GameManager::Init(render::Renderer& renderer, ImGUImpl& gui)
{
this->renderer = &renderer;
this->gui = &gui;
immortalWorld = core::SObject::Create<World>(renderer, gui);
immortalWorld->SetName("immortalWorld");
core::GarbageCollection::GetInstance()->SetRootSet(immortalWorld);
render::RenderViewer uiViewer{};
uiViewer.projMatrix =
glm::orthoRH_ZO(0.f, 13.66f, 0.0f, 7.68f, 0.01f, 1000.f);
uiViewer.viewMatrix = glm::mat4{ 1.f };
uiViewer.viewportRect = { 0, 0, renderer.GetWidth(), renderer.GetHeight() };
uiViewer.viewportScissor = uiViewer.viewportRect;
guiRenderData.priority = -100;
guiRenderData.renderViewers.push_back(uiViewer);
guiRenderData.tag = core::Name{ "ImGUI" };
}
SH_GAME_API auto GameManager::GetRenderer() const -> render::Renderer&
{
assert(renderer != nullptr);
return *renderer;
}
SH_GAME_API auto GameManager::GetUIContext() const -> ImGUImpl&
{
assert(gui != nullptr);
return *gui;
}
SH_GAME_API void GameManager::Clean()
{
auto gc = core::GarbageCollection::GetInstance();
for (auto& [uuid, world] : worlds)
world->Destroy();
immortalWorld->Destroy();
immortalWorld = nullptr;
core::GarbageCollection::GetInstance()->Collect();
core::GarbageCollection::GetInstance()->DestroyPendingKillObjs();
core::GarbageCollection::GetInstance()->Collect(); // 남아 있는 GCObject들도 제거 해줘야 해서 한번 더 호출
core::GarbageCollection::GetInstance()->DestroyPendingKillObjs();
componentLoader.UnloadPlugin();
worlds.clear();
mainWorld.Reset();
}
SH_GAME_API auto GameManager::GetMainWorld() const -> World*
{
return mainWorld.Get();
}
SH_GAME_API auto GameManager::GetWorld(const core::UUID& uuid) const -> World*
{
auto it = worlds.find(uuid);
if (it == worlds.end())
return nullptr;
return it->second;
}
SH_GAME_API void GameManager::UpdateWorlds(double dt)
{
gui->Begin();
for (auto& [uuid, worldPtr] : worlds)
worldPtr->Update(dt);
immortalWorld->Update(dt);
gui->End();
renderer->PushRenderData(guiRenderData);
UpdateSoundListener();
for (auto& [uuid, worldPtr] : worlds)
worldPtr->BeforeSync();
core::ThreadSyncManager::Sync();
for (auto& [uuid, worldPtr] : worlds)
worldPtr->AfterSync();
while (!afterUpdateTaskQueue.empty())
{
auto& func = afterUpdateTaskQueue.front();
func();
afterUpdateTaskQueue.pop();
}
}
SH_GAME_API void GameManager::LoadWorld(const core::UUID& uuid, LoadMode mode, bool bPlayWorld)
{
auto sobjPtr = core::SObject::GetSObjectUsingResolver(uuid);
if (!core::IsValid(sobjPtr))
return;
if (sobjPtr->GetType().IsChildOf(World::GetStaticType()))
{
if (mode == LoadMode::Single)
loadingSingleWorld = static_cast<World*>(sobjPtr);
else
loadingWorldQueue.push(static_cast<World*>(sobjPtr));
}
if (!bLoadingWorld)
{
afterUpdateTaskQueue.push(
[&, bPlayWorld]()
{
// 불러올 월드가 있다면 불러옴
if (core::IsValid(loadingSingleWorld))
{
for (auto& [uuid, worldPtr] : worlds)
worldPtr->Clear();
worlds.clear();
renderer->Reset();
gui->ClearDrawData();
loadingSingleWorld->SetupRenderer();
if (loadingSingleWorld->GetWorldPoint("default") != nullptr)
{
loadingSingleWorld->LoadWorldPoint();
}
else
{
loadingSingleWorld->InitResource();
}
if (bPlayWorld)
loadingSingleWorld->Play();
loadingSingleWorld->Start();
mainWorld = loadingSingleWorld;
worlds[loadingSingleWorld->GetUUID()] = loadingSingleWorld;
loadingSingleWorld = nullptr;
}
// additive모드로 불러온 월드들
while (!loadingWorldQueue.empty())
{
World* world = loadingWorldQueue.front();
loadingWorldQueue.pop();
core::Json* worldPoint = world->GetWorldPoint();
if (worldPoint != nullptr)
world->Deserialize(*worldPoint); // 즉시 역직렬화 해야하므로 LoadWorldPoint()를 쓰지 않음
if (bPlayWorld)
world->Play();
world->Start();
worlds[world->GetUUID()] = world;
}
core::GarbageCollection::GetInstance()->Collect();
core::ThreadSyncManager::Sync();
core::GarbageCollection::GetInstance()->DestroyPendingKillObjs();
bLoadingWorld = false;
}
);
bLoadingWorld = true;
}
}
SH_GAME_API void GameManager::UnloadWorld(const core::UUID& uuid)
{
auto it = worlds.find(uuid);
if (it == worlds.end())
return;
it->second->Destroy();
worlds.erase(it);
}
SH_GAME_API auto GameManager::LoadGame(const std::filesystem::path& managerPath, core::AssetBundle& bundle) -> bool
{
auto dataOpt = core::FileSystem::LoadBinary(managerPath);
if (!dataOpt.has_value())
{
SH_ERROR_FORMAT("Failed to load gameManager.bin: {}", managerPath.u8string());
return false;
}
const auto json = core::Json::from_bson(dataOpt.value());
if (json.is_discarded())
{
SH_ERROR_FORMAT("Failed to parse gameManager.bin: {}", managerPath.u8string());
return false;
}
if (!json.contains("uuids"))
{
SH_ERROR_FORMAT("Not have a 'uuids' key: {}", managerPath.u8string());
return false;
}
const core::Json& uuidJson = json["uuids"];
for (const auto& worldJson : uuidJson)
{
for (const auto& [worldUUID, uuids] : worldJson.items())
{
for (const auto& uuid : uuids)
worldUUIDs[core::UUID{ worldUUID }].push_back(core::UUID{ uuid.get_ref<const std::string&>()});
}
}
LoadDefaultAsset(bundle);
LoadUserModule("ShellEngineUser");
if (json.contains("starting"))
{
const core::UUID startingWorldUUID{ json["starting"].get_ref<const std::string&>() };
auto worldAsset = bundle.LoadAsset(startingWorldUUID);
if (worldAsset == nullptr)
return false;
auto worldLoader = AssetLoaderFactory::GetInstance()->GetLoader(WorldAsset::ASSET_NAME);
game::World* world = static_cast<game::World*>(worldLoader->Load(*worldAsset));
if (world == nullptr)
return false;
worlds[world->GetUUID()] = world;
world->SetupRenderer();
world->InitResource();
world->LoadWorldPoint();
world->Play();
world->Start();
}
immortalWorld->Play();
immortalWorld->Start();
return true;
}
SH_GAME_API auto GameManager::CreateImmortalObject(std::string_view name) -> GameObject&
{
return *immortalWorld->AddGameObject(name);
}
SH_GAME_API void GameManager::SetImmortalObject(GameObject& obj)
{
if (&obj.world != immortalWorld && !obj.IsPendingKill())
{
auto objPtr = immortalWorld->AddGameObject(obj.GetName().ToString());
(*objPtr) = obj;
obj.Destroy();
}
}
SH_GAME_API void GameManager::ClearImmortalObjects()
{
immortalWorld->Clear();
}
SH_GAME_API void GameManager::AddAterUpdateTask(const std::function<void()>& fn)
{
afterUpdateTaskQueue.push(fn);
}
SH_GAME_API void GameManager::LoadUserModule(const std::filesystem::path& path)
{
componentLoader.LoadPlugin(path);
}
SH_GAME_API void GameManager::StartWorlds()
{
for (auto& [uuid, worldPtr] : worlds)
{
worldPtr->Play();
worldPtr->Start();
}
immortalWorld->Play();
immortalWorld->Start();
}
SH_GAME_API void GameManager::StopWorlds()
{
for (auto& [uuid, worldPtr] : worlds)
worldPtr->Stop();
immortalWorld->Stop();
}
GameManager::~GameManager()
{
Clean();
}
void GameManager::LoadDefaultAsset(core::AssetBundle& bundle)
{
auto assetLoaderFactory = AssetLoaderFactory::GetInstance();
auto shaderLoader = assetLoaderFactory->GetLoader(ShaderAsset::ASSET_NAME);
auto materialLoader = assetLoaderFactory->GetLoader(MaterialAsset::ASSET_NAME);
auto errorShaderAsset = bundle.LoadAsset(core::UUID{ "bbc4ef7ec45dce223297a224f8093f0f" });
if (errorShaderAsset == nullptr)
return;
auto errorShaderPtr = shaderLoader->Load(*errorShaderAsset);
if (errorShaderPtr != nullptr)
defaultAssets.push_back(errorShaderPtr);
auto errorMateralAsset = bundle.LoadAsset(core::UUID{ "bbc4ef7ec45dce223297a224f8093f10" });
if (errorMateralAsset == nullptr)
return;
auto errorMatPtr = materialLoader->Load(*errorMateralAsset);
if (errorMatPtr != nullptr)
defaultAssets.push_back(errorMatPtr);
}
void GameManager::UpdateSoundListener()
{
if (mainWorld.IsValid())
{
const Camera* const mainCameraPtr = mainWorld->GetMainCamera();
if (core::IsValid(mainCameraPtr))
{
auto& soundSystem = *sound::SoundSystem::GetInstance();
const auto& worldPos = mainCameraPtr->gameObject.transform->GetWorldPosition();
const glm::vec3 forward = glm::normalize(glm::vec3{ mainCameraPtr->GetLookPos() - worldPos });
glm::vec3 up{ 0.f, 1.f, 0.f };
if (std::abs(glm::dot(forward, up)) > 0.999f)
up = { 1.f, 0.f, 0.f };
const glm::vec3 right = glm::normalize(glm::cross(forward, up));
up = glm::cross(right, forward);
soundSystem.SetListenerPosition(worldPos.x, worldPos.y, worldPos.z);
soundSystem.SetListenerOrientation({ forward.x, forward.y, forward.z }, { up.x, up.y, up.z });
return;
}
}
}
}//namespace