Elevate Engine 1
Loading...
Searching...
No Matches
GameObject.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <set>
5#include <memory>
6#include <string>
7#include <stdint.h>
8
9#include <glm/ext/matrix_float4x4.hpp>
10#include <glm/ext/vector_float3.hpp>
11
13
14#define EE_INVALID_ENTITY_ID UINT32_MAX
15
16namespace Elevate
17{
18 class Event;
19 class Scene;
20 class Component;
21 class ComponentRegistry;
22
23 namespace Editor
24 {
25 class EditorLayer;
26 }
27
28 class GameObject : public ITransformable, public std::enable_shared_from_this<GameObject>
29 {
30 public:
31 GameObject(std::string name, std::shared_ptr<Scene> scene, std::shared_ptr<GameObject> parent = nullptr);
33
34 template<typename T, typename... Args>
35 T& AddComponent(Args&&... args);
36
37 template <typename T>
38 T* GetComponent(bool onlyReturnActive = false);
39
40 std::vector<Component*> GetComponents();
41
42 template <typename T>
43 bool HasComponent();
44
45 template <typename T>
46 void RemoveComponent();
47
48 inline std::string& GetName() { return m_name; }
49 inline void SetName(std::string newName) { m_name = newName; }
50
51 void SetParent(std::shared_ptr<GameObject> newParent);
52 void Destroy();
53
54 void RemoveChild(std::shared_ptr<GameObject> child);
55
56 inline uint32_t GetEntityId() { return m_entityId; }
57 inline uint32_t GetObjectId() { return m_goId; }
58
59 inline const bool HasChild() const { return m_childs.size() > 0; }
60 inline std::set<std::shared_ptr<GameObject>> GetChilds() const { return m_childs; }
61
62 static std::shared_ptr<GameObject> Create(std::string name, std::shared_ptr<Scene> scene, std::shared_ptr<GameObject> parent = nullptr);
63
64 Scene* GetScene() { return m_scene; }
65
66 glm::mat4 GenGlobalMatrix() const;
67 void SetFromGlobalMatrix(const glm::mat4& newWorld);
68 glm::vec3 GetGlobalPosition();
69
70 protected:
71 void Update();
72 void Render();
73 void Notify(Event& event);
74
75 // ITransformable
76 void OnSetPosition() override;
77 void OnSetRotation() override;
78 void OnSetScale() override;
79
80 // Editor Rendering
81 void RenderInEditor();
82 void RenderWhenSelected();
83
84 // This method is protected as the main entry point to modify the parent should be SetParent()
85 void AddChild(std::shared_ptr<GameObject> child);
86
87 private:
88 void Initialize(); // Internal function to use just after constructor
89
90 private:
91 std::string m_name;
92
93 // Parent and Child
94 std::shared_ptr<GameObject> m_parent;
95 std::set<std::shared_ptr<GameObject>> m_childs;
96
97 // The entt id
98 uint32_t m_entityId = EE_INVALID_ENTITY_ID; // Invalid up until the initialization
99 // The gameObject static count
100 uint32_t m_goId = EE_INVALID_ENTITY_ID; // Invalid up until the initialization
101 static uint32_t s_goIdCount;
102 bool m_isInitialized = false;
103
104 Scene* m_scene;
105
106 friend class Scene;
107 friend class ComponentRegistry;
109 };
110}
111
112#include "GameObject.inl"
#define EE_INVALID_ENTITY_ID
Definition GameObject.h:14
static std::shared_ptr< GameObject > Create(std::string name, std::shared_ptr< Scene > scene, std::shared_ptr< GameObject > parent=nullptr)
uint32_t GetEntityId()
Definition GameObject.h:56
void OnSetRotation() override
T & AddComponent(Args &&... args)
uint32_t GetObjectId()
Definition GameObject.h:57
void Notify(Event &event)
void RemoveChild(std::shared_ptr< GameObject > child)
T * GetComponent(bool onlyReturnActive=false)
void SetName(std::string newName)
Definition GameObject.h:49
glm::mat4 GenGlobalMatrix() const
void OnSetPosition() override
glm::vec3 GetGlobalPosition()
void OnSetScale() override
void SetParent(std::shared_ptr< GameObject > newParent)
void SetFromGlobalMatrix(const glm::mat4 &newWorld)
const bool HasChild() const
Definition GameObject.h:59
friend class Editor::EditorLayer
Definition GameObject.h:108
void AddChild(std::shared_ptr< GameObject > child)
std::vector< Component * > GetComponents()
std::set< std::shared_ptr< GameObject > > GetChilds() const
Definition GameObject.h:60
std::string & GetName()
Definition GameObject.h:48
An interface to add all of the methods needed to have a working transform wrapped by getter and sette...