|
@@ -104,7 +104,8 @@ public:
|
|
|
void addEntityToSystem(Entity entity);
|
|
|
|
|
|
/* Components */
|
|
|
- //void addComponent();
|
|
|
+ template <typename T, typename ...TArgs> void addComponent(Entity entity, TArgs&& ...args);
|
|
|
+
|
|
|
|
|
|
/* Systems */
|
|
|
|
|
@@ -113,4 +114,33 @@ public:
|
|
|
void Update();
|
|
|
};
|
|
|
|
|
|
+template<typename T, typename ...TArgs> void Registry::addComponent(Entity entity, TArgs&& ...args)
|
|
|
+{
|
|
|
+ const auto componentId = Component<T>::getId();
|
|
|
+ const auto entityId = entity.getId();
|
|
|
+ Pool<T> *componentPool;
|
|
|
+ /* Check the pool is big enough */
|
|
|
+ if (componentId >= this->componentPools.size())
|
|
|
+ {
|
|
|
+ this->componentPools.reserve(componentId + 1, nullptr);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Is this component exist in the pool? */
|
|
|
+ if (!this->componentPools[componentId])
|
|
|
+ {
|
|
|
+ this->componentPools[componentId] = new Pool<T>();
|
|
|
+ }
|
|
|
+
|
|
|
+ componentPool = this->componentPools[componentId];
|
|
|
+
|
|
|
+ if (entityId >= componentPool->getSize())
|
|
|
+ {
|
|
|
+ componentPool->resize(this->numEntities);
|
|
|
+ }
|
|
|
+
|
|
|
+ T newComponent(std::forward<TArgs>(args)...);
|
|
|
+
|
|
|
+ componentPool->set(entityId, newComponent);
|
|
|
+ entityComponentSignatures[entityId].set(componentId);
|
|
|
+}
|
|
|
#endif /* GAMEENGINE_ECS_H */
|