Browse Source

Lesson 15.3

Godzil 3 years ago
parent
commit
28fe256acd

+ 11 - 0
source/Game.cpp

@@ -34,6 +34,7 @@ Game::Game()
     this->isRunning = false;
     this->windowsHeight = 0;
     this->windowsWidth = 0;
+    this->isDebug = false;
 
     this->registry = std::make_unique<Registry>();
     this->assetStore = std::make_unique<AssetStore>();
@@ -214,8 +215,13 @@ void Game::ProcessInput()
                 {
                     this->isRunning = false;
                 }
+                if (event.key.keysym.sym == SDLK_d)
+                {
+                    this->isDebug = !this->isDebug;
+                }
                 break;
 
+
             default:
                 break;
         }
@@ -230,5 +236,10 @@ void Game::Render()
     // Do something here
     registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore);
 
+    if (this->isDebug)
+    {
+        registry->getSystem<CollisionSystem>().debugRender(this->renderer);
+    }
+
     SDL_RenderPresent(this->renderer);
 }

+ 9 - 1
source/include/Components/BoxCollider.h

@@ -12,6 +12,7 @@
 
 #include <stdint.h>
 
+#include <vector>
 #include <glm/glm.hpp>
 
 struct BoxColliderComponent
@@ -19,9 +20,16 @@ struct BoxColliderComponent
     uint32_t width;
     uint32_t height;
     glm::vec2 offset;
+    bool isColliding;
+    std::vector<Entity>collidingList;
+
     explicit BoxColliderComponent(uint32_t width = 0, uint32_t height= 0,
                                   glm::vec2 offset = glm::vec2(0, 0)):width(width), height(height),
-                                                                              offset(offset) {};
+                                                                              offset(offset)
+   {
+        this->isColliding = false;
+        this->collidingList.clear();
+   };
 };
 
 #endif /* GAMEENGINE_BOXCOLLIDER_H */

+ 1 - 0
source/include/Game.h

@@ -23,6 +23,7 @@ class Game
 {
 private:
     bool isRunning;
+    bool isDebug;
     uint32_t millisecsPerviousFrame;
     SDL_Window *window;
     SDL_Renderer *renderer;

+ 45 - 4
source/include/Systems/Collision.h

@@ -13,6 +13,8 @@
 #include <ECS.h>
 #include <Logger.h>
 
+#include <SDL.h>
+
 #include <Components/Transform.h>
 #include <Components/BoxCollider.h>
 
@@ -46,22 +48,61 @@ public:
     void update()
     {
         auto entities = this->getSystemEntities();
+        for(auto entity: entities)
+        {
+            auto &collider = entity.getComponent<BoxColliderComponent>();
+            collider.isColliding = false;
+            collider.collidingList.clear();
+        }
         for(auto i = entities.begin(); i != entities.end(); i++)
         {
             for(auto j = (i + 1); j != entities.end(); j++)
             {
-                BoxRect boxA = BoxRect(i->getComponent<TransformComponent>(),
-                                       i->getComponent<BoxColliderComponent>());
-                BoxRect boxB = BoxRect(j->getComponent<TransformComponent>(),
-                                       j->getComponent<BoxColliderComponent>());
+                auto &collideA = i->getComponent<BoxColliderComponent>();
+                auto &collideB = j->getComponent<BoxColliderComponent>();
+
+                BoxRect boxA = BoxRect(i->getComponent<TransformComponent>(), collideA);
+                BoxRect boxB = BoxRect(j->getComponent<TransformComponent>(), collideB);
 
                 if (boxA.collideWith(boxB))
                 {
                     Logger::Debug("Entity #%d collided with #%d!", i->getId(), j->getId());
+                    collideB.isColliding = true;
+                    collideA.isColliding = true;
+                    collideA.collidingList.push_back(*j);
+                    collideB.collidingList.push_back(*i);
                 }
             }
         }
     }
+
+    void debugRender(SDL_Renderer *renderer)
+    {
+        for(auto entity: this->getSystemEntities())
+        {
+            auto collider = entity.getComponent<BoxColliderComponent>();
+            auto transform = entity.getComponent<TransformComponent>();
+
+            SDL_Rect rect;
+            rect.x = static_cast<int>(transform.position.x + collider.offset.x);
+            rect.y = static_cast<int>(transform.position.y + collider.offset.y);
+            rect.w = static_cast<int>(collider.width);
+            rect.h = static_cast<int>(collider.height);
+
+            if (collider.isColliding)
+            {
+                SDL_SetRenderDrawColor(renderer, 255, 64, 16, 255);
+            }
+            else
+            {
+                SDL_SetRenderDrawColor(renderer, 64, 255, 21, 255);
+            }
+
+            SDL_RenderDrawRect(renderer, &rect);
+
+
+        }
+    }
 };
 
 #endif /* GAMEENGINE_COLLISION_H */