Godzil 3 anni fa
parent
commit
9e8e854261
1 ha cambiato i file con 21 aggiunte e 27 eliminazioni
  1. 21 27
      source/include/Systems/Collision.h

+ 21 - 27
source/include/Systems/Collision.h

@@ -16,30 +16,26 @@
 #include <Components/Transform.h>
 #include <Components/BoxCollider.h>
 
-class BoxRect
+class CollisionSystem: public System
 {
-    int32_t x0, y0, x1, y1;
-public:
-    BoxRect(TransformComponent tc, BoxColliderComponent bc)
+    class BoxRect
     {
-        this->x0 = tc.position.x + bc.offset.x;
-        this->x1 = this->x0 + bc.width;
-        this->y0 = tc.position.y + bc.offset.y;
-        this->y1 = this->y0 + bc.height;
-    }
+        double x0, y0, x1, y1;
+    public:
+        BoxRect(TransformComponent tc, BoxColliderComponent bc)
+        {
+            this->x0 = tc.position.x + bc.offset.x;
+            this->x1 = this->x0 + bc.width;
+            this->y0 = tc.position.y + bc.offset.y;
+            this->y1 = this->y0 + bc.height;
+        }
 
-    bool collideWith(BoxRect &b)
-    {
-        if ( (this->x0 < b.x1) && (this->x1 > b.x0) && (this->y0 < b.y1) && (this->y1 > b.y0) )
+        bool collideWith(BoxRect &b) const
         {
-            return true;
+            return ((this->x0 < b.x1) && (this->x1 > b.x0) && (this->y0 < b.y1) && (this->y1 > b.y0));
         }
-        return false;
-    }
-};
+    };
 
-class CollisionSystem: public System
-{
 public:
     CollisionSystem()
     {
@@ -50,20 +46,18 @@ public:
     void update()
     {
         auto entities = this->getSystemEntities();
-        for(auto entityA: this->getSystemEntities())
+        for(auto i = entities.begin(); i != entities.end(); i++)
         {
-            for(auto entityB: this->getSystemEntities())
+            for(auto j = (i + 1); j != entities.end(); j++)
             {
-                if (entityA.getId() == entityB.getId())
-                {
-                    continue; /* We are not colliding ourselves */
-                }
-                BoxRect boxA = BoxRect(entityA.getComponent<TransformComponent>(), entityA.getComponent<BoxColliderComponent>());
-                BoxRect boxB = BoxRect(entityB.getComponent<TransformComponent>(), entityB.getComponent<BoxColliderComponent>());
+                BoxRect boxA = BoxRect(i->getComponent<TransformComponent>(),
+                                       i->getComponent<BoxColliderComponent>());
+                BoxRect boxB = BoxRect(j->getComponent<TransformComponent>(),
+                                       j->getComponent<BoxColliderComponent>());
 
                 if (boxA.collideWith(boxB))
                 {
-                    Logger::Debug("Entityty #%d collided with #%d!", entityA.getId(), entityB.getId());
+                    Logger::Debug("Entity #%d collided with #%d!", i->getId(), j->getId());
                 }
             }
         }