Przeglądaj źródła

Lesson 12.1

(and fix some typo)
Godzil 3 lat temu
rodzic
commit
d40ebc22bc
4 zmienionych plików z 45 dodań i 10 usunięć
  1. 2 2
      source/ECS.cpp
  2. 8 4
      source/Game.cpp
  3. 4 4
      source/include/ECS.h
  4. 31 0
      source/include/Systems/Movement.h

+ 2 - 2
source/ECS.cpp

@@ -34,7 +34,7 @@ void System::removeEntity(Entity entity)
     }), this->entities.end());
 }
 
-std::vector<Entity> System::getEntities() const
+std::vector<Entity> System::getSystemEntities() const
 {
     return this->entities;
 }
@@ -82,7 +82,7 @@ void Registry::addEntityToSystems(Entity entity)
     }
 }
 
-void Registry::Update()
+void Registry::update()
 {
     /* Add pending entities */
     for(auto entity: entitiesToBeAdded)

+ 8 - 4
source/Game.cpp

@@ -19,6 +19,8 @@
 #include <Components/Transform.h>
 #include <Components/RigidBody.h>
 
+#include <Systems/Movement.h>
+
 Game::Game()
 {
     this->isRunning = false;
@@ -96,14 +98,12 @@ void Game::Run()
 
 void Game::Setup()
 {
-    Entity tank = this->registry->createEntity();
+    this->registry->addSystem<MovementSystem>();
 
-    //this->registry->addComponent<TransformComponent>(tank, glm::vec2(10, 30), glm::vec2(1, 1), 0);
-    //this->registry->addComponent<RigidBodyComponent>(tank, glm::vec2(50, 0));
+    Entity tank = this->registry->createEntity();
 
     tank.addComponent<TransformComponent>(glm::vec2(10, 30), glm::vec2(1, 1), 0);
     tank.addComponent<RigidBodyComponent>(glm::vec2(50, 0));
-    tank.removeComponent<TransformComponent>();
 }
 
 void Game::Update()
@@ -116,6 +116,10 @@ void Game::Update()
     double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
 
     this->millisecsPerviousFrame = SDL_GetTicks();
+
+    registry->getSystem<MovementSystem>().Update();
+
+    registry->update();
 }
 
 void Game::ProcessInput()

+ 4 - 4
source/include/ECS.h

@@ -79,7 +79,7 @@ public:
 
     void addEntity(Entity entity);
     void removeEntity(Entity entity);
-    std::vector<Entity> getEntities() const;
+    std::vector<Entity> getSystemEntities() const;
     const Signature& getComponentSignature() const;
 
     template<typename T> void requireComponent();
@@ -125,7 +125,7 @@ public:
     template<typename T> T& getSystem() const;
 
     /* Others */
-    void Update();
+    void update();
 };
 
 template<typename T, typename ...TArgs> void Registry::addComponent(Entity entity, TArgs&& ...args)
@@ -184,7 +184,7 @@ template <typename T> T& Registry::getComponent(Entity entity)
     const auto entityId = entity.getId();
 
     auto componentPool = std::static_pointer_cast<Pool<T>>(this->componentPools[componentId]);
-    return componentPool.get(entityId);
+    return componentPool->get(entityId);
 }
 
 template<typename T, typename ...TArgs> void Registry::addSystem(TArgs&& ...args)
@@ -224,7 +224,7 @@ template <typename T> bool Entity::hasComponent()
 }
 template <typename T> T& Entity::getComponent()
 {
-    this->registry->getComponent<T>(*this);
+    return this->registry->getComponent<T>(*this);
 }
 
 #endif /* GAMEENGINE_ECS_H */

+ 31 - 0
source/include/Systems/Movement.h

@@ -10,4 +10,35 @@
 #ifndef GAMEENGINE_MOVEMENT_H
 #define GAMEENGINE_MOVEMENT_H
 
+#include <ECS.h>
+#include <Logger.h>
+
+#include <Components/Transform.h>
+#include <Components/RigidBody.h>
+
+class MovementSystem: public System
+{
+public:
+    MovementSystem()
+    {
+        this->requireComponent<TransformComponent>();
+        this->requireComponent<RigidBodyComponent>();
+    }
+
+    void Update()
+    {
+        for (auto entity: this->getSystemEntities())
+        {
+            auto &transform = entity.getComponent<TransformComponent>();
+            const auto rigidbody = entity.getComponent<RigidBodyComponent>();
+
+            transform.position.x += rigidbody.velocity.x;
+            transform.position.y += rigidbody.velocity.y;
+
+            Logger::Debug("Entity id#%d - Position is now (%f, %f)",
+                          entity.getId(), transform.position.x, transform.position.y);
+        }
+    }
+};
+
 #endif /* GAMEENGINE_MOVEMENT_H */