Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
Component.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Engine/api.hpp"
4 
5 #include <ThirdParty/cereal/include/cereal/cereal.hpp>
6 #include <ThirdParty/EASTL-master/include/EASTL/shared_ptr.h>
7 #include <ThirdParty/EASTL-master/include/EASTL/vector.h>
8 
9 namespace Engine
10 {
11  class Entity;
12 
17  {
18  public:
19  virtual ~Component() = default;
24  eastl::weak_ptr<Entity> GetOwner() const;
25 
26  bool isEnabled;
27 
28  protected:
29  friend class Entity;
30  explicit Component();
31 
35  virtual void InitializeComponent();
36 
40  virtual void Update();
41 
46  virtual void OnComponentAdded(eastl::weak_ptr<Component> addedComponent);
47 
52  virtual void OnComponentRemoved(eastl::weak_ptr<Component> removedComponent);
53 
54  template<typename ComponentType>
59  eastl::weak_ptr<ComponentType> GetComponent();
60 
61  template<typename ComponentType>
66  eastl::vector<eastl::weak_ptr<ComponentType>> GetComponents();
67 
72  eastl::vector<eastl::shared_ptr<Component>> GetAllComponents() const;
73 
74  template <class ComponentType, class... Args>
80  eastl::weak_ptr<ComponentType> AddComponent(Args&&... args);
81 
82  //template <class ComponentType, class... Args, eastl::enable_if_t<eastl::extent<ComponentType>::value != 0, int> = 0>
83  //eastl::weak_ptr<ComponentType> AddComponent(Args&&... args) = delete;
84 
85  template <class ComponentType, class... Args>
92  eastl::vector<eastl::weak_ptr<ComponentType>> AddComponents(size_t count, Args&&... args);
93 
94  template <typename ComponentType>
99  void RemoveComponent(size_t amountToRemove = 1) const;
100 
101  template <typename ComponentType>
105  void RemoveAllComponents() const;
106 
111  template <typename archive>
112  void SaveBaseComponent(archive ar);
113 
118  template <typename archive>
119  void LoadBaseComponent(archive ar);
120 
121  virtual void OnBeginContact(eastl::weak_ptr<Entity> entity);
122  virtual void OnEndContact(eastl::weak_ptr<Entity> entity);
123 
128  eastl::weak_ptr<Component> GetPointerRefence() const;
129 
130  template <typename ComponentType>
135  eastl::weak_ptr<ComponentType> GetPointerRefence();
136  private:
141  void SetOwner(eastl::weak_ptr<Entity> owner);
142 
143  void SetPointerReference(eastl::weak_ptr<Component> pointerReference);
144 
145  eastl::weak_ptr<Entity> owner;
146  eastl::weak_ptr<Component> pointerReference;
147 
148  };
149 
150  template <typename ComponentType>
151  eastl::weak_ptr<ComponentType> Component::GetComponent()
152  {
153  return owner.lock()->GetComponent<ComponentType>();
154  }
155 
156  template <typename ComponentType>
157  eastl::vector<eastl::weak_ptr<ComponentType>> Component::GetComponents()
158  {
159  return owner.lock()->GetComponents<ComponentType>();
160  }
161 
162  template <class ComponentType, class ... Args>
163  eastl::weak_ptr<ComponentType> Component::AddComponent(Args&&... args)
164  {
165  return owner.lock()->AddComponent<ComponentType>(&args...);
166  }
167 
168  template <class ComponentType, class ... Args>
169  eastl::vector<eastl::weak_ptr<ComponentType>> Component::AddComponents(size_t count, Args&&... args)
170  {
171  return owner.lock()->AddComponents<ComponentType>(count, &args...);
172  }
173 
174  template <typename ComponentType>
175  void Component::RemoveComponent(size_t amountToRemove) const
176  {
177  owner.lock()->RemoveComponent<ComponentType>(amountToRemove);
178  }
179 
180  template <typename ComponentType>
182  {
183  owner.lock()->RemoveAllComponents<ComponentType>();
184  }
185 
186  template <typename ComponentType>
187  eastl::weak_ptr<ComponentType> Component::GetPointerRefence()
188  {
189  return eastl::dynamic_pointer_cast<ComponentType, Component>(pointerReference.lock());
190  }
191 } // namespace Engine
eastl::weak_ptr< ComponentType > GetComponent()
This method allows you to get the first component of the given type.
Definition: Component.hpp:151
This is the base class for components. NOTE: only the Entity class is allowed to create this object...
Definition: Component.hpp:16
This object is able a holder object for components. NOTE: only the EntitySystem is allowed to create ...
Definition: Entity.hpp:13
eastl::vector< eastl::weak_ptr< ComponentType > > AddComponents(size_t count, Args &&...args)
Allows you to create X amount of components of the given type.
Definition: Component.hpp:169
#define ENGINE_API
Definition: api.hpp:25
eastl::vector< eastl::weak_ptr< ComponentType > > GetComponents()
This method allows you to get all of the components of the given type.
Definition: Component.hpp:157
void RemoveAllComponents() const
Deletes all the components of the given type.
Definition: Component.hpp:181
eastl::weak_ptr< Component > GetPointerRefence() const
This method allows you to get a direct refence to the weak pointer of this component.
Definition: Component.cpp:47
eastl::weak_ptr< ComponentType > AddComponent(Args &&...args)
Allows you to create a component of the given type.
Definition: Component.hpp:163
void RemoveComponent(size_t amountToRemove=1) const
Allows X amount of components of the given type.
Definition: Component.hpp:175