Godzil 3 éve
szülő
commit
4ae1651710
3 módosított fájl, 44 hozzáadás és 2 törlés
  1. 3 0
      source/ECS.cpp
  2. 6 2
      source/Game.cpp
  3. 35 0
      source/include/ECS.h

+ 3 - 0
source/ECS.cpp

@@ -49,6 +49,9 @@ Entity Registry::createEntity()
 {
     uint32_t entityId = this->numEntities++;
     Entity entity(entityId);
+
+    entity.registry = this;
+
     this->entitiesToBeAdded.insert(entity);
 
     if (entityId >= this->entityComponentSignatures.size())

+ 6 - 2
source/Game.cpp

@@ -98,8 +98,12 @@ void Game::Setup()
 {
     Entity tank = this->registry->createEntity();
 
-    this->registry->addComponent<TransformComponent>(tank, glm::vec2(10, 30), glm::vec2(1, 1), 0);
-    this->registry->addComponent<RigidBodyComponent>(tank, glm::vec2(50, 0));
+    //this->registry->addComponent<TransformComponent>(tank, glm::vec2(10, 30), glm::vec2(1, 1), 0);
+    //this->registry->addComponent<RigidBodyComponent>(tank, glm::vec2(50, 0));
+
+    tank.addComponent<TransformComponent>(glm::vec2(10, 30), glm::vec2(1, 1), 0);
+    tank.addComponent<RigidBodyComponent>(glm::vec2(50, 0));
+
 }
 
 void Game::Update()

+ 35 - 0
source/include/ECS.h

@@ -57,6 +57,14 @@ public:
     bool operator!=(const Entity &other) const { return this->id != other.id; };
     bool operator>(const Entity &other) const  { return this->id > other.id; };
     bool operator<(const Entity &other) const  { return this->id < other.id; };
+
+
+    /* Some syntaxic sugar */
+    template <typename T, typename ...TArgs> void addComponent(TArgs&& ...args);
+    template <typename T> void removeComponent();
+    template <typename T> bool hasComponent();
+    template <typename T> T& getComponent();
+    class Registry *registry = nullptr;
 };
 
 class System
@@ -108,6 +116,7 @@ public:
     template <typename T, typename ...TArgs> void addComponent(Entity entity, TArgs&& ...args);
     template <typename T> void removeComponent(Entity entity);
     template <typename T> bool hasComponent(Entity entity);
+    template <typename T> T& getComponent(Entity entity);
 
     /* Systems */
     template<typename T, typename ...TArgs> void addSystem(TArgs&& ...args);
@@ -167,6 +176,15 @@ template <typename T> bool Registry::hasComponent(Entity entity)
     return this->entityComponentSignatures[entityId].test(componentId);
 }
 
+template <typename T> T& Registry::getComponent(Entity entity)
+{
+    const auto componentId = Component<T>::getId();
+    const auto entityId = entity.getId();
+
+    auto componentPool = std::static_pointer_cast<Pool<T>>(this->componentPools[componentId]);
+    return componentPool.get(entityId);
+}
+
 template<typename T, typename ...TArgs> void Registry::addSystem(TArgs&& ...args)
 {
     std::shared_ptr<T> newSystem = std::make_shared<T>(std::forward<TArgs>(args)...);
@@ -190,4 +208,21 @@ template<typename T> T& Registry::getSystem() const
     return *(std::static_pointer_cast<T>(system->second));
 }
 
+template <typename T, typename ...TArgs> void Entity::addComponent(TArgs&& ...args)
+{
+    this->registry->addComponent<T>(*this, std::forward<TArgs>(args)...);
+}
+template <typename T> void Entity::removeComponent()
+{
+    this->registry->removeComponent<T>(*this);
+}
+template <typename T> bool Entity::hasComponent()
+{
+    return this->registry->hasComponent<T>(*this);
+}
+template <typename T> T& Entity::getComponent()
+{
+    this->registry->getComponent<T>(*this);
+}
+
 #endif /* GAMEENGINE_ECS_H */