Godzil %!s(int64=3) %!d(string=hai) anos
pai
achega
38b88f470b

+ 8 - 1
source/Game.cpp

@@ -28,6 +28,7 @@
 #include <Systems/Render.h>
 #include <Systems/Animation.h>
 #include <Systems/Collision.h>
+#include <Systems/Damage.h>
 
 Game::Game()
 {
@@ -38,6 +39,7 @@ Game::Game()
 
     this->registry = std::make_unique<Registry>();
     this->assetStore = std::make_unique<AssetStore>();
+    this->eventBus = std::make_unique<EventBus>();
 }
 
 Game::~Game()
@@ -112,6 +114,7 @@ void Game::LoadLevel(int level)
     this->registry->addSystem<RenderSystem>();
     this->registry->addSystem<AnimationSystem>();
     this->registry->addSystem<CollisionSystem>();
+    this->registry->addSystem<DamageSystem>();
 
     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");
@@ -191,9 +194,13 @@ void Game::Update()
 
     this->millisecsPerviousFrame = SDL_GetTicks();
 
+    eventBus->reset();
+
+    registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
+
     registry->getSystem<MovementSystem>().update(deltaTime);
     registry->getSystem<AnimationSystem>().update();
-    registry->getSystem<CollisionSystem>().update();
+    registry->getSystem<CollisionSystem>().update(this->eventBus);
 
     registry->update();
 }

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

@@ -21,14 +21,12 @@ struct BoxColliderComponent
     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)
    {
         this->isColliding = false;
-        this->collidingList.clear();
    };
 };
 

+ 7 - 2
source/include/EventBus.h

@@ -61,7 +61,12 @@ public:
     EventBus() = default;
     ~EventBus() = default;
 
-    template<typename TEvent, typename TOwner>void SubscribeToEvent(TOwner *ownerInstance, void (TOwner::*callbackFunction)(TEvent &))
+    void reset()
+    {
+        this->subscribers.clear();
+    }
+
+    template<typename TEvent, typename TOwner>void subscribeToEvent(TOwner *ownerInstance, void (TOwner::*callbackFunction)(TEvent &))
     {
         auto subscriber = std::make_unique<EventCallback<TOwner, TEvent>>(ownerInstance, callbackFunction);
 
@@ -73,7 +78,7 @@ public:
         this->subscribers[typeid(TEvent)]->push_back(std::move(subscriber));
     }
 
-    template<typename T, typename ...TArgs> void EmitEvent(TArgs &&...args)
+    template<typename T, typename ...TArgs> void emitEvent(TArgs &&...args)
     {
         auto handlers = this->subscribers[typeid(T)].get();
         if (handlers)

+ 7 - 8
source/include/Systems/Collision.h

@@ -10,13 +10,15 @@
 #ifndef GAMEENGINE_COLLISION_H
 #define GAMEENGINE_COLLISION_H
 
+#include <SDL.h>
+
 #include <ECS.h>
 #include <Logger.h>
-
-#include <SDL.h>
+#include <EventBus.h>
 
 #include <Components/Transform.h>
 #include <Components/BoxCollider.h>
+#include <Events/Collision.h>
 
 class CollisionSystem: public System
 {
@@ -45,14 +47,13 @@ public:
         this->requireComponent<BoxColliderComponent>();
     }
 
-    void update()
+    void update(std::unique_ptr<EventBus> &eventBus)
     {
         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++)
         {
@@ -67,13 +68,11 @@ public:
                 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);
 
-                    i->kill();
-                    j->kill();
+                    eventBus->emitEvent<CollisionEvent>(*i, *j);
                 }
             }
         }

+ 13 - 0
source/include/Systems/Damage.h

@@ -0,0 +1,13 @@
+/*
+ * 2D Game Engine 
+ * Damage.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 16/02/2021.
+ */
+
+#ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_DAMAGE_H
+#define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_DAMAGE_H
+
+#endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_DAMAGE_H */