CSEngine
Loading...
Searching...
No Matches
GameObjectMgr.cpp
1#include <algorithm>
2#include "GameObjectMgr.h"
3#include "MemoryMgr.h"
4#include "EngineCore.h"
5
6using namespace CSE;
7
8
9GameObjectMgr::GameObjectMgr() = default;
10
11
12GameObjectMgr::~GameObjectMgr() = default;
13
14
15void GameObjectMgr::Init() {
16
17 for (const auto& pair : m_objects) {
18 const auto& object = pair.second;
19 if (object == nullptr) continue;
20 object->Init();
21 }
22
23}
24
25
26void GameObjectMgr::Update(float elapsedTime) {
27 auto iterator = m_objects.begin();
28 while (iterator != m_objects.end()) {
29 const auto& object = iterator->second;
30 if (object == nullptr) continue;
31 object->Tick(elapsedTime);
32 ++iterator;
33 }
34}
35
36
37void GameObjectMgr::DestroyQueuedObject() {
38 if (m_destroyObjectsQueue.empty()) return;
39
40 for (; !m_destroyObjectsQueue.empty(); m_destroyObjectsQueue.pop()) {
41 const auto& object = m_destroyObjectsQueue.front();
42
44 CORE->GetCore(MemoryMgr)->ReleaseObject(object);
45 }
46}
47
48void GameObjectMgr::AddDestroyObject(SGameObject* object) {
49 if(object->GetStatus() != SGameObject::DESTROY) return;
50 m_destroyObjectsQueue.push(object);
51}
52
53SGameObject* GameObjectMgr::Find(const std::string& name) const {
54 for (const auto& pair : m_objects) {
55 const auto& object = pair.second;
56 if (object->GetName() == name)
57 return object;
58 }
59
60 return nullptr;
61
62}
63
64SGameObject* GameObjectMgr::FindByID(const std::string& id) const {
65 std::string obj_id = split(id, '?')[0];
66
67 for (const auto& pair : m_objects) {
68 const auto& object = pair.second;
69 if (object->isPrefab()) continue;
70 auto id_ = object->GetID();
71 if (id_ == obj_id)
72 return object;
73 }
74
75 return nullptr;
76}
77
78SGameObject* GameObjectMgr::FindByHash(const std::string& hash) const {
79 std::string obj_hash = split(hash, '?')[0];
80 return SContainerHash<SGameObject*>::Get(obj_hash);
81}
82
83SComponent* GameObjectMgr::FindComponentByID(const std::string& id) const {
84 auto object = FindByID(id);
85 if (object == nullptr) return nullptr;
86
87 auto components = object->GetComponents();
88 auto split_str = split(id, '?');
89
90 for (auto component : components) {
91 if (split_str[1] == component->GetClassType()) {
92 return component;
93 }
94 }
95 return nullptr;
96}
97
98SComponent* GameObjectMgr::FindComponentByHash(const std::string& hash) const {
99 auto object = FindByHash(hash);
100 if (object == nullptr) return nullptr;
101
102 auto components = object->GetComponents();
103 auto split_str = split(hash, '?');
104
105 for (auto component : components) {
106 if (split_str[1] == component->GetClassType()) {
107 return component;
108 }
109 }
110 return nullptr;
111}