Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Entity.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Development Kit"
3 // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
4 
5 #pragma once
6 
7 #ifndef NDK_ENTITY_HPP
8 #define NDK_ENTITY_HPP
9 
10 #include <Nazara/Core/Bitset.hpp>
11 #include <Nazara/Core/HandledObject.hpp>
12 #include <Nazara/Core/Signal.hpp>
13 #include <NDK/Algorithm.hpp>
14 #include <NDK/Prerequesites.hpp>
15 #include <memory>
16 #include <vector>
17 
18 namespace Ndk
19 {
20  class BaseComponent;
21  class BaseSystem;
22  class Entity;
23  class EntityList;
24  class World;
25 
26  using EntityHandle = Nz::ObjectHandle<Entity>;
27 
28  class NDK_API Entity : public Nz::HandledObject<Entity>
29  {
30  friend BaseSystem;
31  friend EntityList;
32  friend World;
33 
34  public:
35  Entity(const Entity&) = delete;
36  Entity(Entity&& entity);
37  ~Entity();
38 
39  BaseComponent& AddComponent(std::unique_ptr<BaseComponent>&& component);
40  template<typename ComponentType, typename... Args> ComponentType& AddComponent(Args&&... args);
41 
42  const EntityHandle& Clone() const;
43 
44  inline void Enable(bool enable = true);
45 
46  inline BaseComponent& GetComponent(ComponentIndex index);
47  template<typename ComponentType> ComponentType& GetComponent();
48  inline const BaseComponent& GetComponent(ComponentIndex index) const;
49  template<typename ComponentType> const ComponentType& GetComponent() const;
50  inline const Nz::Bitset<>& GetComponentBits() const;
51  inline EntityId GetId() const;
52  inline const Nz::Bitset<>& GetSystemBits() const;
53  inline World* GetWorld() const;
54 
55  inline bool HasComponent(ComponentIndex index) const;
56  template<typename ComponentType> bool HasComponent() const;
57 
58  void Kill();
59 
60  void Invalidate();
61  inline bool IsEnabled() const;
62  inline bool IsValid() const;
63 
64  inline void RemoveAllComponents();
65  inline void RemoveComponent(ComponentIndex index);
66  template<typename ComponentType> void RemoveComponent();
67 
68  inline Nz::String ToString() const;
69 
70  Entity& operator=(const Entity&) = delete;
71  Entity& operator=(Entity&&) = delete;
72 
73  NazaraSignal(OnEntityDestruction, Entity* /*entity*/);
74 
75  private:
76  Entity(World* world, EntityId id);
77 
78  void Create();
79  void Destroy();
80 
81  void DestroyComponent(ComponentIndex index);
82 
83  inline Nz::Bitset<>& GetRemovedComponentBits();
84 
85  inline void RegisterEntityList(EntityList* list);
86  inline void RegisterSystem(SystemIndex index);
87 
88  inline void SetWorld(World* world) noexcept;
89 
90  inline void UnregisterEntityList(EntityList* list);
91  inline void UnregisterSystem(SystemIndex index);
92 
93  std::vector<std::unique_ptr<BaseComponent>> m_components;
94  std::vector<EntityList*> m_containedInLists;
95  Nz::Bitset<> m_componentBits;
96  Nz::Bitset<> m_removedComponentBits;
97  Nz::Bitset<> m_systemBits;
98  EntityId m_id;
99  World* m_world;
100  bool m_enabled;
101  bool m_valid;
102  };
103 }
104 
105 #include <NDK/Entity.inl>
106 
107 #endif // NDK_ENTITY_HPP
TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so...
Definition: Algorithm.hpp:12
NDK class that represents an entity in a world.
Definition: Entity.hpp:28
Core class that represents a string.
Definition: String.hpp:22
NDK class that represents the common base of all systems.
Definition: BaseSystem.hpp:17
NDK class that represents a set of entities to help performing batch operations.
Definition: EntityList.hpp:16
NDK class that represents a world.
Definition: World.hpp:25
Core class that represents a set of bits.
Definition: Bitset.hpp:22
NDK class that represents the common base of all components.
Definition: BaseComponent.hpp:17