Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
BaseComponent.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_BASECOMPONENT_HPP
8 #define NDK_BASECOMPONENT_HPP
9 
10 #include <NDK/Entity.hpp>
11 #include <functional>
12 #include <unordered_map>
13 #include <vector>
14 
15 namespace Ndk
16 {
17  class NDK_API BaseComponent
18  {
19  friend Entity;
20  friend class Sdk;
21 
22  public:
23  using Factory = std::function<BaseComponent*()>;
24 
25  BaseComponent(ComponentIndex componentIndex);
26  BaseComponent(BaseComponent&&) = default;
27  virtual ~BaseComponent();
28 
29  virtual std::unique_ptr<BaseComponent> Clone() const = 0;
30 
31  inline const EntityHandle& GetEntity() const;
32  ComponentIndex GetIndex() const;
33 
34  inline static ComponentIndex GetMaxComponentIndex();
35 
36  BaseComponent& operator=(const BaseComponent&) = delete;
37  BaseComponent& operator=(BaseComponent&&) = default;
38 
39  protected:
40  BaseComponent(const BaseComponent&) = default;
41 
42  ComponentIndex m_componentIndex;
43  EntityHandle m_entity;
44 
45  static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc);
46 
47  private:
48  virtual void OnAttached();
49  virtual void OnComponentAttached(BaseComponent& component);
50  virtual void OnComponentDetached(BaseComponent& component);
51  virtual void OnDetached();
52  virtual void OnEntityDestruction();
53 
54  void SetEntity(Entity* entity);
55 
56  static bool Initialize();
57  static void Uninitialize();
58 
59  struct ComponentEntry
60  {
61  ComponentId id;
62  Factory factory;
63  };
64 
65  static std::vector<ComponentEntry> s_entries;
66  static std::unordered_map<ComponentId, ComponentIndex> s_idToIndex;
67  };
68 }
69 
70 #include <NDK/BaseComponent.inl>
71 
72 #endif // NDK_BASECOMPONENT_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
NDK class that represents the software development kit, a set of tools made to ease the conception of...
Definition: Sdk.hpp:14
NDK class that represents the common base of all components.
Definition: BaseComponent.hpp:17