Just™ Game Engine
entity.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2022 Konstanty Misiak
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#pragma once
8#include "jng/scene/scene.hpp"
9
10#include <entt/entt.hpp>
11
12namespace jng {
13
14 class Scene;
15
16 class Entity
17 {
18 public:
19 Entity() = default;
20 Entity(entt::entity handle, Scene& scene);
21
22 template<typename Component, typename ...Args>
23 Component& addComponent(Args&& ...args);
24 template<typename Component, typename ...Args>
25 Component& addOrReplaceComponent(Args&& ...args);
26 template<typename Component>
27 void removeComponent();
28 template<typename Component>
29 bool hasComponent() const;
30 template<typename Component>
31 Component& getComponent();
32 template<typename Component>
33 const Component& getComponent() const;
34 template<typename Component, typename ...Args>
35 Component& getOrAddComponent(Args&& ...args);
36
37 GUID getGUID() const;
38 const std::string& getTag() const;
39
40 Scene* getScene() { return m_sceneRef; }
41
42 bool operator==(const Entity& other) const { return m_handle == other.m_handle; }
43 bool operator!=(const Entity& other) const { return m_handle != other.m_handle; }
44 operator bool() { return m_handle != entt::null; }
45 operator void*() { return reinterpret_cast<void*>(m_handle); }
46 private:
47 entt::entity m_handle{ entt::null };
48 Scene* m_sceneRef{ nullptr };
49
50 friend class Scene;
51 };
52
53 template<typename Component, typename ...Args>
54 Component& Entity::addComponent(Args&& ...args)
55 {
56 JNG_CORE_ASSERT(!hasComponent<Component>(), "Entity can't have more that one component of the same type!");
57 return m_sceneRef->m_registry.emplace<Component>(m_handle, std::forward<Args>(args)...);
58 }
59
60 template<typename Component, typename ...Args>
61 inline Component& Entity::addOrReplaceComponent(Args && ...args)
62 {
63 return m_sceneRef->m_registry.emplace_or_replace<Component>(m_handle, std::forward<Args>(args)...);
64 }
65
66 template<typename Component>
68 {
69 m_sceneRef->m_registry.remove<Component>(m_handle);
70 }
71
72 template<typename Component>
74 {
75 return m_sceneRef->m_registry.all_of<Component>(m_handle);
76 }
77
78 template<typename Component>
80 {
81 JNG_CORE_ASSERT(hasComponent<Component>(), "Entity doesn't have component of that type!");
82 return m_sceneRef->m_registry.get<Component>(m_handle);
83 }
84
85 template<typename Component>
86 const Component& Entity::getComponent() const
87 {
88 JNG_CORE_ASSERT(hasComponent<Component>(), "Entity doesn't have component of that type!");
89 return m_sceneRef->m_registry.get<Component>(m_handle);
90 }
91
92 template<typename Component, typename ...Args>
93 inline Component& Entity::getOrAddComponent(Args&& ...args)
94 {
95 return m_sceneRef->m_registry.get_or_emplace<Component>(m_handle, std::forward<Args>(args)...);
96 }
97
98} // namespace jng
#define JNG_CORE_ASSERT(x,...)
Definition: assert.hpp:24
Definition: entity.hpp:17
void removeComponent()
Definition: entity.hpp:67
friend class Scene
Definition: entity.hpp:50
bool operator!=(const Entity &other) const
Definition: entity.hpp:43
bool operator==(const Entity &other) const
Definition: entity.hpp:42
const std::string & getTag() const
Definition: entity.cpp:23
GUID getGUID() const
Definition: entity.cpp:18
Scene * getScene()
Definition: entity.hpp:40
Component & addOrReplaceComponent(Args &&...args)
Definition: entity.hpp:61
Component & getOrAddComponent(Args &&...args)
Definition: entity.hpp:93
Entity()=default
Component & getComponent()
Definition: entity.hpp:79
bool hasComponent() const
Definition: entity.hpp:73
Component & addComponent(Args &&...args)
Definition: entity.hpp:54
Definition: GUID.hpp:13
Definition: scene.hpp:23
Definition: base.hpp:11