Godzil 3 роки тому
батько
коміт
73552926c8
2 змінених файлів з 36 додано та 10 видалено
  1. 12 5
      source/ECS.cpp
  2. 24 5
      source/include/ECS.h

+ 12 - 5
source/ECS.cpp

@@ -21,20 +21,27 @@ uint32_t Entity::getId() const
 /* System */
 void System::addEntity(Entity entity)
 {
-
+    this->entities.push_back(entity);
 }
 
 void System::removeEntity(Entity entity)
 {
-
+    for(uint32_t i = 0; i < this->entities.size(); i++)
+    {
+        if (this->entities[i].getId() == entity.getId())
+        {
+            this->entities.erase(this->entities.begin() + i);
+            return;
+        }
+    }
 }
 
 std::vector<Entity> System::getEntities() const
 {
-
+    return this->entities;
 }
 
-Signature& System::getComponentSignature() const
+const Signature& System::getComponentSignature() const
 {
-
+    return this->componentSignature;
 }

+ 24 - 5
source/include/ECS.h

@@ -19,9 +19,20 @@ const uint32_t MAX_COMPONENTS = 32;
 
 typedef std::bitset<MAX_COMPONENTS> Signature;
 
-class Component
+struct IComponent
 {
+protected:
+    static uint32_t nextId;
+};
 
+template <typename T> class Component: public IComponent
+{
+public:
+    static uint32_t getId()
+    {
+        static auto id = IComponent::nextId++;
+        return id;
+    }
 };
 
 class Entity
@@ -44,12 +55,20 @@ public:
     System() = default;
     ~System() = default;
 
-  void addEntity(Entity entity);
-  void removeEntity(Entity entity);
-  std::vector<Entity> getEntities() const;
-  Signature& getComponentSignature() const;
+    void addEntity(Entity entity);
+    void removeEntity(Entity entity);
+    std::vector<Entity> getEntities() const;
+    const Signature& getComponentSignature() const;
+
+    template<typename T> void requireComponent();
 };
 
+template<typename T> void System::requireComponent()
+{
+    const auto componentId = Component<T>::getId();
+    this->componentSignature.set(componentId);
+}
+
 class Registry
 {