Browse Source

Lesson 16.2

Godzil 3 years ago
parent
commit
ac7a5c08b7
3 changed files with 51 additions and 5 deletions
  1. 41 3
      source/ECS.cpp
  2. 7 0
      source/include/ECS.h
  3. 3 2
      source/include/Systems/Collision.h

+ 41 - 3
source/ECS.cpp

@@ -21,6 +21,11 @@ uint32_t Entity::getId() const
     return this->id;
 }
 
+void Entity::kill()
+{
+    this->registry->killEntity(*this);
+}
+
 /* System */
 void System::addEntity(Entity entity)
 {
@@ -48,7 +53,18 @@ const Signature& System::getComponentSignature() const
 
 Entity Registry::createEntity()
 {
-    uint32_t entityId = this->numEntities++;
+    uint32_t entityId;
+
+    if (this->freeIds.empty())
+    {
+        entityId = this->numEntities++;
+    }
+    else
+    {
+        entityId = this->freeIds.front();
+        this->freeIds.pop_front();
+    }
+
     Entity entity(entityId);
 
     entity.registry = this;
@@ -65,6 +81,12 @@ Entity Registry::createEntity()
     return entity;
 }
 
+void Registry::killEntity(Entity entity)
+{
+    Logger::Info("Entity #%d flagged for killing", entity.getId());
+    this->entitiesToBeKilled.insert(entity);
+}
+
 void Registry::addEntityToSystems(Entity entity)
 {
     const auto entityId = entity.getId();
@@ -83,14 +105,30 @@ void Registry::addEntityToSystems(Entity entity)
     }
 }
 
+void Registry::removeEntityFromSystems(Entity entity)
+{
+    for (auto &system: this->systems)
+    {
+        system.second->removeEntity(entity);
+    }
+}
+
 void Registry::update()
 {
     /* Add pending entities */
-    for(auto entity: entitiesToBeAdded)
+    for(auto entity: this->entitiesToBeAdded)
     {
         this->addEntityToSystems(entity);
     }
-    entitiesToBeAdded.clear();
+    this->entitiesToBeAdded.clear();
 
+    for(auto entity: this->entitiesToBeKilled)
+    {
+        this->removeEntityFromSystems(entity);
 
+        this->entityComponentSignatures[entity.getId()].reset();
+
+        this->freeIds.push_back(entity.getId());
+    }
+    this->entitiesToBeKilled.clear();
 }

+ 7 - 0
source/include/ECS.h

@@ -18,6 +18,7 @@
 #include <typeindex>
 #include <set>
 #include <memory>
+#include <deque>
 
 #include <Logger.h>
 #include <Pool.h>
@@ -66,6 +67,7 @@ public:
     template <typename T> bool hasComponent();
     template <typename T> T& getComponent();
     class Registry *registry = nullptr;
+    void kill();
 };
 
 class System
@@ -105,13 +107,18 @@ private:
     std::set<Entity> entitiesToBeAdded;
     std::set<Entity> entitiesToBeKilled;
 
+    std::deque<int> freeIds;
+
 public:
     Registry() = default;
     ~Registry() = default;
 
     /* Entities */
     Entity createEntity();
+    void killEntity(Entity entity);
+
     void addEntityToSystems(Entity entity);
+    void removeEntityFromSystems(Entity entity);
 
     /* Components */
     template <typename T, typename ...TArgs> void addComponent(Entity entity, TArgs&& ...args);

+ 3 - 2
source/include/Systems/Collision.h

@@ -71,6 +71,9 @@ public:
                     collideA.isColliding = true;
                     collideA.collidingList.push_back(*j);
                     collideB.collidingList.push_back(*i);
+
+                    i->kill();
+                    j->kill();
                 }
             }
         }
@@ -99,8 +102,6 @@ public:
             }
 
             SDL_RenderDrawRect(renderer, &rect);
-
-
         }
     }
 };