Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Entity.hpp
1#pragma once
2
3#include <Common/Math.hpp>
4#include <Common/HashedString.hpp>
5#include <EngineCore/CoreComponents/Parent/ParentComponent.hpp>
6
7#include "EntityHandle.hpp"
8
9namespace Grindstone {
10 namespace SceneManagement {
11 class Scene;
12 }
13
14 namespace ECS {
15 class Entity {
16 private:
17 EntityHandle entityId = entt::null;
18 SceneManagement::Scene* scene = nullptr;
19 public:
20 Entity() = default;
21 Entity(const Entity& other) = default;
22 Entity(entt::entity entityId, SceneManagement::Scene * scene)
23 : entityId(entityId), scene(scene) {}
24
25 virtual void* AddComponent(Grindstone::HashedString componentType);
26 virtual void* AddComponentWithoutSetup(Grindstone::HashedString componentType);
27 virtual bool HasComponent(Grindstone::HashedString componentType) const;
28 virtual void* GetComponent(Grindstone::HashedString componentType) const;
29 virtual bool TryGetComponent(Grindstone::HashedString componentType, void*& outComponent) const;
30 virtual void RemoveComponent(Grindstone::HashedString componentType);
31
32 // Parent helpers
33 virtual bool IsChildOf(const Entity& other) const;
34 virtual Entity GetParent() const;
35 virtual bool SetParent(Entity);
36
37 // Transform helpers
38 virtual Math::Matrix4 GetLocalMatrix() const;
39 virtual Math::Matrix4 GetWorldMatrix() const;
40 virtual Math::Float3 GetLocalPosition() const;
41 virtual Math::Float3 GetWorldPosition() const;
42 virtual Math::Quaternion GetLocalRotation() const;
43 virtual Math::Quaternion GetWorldRotation() const;
44 virtual Math::Float3 GetLocalScale() const;
45
46 virtual Math::Float3 GetLocalForward() const;
47 virtual Math::Float3 GetWorldForward() const;
48 virtual Math::Float3 GetLocalRight() const;
49 virtual Math::Float3 GetWorldRight() const;
50 virtual Math::Float3 GetLocalUp() const;
51 virtual Math::Float3 GetWorldUp() const;
52
53 virtual void Destroy();
54 virtual entt::registry& GetSceneEntityRegistry() const;
55
56 template<typename ComponentType, typename... Args>
57 ComponentType& AddComponent(Args&&... args) {
58 return GetSceneEntityRegistry().emplace<ComponentType>(entityId, std::forward<Args>(args)...);
59 }
60
61 template<typename ComponentType>
62 bool HasComponent() const {
63 return GetSceneEntityRegistry().all_of<ComponentType>(entityId);
64 }
65
66 template<typename ComponentType>
67 ComponentType& GetComponent() const {
68 return GetSceneEntityRegistry().get<ComponentType>(entityId);
69 }
70
71 template<typename ComponentType>
72 bool TryGetComponent(ComponentType*& outComponent) const {
73 ComponentType* testComponent = GetSceneEntityRegistry().try_get<ComponentType>(entityId);
74 if (testComponent != nullptr) {
75 outComponent = testComponent;
76 return true;
77 }
78
79 return false;
80 }
81
82 template<typename ComponentType>
83 void RemoveComponent() {
84 GetSceneEntityRegistry().remove<ComponentType>(entityId);
85 }
86
87 virtual EntityHandle GetHandle() const {
88 return entityId;
89 }
90
91 virtual SceneManagement::Scene* GetScene() const {
92 return scene;
93 }
94
95 explicit operator bool() const {
96 return entityId != entt::null && scene != nullptr && GetSceneEntityRegistry().valid(entityId);
97 }
98
99 bool operator==(const Entity& other) const {
100 return (entityId == other.entityId) && (scene == other.scene);
101 }
102
103 bool operator!=(const Entity& other) const {
104 return !(*this == other);
105 }
106 };
107
108 inline bool operator < (const ECS::Entity& lhs, const ECS::Entity& rhs) {
109 const bool isSceneLess = lhs.GetScene() < rhs.GetScene();
110 const bool isEntityLess = lhs.GetHandle() < rhs.GetHandle();
111 return isSceneLess || isEntityLess;
112 }
113 }
114}
Definition Entity.hpp:15
Definition HashedString.hpp:9
Definition Quaternion.cs:6
Definition Scene.hpp:21
Definition FloatVectors.cs:108