5#include <entt/entt.hpp>
7#include <glm/mat4x4.hpp>
8#include <glm/gtx/matrix_decompose.hpp>
20 uint32_t GameObject::s_goIdCount = 0;
23 : m_name(name), m_scene(scene.get()), m_parent(parent)
31 glm::mat4 newLocal = m_parent ? glm::inverse(m_parent->GenGlobalMatrix()) * newWorld : newWorld;
34 glm::quat rotationQuat;
37 glm::vec4 perspective;
39 glm::decompose(newLocal, scale, rotationQuat, position, skew, perspective);
40 glm::vec3 rotationEuler = glm::degrees(glm::eulerAngles(rotationQuat));
50 return glm::vec3(global[3]);
64 for (std::shared_ptr<GameObject> child : m_childs)
81 for (std::shared_ptr<GameObject> child : m_childs)
97 for (std::shared_ptr<GameObject> child : m_childs)
112 if (comp->IsActive())
114 comp->OnSetPosition();
118 for (std::shared_ptr<GameObject> child : m_childs)
120 child->OnSetPosition();
128 if (comp->IsActive())
130 comp->OnSetRotation();
134 for (std::shared_ptr<GameObject> child : m_childs)
136 child->OnSetRotation();
144 if (comp->IsActive())
150 for (std::shared_ptr<GameObject> child : m_childs)
156 void GameObject::Initialize()
162 m_parent->AddChild(shared_from_this());
166 m_scene->AddRootObject(shared_from_this());
170 auto registryIt = registryMap.find(m_scene->m_registryId);
172 if (registryIt == registryMap.end()) {
173 EE_CORE_ERROR(
"Registry not found for scene '%s' with id: %d",
174 m_scene->
GetName(), m_scene->m_registryId);
177 if (!registryIt->second) {
178 EE_CORE_ERROR(
"Registry pointer is null for scene '%s' with id: %d",
179 m_scene->
GetName(), m_scene->m_registryId);
182 entt::entity entity = registryIt->second->create();
183 m_entityId =
static_cast<std::uint32_t
>(entity);
186 m_isInitialized =
true;
190 EE_CORE_ERROR(
"Object '%s' must be linked with an existing scene!", m_name);
222 std::vector<Component*> components;
223 if (!m_scene)
return components;
226 if (
Component* component = entry.getter(weak_from_this())) {
227 components.push_back(component);
239 if (comp->IsActive())
241 comp->RenderInEditor();
245 for (std::shared_ptr<GameObject> child : m_childs)
247 child->RenderInEditor();
256 if (comp->IsActive())
258 comp->RenderWhenSelected();
262 for (std::shared_ptr<GameObject> child : m_childs)
264 child->RenderWhenSelected();
270 if (newParent == m_parent)
275 m_parent->RemoveChild(shared_from_this());
278 this->m_parent = newParent;
282 newParent->AddChild(shared_from_this());
285 m_scene->RemoveFromRoot(shared_from_this());
289 m_scene->AddRootObject(shared_from_this());
296 m_parent->RemoveChild(shared_from_this());
299 m_scene->RemoveFromRoot(shared_from_this());
302 auto childsCopy = m_childs;
303 for (
const auto& child : childsCopy)
318 child->m_parent = shared_from_this();
319 m_childs.emplace(child);
320 m_scene->RemoveFromRoot(child);
326 m_childs.erase(child);
329 std::shared_ptr<GameObject>
GameObject::Create(std::string name, std::shared_ptr<Scene> scene, std::shared_ptr<GameObject> parent)
331 std::shared_ptr<GameObject> obj = std::make_shared<GameObject>(name, scene, parent);
333 obj->SetPosition({ 0.0f, 0.0f, 0.0f });
#define EE_INVALID_ENTITY_ID
static std::unordered_map< std::type_index, Entry > & GetEntries()
static std::shared_ptr< GameObject > Create(std::string name, std::shared_ptr< Scene > scene, std::shared_ptr< GameObject > parent=nullptr)
void OnSetRotation() override
void Notify(Event &event)
void RemoveChild(std::shared_ptr< GameObject > child)
glm::mat4 GenGlobalMatrix() const
void RenderWhenSelected()
void OnSetPosition() override
glm::vec3 GetGlobalPosition()
void OnSetScale() override
void SetParent(std::shared_ptr< GameObject > newParent)
void SetFromGlobalMatrix(const glm::mat4 &newWorld)
void AddChild(std::shared_ptr< GameObject > child)
GameObject(std::string name, std::shared_ptr< Scene > scene, std::shared_ptr< GameObject > parent=nullptr)
std::vector< Component * > GetComponents()
const std::string & GetName() const
static void UpdatePosition(GameObject *obj)
static void UnregisterGameObject(GameObject *obj)
static void RegisterGameObject(GameObject *obj)
std::unordered_map< uint32_t, std::unique_ptr< entt::registry > > & GetRegistryMap()