Godzil 3 gadi atpakaļ
vecāks
revīzija
eee42b85fb

+ 11 - 5
source/Game.cpp

@@ -22,10 +22,12 @@
 #include <Components/Transform.h>
 #include <Components/RigidBody.h>
 #include <Components/Sprite.h>
+#include <Components/BoxCollider.h>
 
 #include <Systems/Movement.h>
 #include <Systems/Render.h>
 #include <Systems/Animation.h>
+#include <Systems/Collision.h>
 
 Game::Game()
 {
@@ -108,6 +110,7 @@ void Game::LoadLevel(int level)
     this->registry->addSystem<MovementSystem>();
     this->registry->addSystem<RenderSystem>();
     this->registry->addSystem<AnimationSystem>();
+    this->registry->addSystem<CollisionSystem>();
 
     this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
     this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
@@ -147,7 +150,7 @@ void Game::LoadLevel(int level)
     mapFile.close();
 
     Entity chopper = this->registry->createEntity();
-    chopper.addComponent<TransformComponent>(glm::vec2(10, 10), glm::vec2(1, 1), 0);
+    chopper.addComponent<TransformComponent>(glm::vec2(10, 100), glm::vec2(1, 1), 0);
     chopper.addComponent<SpriteComponent>("chopper-image", 2, 32, 32);
     chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
     chopper.addComponent<AnimationComponent>(2, 15, true);
@@ -159,14 +162,16 @@ void Game::LoadLevel(int level)
     radar.addComponent<AnimationComponent>(8, 5, true);
 
     Entity tank = this->registry->createEntity();
-    tank.addComponent<TransformComponent>(glm::vec2(10, 50), glm::vec2(1, 1), 0);
-    tank.addComponent<RigidBodyComponent>(glm::vec2(20, 0));
+    tank.addComponent<TransformComponent>(glm::vec2(500, 10), glm::vec2(1, 1), 0);
+    tank.addComponent<RigidBodyComponent>(glm::vec2(-30, 0));
     tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
+    tank.addComponent<BoxColliderComponent>(32, 32, glm::vec2(0, 0));
 
     Entity truck = this->registry->createEntity();
-    truck.addComponent<TransformComponent>(glm::vec2(10, 100), glm::vec2(1, 1), 0);
-    truck.addComponent<RigidBodyComponent>(glm::vec2(25, 0));
+    truck.addComponent<TransformComponent>(glm::vec2(10, 10), glm::vec2(1, 1), 0);
+    truck.addComponent<RigidBodyComponent>(glm::vec2(20, 0));
     truck.addComponent<SpriteComponent>("truck-image", 1, 32, 32);
+    truck.addComponent<BoxColliderComponent>(32, 32, glm::vec2(0, 0));
 }
 
 void Game::Setup()
@@ -187,6 +192,7 @@ void Game::Update()
 
     registry->getSystem<MovementSystem>().update(deltaTime);
     registry->getSystem<AnimationSystem>().update();
+    registry->getSystem<CollisionSystem>().update();
 
     registry->update();
 }

+ 27 - 0
source/include/Components/BoxCollider.h

@@ -0,0 +1,27 @@
+/*
+ * 2D Game Engine 
+ * BoxCollider.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 15/02/2021.
+ */
+
+#ifndef GAMEENGINE_BOXCOLLIDER_H
+#define GAMEENGINE_BOXCOLLIDER_H
+
+#include <stdint.h>
+
+#include <glm/glm.hpp>
+
+struct BoxColliderComponent
+{
+    uint32_t width;
+    uint32_t height;
+    glm::vec2 offset;
+    explicit BoxColliderComponent(uint32_t width = 0, uint32_t height= 0,
+                                  glm::vec2 offset = glm::vec2(0, 0)):width(width), height(height),
+                                                                              offset(offset) {};
+};
+
+#endif /* GAMEENGINE_BOXCOLLIDER_H */

+ 73 - 0
source/include/Systems/Collision.h

@@ -0,0 +1,73 @@
+/*
+ * 2D Game Engine 
+ * Collision.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 15/02/2021.
+ */
+
+#ifndef GAMEENGINE_COLLISION_H
+#define GAMEENGINE_COLLISION_H
+
+#include <ECS.h>
+#include <Logger.h>
+
+#include <Components/Transform.h>
+#include <Components/BoxCollider.h>
+
+class BoxRect
+{
+    int32_t 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) )
+        {
+            return true;
+        }
+        return false;
+    }
+};
+
+class CollisionSystem: public System
+{
+public:
+    CollisionSystem()
+    {
+        this->requireComponent<TransformComponent>();
+        this->requireComponent<BoxColliderComponent>();
+    }
+
+    void update()
+    {
+        auto entities = this->getSystemEntities();
+        for(auto entityA: this->getSystemEntities())
+        {
+            for(auto entityB: this->getSystemEntities())
+            {
+                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>());
+
+                if (boxA.collideWith(boxB))
+                {
+                    Logger::Debug("Entityty #%d collided with #%d!", entityA.getId(), entityB.getId());
+                }
+            }
+        }
+    }
+};
+
+#endif /* GAMEENGINE_COLLISION_H */