|
@@ -57,6 +57,14 @@ public:
|
|
|
bool operator!=(const Entity &other) const { return this->id != other.id; };
|
|
|
bool operator>(const Entity &other) const { return this->id > other.id; };
|
|
|
bool operator<(const Entity &other) const { return this->id < other.id; };
|
|
|
+
|
|
|
+
|
|
|
+ /* Some syntaxic sugar */
|
|
|
+ template <typename T, typename ...TArgs> void addComponent(TArgs&& ...args);
|
|
|
+ template <typename T> void removeComponent();
|
|
|
+ template <typename T> bool hasComponent();
|
|
|
+ template <typename T> T& getComponent();
|
|
|
+ class Registry *registry = nullptr;
|
|
|
};
|
|
|
|
|
|
class System
|
|
@@ -108,6 +116,7 @@ public:
|
|
|
template <typename T, typename ...TArgs> void addComponent(Entity entity, TArgs&& ...args);
|
|
|
template <typename T> void removeComponent(Entity entity);
|
|
|
template <typename T> bool hasComponent(Entity entity);
|
|
|
+ template <typename T> T& getComponent(Entity entity);
|
|
|
|
|
|
/* Systems */
|
|
|
template<typename T, typename ...TArgs> void addSystem(TArgs&& ...args);
|
|
@@ -167,6 +176,15 @@ template <typename T> bool Registry::hasComponent(Entity entity)
|
|
|
return this->entityComponentSignatures[entityId].test(componentId);
|
|
|
}
|
|
|
|
|
|
+template <typename T> T& Registry::getComponent(Entity entity)
|
|
|
+{
|
|
|
+ const auto componentId = Component<T>::getId();
|
|
|
+ const auto entityId = entity.getId();
|
|
|
+
|
|
|
+ auto componentPool = std::static_pointer_cast<Pool<T>>(this->componentPools[componentId]);
|
|
|
+ return componentPool.get(entityId);
|
|
|
+}
|
|
|
+
|
|
|
template<typename T, typename ...TArgs> void Registry::addSystem(TArgs&& ...args)
|
|
|
{
|
|
|
std::shared_ptr<T> newSystem = std::make_shared<T>(std::forward<TArgs>(args)...);
|
|
@@ -190,4 +208,21 @@ template<typename T> T& Registry::getSystem() const
|
|
|
return *(std::static_pointer_cast<T>(system->second));
|
|
|
}
|
|
|
|
|
|
+template <typename T, typename ...TArgs> void Entity::addComponent(TArgs&& ...args)
|
|
|
+{
|
|
|
+ this->registry->addComponent<T>(*this, std::forward<TArgs>(args)...);
|
|
|
+}
|
|
|
+template <typename T> void Entity::removeComponent()
|
|
|
+{
|
|
|
+ this->registry->removeComponent<T>(*this);
|
|
|
+}
|
|
|
+template <typename T> bool Entity::hasComponent()
|
|
|
+{
|
|
|
+ return this->registry->hasComponent<T>(*this);
|
|
|
+}
|
|
|
+template <typename T> T& Entity::getComponent()
|
|
|
+{
|
|
|
+ this->registry->getComponent<T>(*this);
|
|
|
+}
|
|
|
+
|
|
|
#endif /* GAMEENGINE_ECS_H */
|