Godzil 3 éve
szülő
commit
723a4627c7
3 módosított fájl, 45 hozzáadás és 1 törlés
  1. 13 0
      source/include/Components/Transform.h
  2. 31 1
      source/include/ECS.h
  3. 1 0
      source/include/Pool.h

+ 13 - 0
source/include/Components/Transform.h

@@ -10,4 +10,17 @@
 #ifndef GAMEENGINE_TRANSFORM_H
 #define GAMEENGINE_TRANSFORM_H
 
+#include <glm/glm.hpp>
+
+struct TransformComponent
+{
+    glm::vec2 position;
+    glm::vec2 scale;
+    double rotation;
+
+    explicit TransformComponent(glm::vec2 position = glm::vec2(0, 0),
+                                glm::vec2 scale = glm::vec2(1, 1),
+                                double rotation = 0):position(position), scale(scale), rotation(rotation) {};
+};
+
 #endif /* GAMEENGINE_TRANSFORM_H */

+ 31 - 1
source/include/ECS.h

@@ -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 */

+ 1 - 0
source/include/Pool.h

@@ -28,6 +28,7 @@ public:
     virtual ~Pool() = default;
 
     bool isEmpty() const { return this->data.empty(); }
+    uint32_t getSize() { return this->data.size(); }
     void resize(int size) { this->data.reserve(size); }
     void clear() { this->data.clear(); }