Godzil 3 年 前
コミット
e8ec6990ec
4 ファイル変更25 行追加5 行削除
  1. 15 3
      source/ECS.cpp
  2. 4 0
      source/Game.cpp
  3. 2 2
      source/include/ECS.h
  4. 4 0
      source/include/Game.h

+ 15 - 3
source/ECS.cpp

@@ -45,18 +45,23 @@ const Signature& System::getComponentSignature() const
 }
 
 
-Entity Registry::CreateEntity()
+Entity Registry::createEntity()
 {
     uint32_t entityId = this->numEntities++;
     Entity entity(entityId);
     this->entitiesToBeAdded.insert(entity);
 
-    Logger::Info("Entity created with id = %s", entityId);
+    if (entityId >= this->entityComponentSignatures.size())
+    {
+      this->entityComponentSignatures.resize(entityId + 1);
+    }
+
+    Logger::Info("Entity created with id = %d", entityId);
 
     return entity;
 }
 
-void Registry::addEntityToSystem(Entity entity)
+void Registry::addEntityToSystems(Entity entity)
 {
     const auto entityId = entity.getId();
     const auto entityComponentSignature = this->entityComponentSignatures[entityId];
@@ -76,5 +81,12 @@ void Registry::addEntityToSystem(Entity entity)
 
 void Registry::Update()
 {
+    /* Add pending entities */
+    for(auto entity: entitiesToBeAdded)
+    {
+        this->addEntityToSystems(entity);
+    }
+    entitiesToBeAdded.clear();
+
 
 }

+ 4 - 0
source/Game.cpp

@@ -19,6 +19,8 @@ Game::Game()
     this->isRunning = false;
     this->windowsHeight = 0;
     this->windowsWidth = 0;
+
+    this->registry = new Registry();
 }
 
 Game::~Game()
@@ -89,6 +91,8 @@ void Game::Run()
 
 void Game::Setup()
 {
+    Entity tank = this->registry->createEntity();
+    Entity truck = this->registry->createEntity();
 
 }
 

+ 2 - 2
source/include/ECS.h

@@ -100,8 +100,8 @@ public:
     ~Registry() = default;
 
     /* Entities */
-    Entity CreateEntity();
-    void addEntityToSystem(Entity entity);
+    Entity createEntity();
+    void addEntityToSystems(Entity entity);
 
     /* Components */
     template <typename T, typename ...TArgs> void addComponent(Entity entity, TArgs&& ...args);

+ 4 - 0
source/include/Game.h

@@ -12,6 +12,8 @@
 #include <SDL.h>
 #include <stdint.h>
 
+#include <ECS.h>
+
 const uint8_t FPS = 60;
 const uint32_t MILLISECS_PER_FRAME = 1000 / FPS;
 
@@ -23,6 +25,8 @@ private:
     SDL_Window *window;
     SDL_Renderer *renderer;
 
+    Registry *registry;
+
 public:
     Game();
     ~Game();