Hymn to Beauty
C++ 3D Engine
Entity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <vector>
5#include <typeindex>
6#include <json/json.h>
7#include <glm/gtc/quaternion.hpp>
8#define GLM_ENABLE_EXPERIMENTAL
9#include <glm/gtx/quaternion.hpp>
10#include "../Component/SuperComponent.hpp"
11#include <fstream>
12
13class World;
14
16class Entity {
17 public:
19
23 Entity(World* world, const std::string& name);
24
26 ~Entity();
27
29
32 Entity* GetParent() const;
33
35
39 Entity* AddChild(const std::string& name = "");
40
42
46 bool RemoveChild(Entity* child);
47
49
53 Entity* SetParent(Entity* newParent);
54
56
61 bool HasChild(const Entity* child, bool deep = true) const;
62
64
68 Entity* InstantiateScene(const std::string& name);
69
71
76 Entity* InstantiateScene(const std::string& name, const std::string& originScene);
77
79
85 void CheckIfSceneExists(const std::string& filename, bool& error, const std::string& originScene, Json::Value root);
86
88
91 const std::vector<Entity*>& GetChildren() const;
92
94
98 Entity* GetChild(const std::string& name) const;
99
101
105 Entity* GetChildFromIndex(int index) const;
106
108
111 unsigned int GetNumChildren() const;
112
114
117 bool IsScene() const;
118
120
123 template <typename T> T* AddComponent();
124
126
129 template <typename T> T* GetComponent() const;
130
132 template <typename T> void KillComponent();
133
135 void Kill();
136
138
141 bool IsKilled() const;
142
144
148 void Serialize(Json::Value& node, bool load);
149
151
154 const glm::vec3& GetPosition() const;
155
157
160 void SetPosition(const glm::vec3& position);
161
163
166 glm::vec3 GetWorldPosition() const;
167
169
172 void SetWorldPosition(const glm::vec3& worldPos);
173
175
178 void Move(const glm::vec3& translation);
179
181
184 const glm::vec3& GetScale() const;
185
187
190 void SetScale(const glm::vec3& scale);
191
193
196 const glm::quat& GetRotation() const;
197
199
202 void SetRotation(const glm::quat& localRot);
203
205
208 glm::quat GetWorldRotation() const;
209
211
214 void SetWorldRotation(const glm::quat& worldRot);
215
217
220 glm::vec3 GetWorldDirection() const;
221
223
226 void RotateYaw(float angle);
227
229
232 void RotatePitch(float angle);
233
235
238 void RotateRoll(float angle);
239
241
245 void RotateAroundWorldAxis(float angle, const glm::vec3& axis);
246
248
251 const glm::mat4& GetLocalMatrix() const;
252
254
257 const glm::mat4& GetWorldModelMatrix() const;
258
260
264 void SetEnabled(bool enabled, bool recursive = false);
265
267
270 bool IsEnabled() const;
271
273 std::string name;
274
276
279 unsigned int GetUniqueIdentifier() const;
280
282
285 void SetUniqueIdentifier(unsigned int UID);
286
289 bool brushActive = false;
290 bool vertsLoaded = false;
291 bool painting = false;
292 bool sceneChosen = false;
293
294 private:
295 enum DirtyFlag {
296 LOCAL_MATRIX = 1,
297 WORLD_MATRIX = 2
298 };
299
300 template <typename T> void Serialize(Json::Value& node, bool load, const std::string& name);
301 Component::SuperComponent* AddComponent(std::type_index componentType);
302 Component::SuperComponent* GetComponent(std::type_index componentType) const;
303 void KillComponent(std::type_index componentType);
304 void KillHelper();
305 void SetDirty(uint32_t dirtyMask = DirtyFlag::LOCAL_MATRIX | DirtyFlag::WORLD_MATRIX);
306
307 World* world;
308 Entity* parent = nullptr;
309 std::vector<Entity*> children;
310 bool scene = false;
311 std::string sceneName;
312
313 std::map<std::type_index, Component::SuperComponent*> components;
314
315 glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
316 glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
317 glm::quat rotation = glm::angleAxis(0.0f, glm::vec3(0.0f, 1.0f, 0.0f));
318
319 bool killed = false;
320 bool enabled = true;
321 unsigned int uniqueIdentifier = 0;
322
323 // Cache matrices.
324 mutable glm::mat4 localMatrix;
325 mutable glm::mat4 worldMatrix;
326 mutable uint32_t dirtyMask = DirtyFlag::LOCAL_MATRIX | DirtyFlag::WORLD_MATRIX;
327};
328
329template <typename T> T* Entity::AddComponent() {
330 std::type_index componentType = std::type_index(typeid(T*));
331 return static_cast<T*>(AddComponent(componentType));
332}
333
334template <typename T> T* Entity::GetComponent() const {
335 return static_cast<T*>(GetComponent(std::type_index(typeid(T*))));
336}
337
338template <typename T> void Entity::KillComponent() {
339 KillComponent(typeid(T*));
340}
341
342template <typename T> void Entity::Serialize(Json::Value& node, bool load, const std::string& name) {
343 std::type_index componentType = std::type_index(typeid(T*));
344 Component::SuperComponent* component = nullptr;
345 if (load) {
346 if (!node[name].isNull()) {
347 component = AddComponent(componentType);
348 }
349 } else {
350 component = GetComponent(componentType);
351 }
352
353 if (component != nullptr) {
354 component->Serialize(node[name], load);
355 }
356}
Component which all components inherit.
Definition: SuperComponent.hpp:9
virtual void Serialize(Json::Value &node, bool load)=0
Save or load component values to/from JSON.
Entity containing various components.
Definition: Entity.hpp:16
const std::vector< Entity * > & GetChildren() const
Get all of the entity's children.
Definition: Entity.cpp:135
void RotatePitch(float angle)
Rotates around the X axis.
Definition: Entity.cpp:323
const glm::mat4 & GetWorldModelMatrix() const
Get the world model matrix.
Definition: Entity.cpp:355
glm::vec3 GetWorldPosition() const
Get the position in the world.
Definition: Entity.cpp:256
const glm::mat4 & GetLocalMatrix() const
Get the local model matrix.
Definition: Entity.cpp:337
bool vertsLoaded
Definition: Entity.hpp:290
void RotateYaw(float angle)
Rotates around the Y axis.
Definition: Entity.cpp:319
const glm::vec3 & GetPosition() const
Get the local position.
Definition: Entity.cpp:247
void CheckIfSceneExists(const std::string &filename, bool &error, const std::string &originScene, Json::Value root)
Check if scene already exists in any of json files.
Definition: Entity.cpp:117
Entity * GetChild(const std::string &name) const
Get child based on its name.
Definition: Entity.cpp:139
const glm::vec3 & GetScale() const
Get the local scale.
Definition: Entity.cpp:277
void RotateAroundWorldAxis(float angle, const glm::vec3 &axis)
Rotates around an axis given in world space.
Definition: Entity.cpp:331
void RotateRoll(float angle)
Rotates around the Z axis.
Definition: Entity.cpp:327
Entity * InstantiateScene(const std::string &name)
Instantiate a scene as a child to this entity.
Definition: Entity.cpp:76
const glm::quat & GetRotation() const
Get the rotation of this entity.
Definition: Entity.cpp:286
glm::vec3 GetWorldDirection() const
Get direction of the entity.
Definition: Entity.cpp:315
void SetRotation(const glm::quat &localRot)
Set the local rotation of the entity.
Definition: Entity.cpp:290
glm::quat GetWorldRotation() const
Get rotation of the entity.
Definition: Entity.cpp:295
Entity * AddChild(const std::string &name="")
Add child entity.
Definition: Entity.cpp:39
bool sceneChosen
Definition: Entity.hpp:292
Entity * SetParent(Entity *newParent)
Set a new parent.
Definition: Entity.cpp:46
void SetWorldPosition(const glm::vec3 &worldPos)
Set the position of the entity in world space.
Definition: Entity.cpp:265
void SetEnabled(bool enabled, bool recursive=false)
Set whether the entity should be enabled.
Definition: Entity.cpp:368
bool brushActive
Definition: Entity.hpp:289
bool IsKilled() const
Get whether entity has been killed.
Definition: Entity.cpp:182
bool RemoveChild(Entity *child)
Remove child entity.
Definition: Entity.cpp:159
T * AddComponent()
Adds component with type T.
Definition: Entity.hpp:329
std::string name
Name of the entity.
Definition: Entity.hpp:273
unsigned int GetUniqueIdentifier() const
Get the entity's UID.
Definition: Entity.cpp:381
void Move(const glm::vec3 &translation)
Move in local space.
Definition: Entity.cpp:273
void SetPosition(const glm::vec3 &position)
Set the local position.
Definition: Entity.cpp:251
void Kill()
Kill the entity, will be removed at the end of the frame.
Definition: Entity.cpp:174
bool IsScene() const
Get whether the entity is an instantiated scene.
Definition: Entity.cpp:170
bool loadPaintModeClicked
Variables used for enabling and disabling the paint brush tool.
Definition: Entity.hpp:288
Entity(World *world, const std::string &name)
Create new entity.
Definition: Entity.cpp:29
bool IsEnabled() const
Get whether the entity is enabled.
Definition: Entity.cpp:377
void SetWorldRotation(const glm::quat &worldRot)
Set the rotation of the entity in world space.
Definition: Entity.cpp:305
Entity * GetChildFromIndex(int index) const
Get child based on its name.
Definition: Entity.cpp:148
unsigned int GetNumChildren() const
Get the number of children.
Definition: Entity.cpp:155
void SetScale(const glm::vec3 &scale)
Set the local scale.
Definition: Entity.cpp:281
~Entity()
Destructor.
Definition: Entity.cpp:33
void Serialize(Json::Value &node, bool load)
Save or load entity to/from JSON.
Definition: Entity.cpp:186
void KillComponent()
Kill component of type T.
Definition: Entity.hpp:338
Entity * GetParent() const
Get the entity's parent entity.
Definition: Entity.cpp:35
void SetUniqueIdentifier(unsigned int UID)
Set the entity's UID.
Definition: Entity.cpp:385
bool painting
Definition: Entity.hpp:291
bool HasChild(const Entity *child, bool deep=true) const
Check if entity is a child.
Definition: Entity.cpp:65
T * GetComponent() const
Gets component with type T.
Definition: Entity.hpp:334
The game world containing all entities.
Definition: World.hpp:14