Godzil 3 gadi atpakaļ
vecāks
revīzija
845cb36a62

+ 9 - 1
source/Game.cpp

@@ -6,6 +6,7 @@
  *
  * Created by Manoël Trapier on 09/02/2021.
  */
+
 #include <SDL.h>
 #include <SDL_image.h>
 #include <glm/glm.hpp>
@@ -29,6 +30,7 @@
 #include <Systems/Animation.h>
 #include <Systems/Collision.h>
 #include <Systems/Damage.h>
+#include <Systems/KeyboardMovement.h>
 
 Game::Game()
 {
@@ -115,6 +117,7 @@ void Game::LoadLevel(int level)
     this->registry->addSystem<AnimationSystem>();
     this->registry->addSystem<CollisionSystem>();
     this->registry->addSystem<DamageSystem>();
+    this->registry->addSystem<KeyboardMovementSystem>();
 
     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");
@@ -197,6 +200,7 @@ void Game::Update()
     eventBus->reset();
 
     registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
+    registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
 
     registry->getSystem<MovementSystem>().update(deltaTime);
     registry->getSystem<AnimationSystem>().update();
@@ -222,10 +226,14 @@ void Game::ProcessInput()
                 {
                     this->isRunning = false;
                 }
-                if (event.key.keysym.sym == SDLK_d)
+                else if (event.key.keysym.sym == SDLK_d)
                 {
                     this->isDebug = !this->isDebug;
                 }
+                else
+                {
+                    this->eventBus->emitEvent<KeyPressedEvent>(event.key.keysym.sym);
+                }
                 break;
 
 

+ 0 - 1
source/include/Events/Collision.h

@@ -10,7 +10,6 @@
 #ifndef GAMEENGINE_SOURCE_INCLUDE_EVENTS_COLLISION_H
 #define GAMEENGINE_SOURCE_INCLUDE_EVENTS_COLLISION_H
 
-#include <ECS.h>
 #include <Event.h>
 
 class CollisionEvent : public Event

+ 24 - 0
source/include/Events/KeyPressed.h

@@ -0,0 +1,24 @@
+/*
+ * 2D Game Engine 
+ * KeyPressed.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_EVENTS_KEYPRESSED_H
+#define GAMEENGINE_SOURCE_INCLUDE_EVENTS_KEYPRESSED_H
+
+#include <stdint.h>
+
+#include <Event.h>
+
+class KeyPressedEvent : public Event
+{
+public:
+    uint16_t keyCode;
+    explicit KeyPressedEvent(uint16_t keyCode) : keyCode(keyCode) {};
+};
+
+#endif /* GAMEENGINE_SOURCE_INCLUDE_EVENTS_KEYPRESSED_H */

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

@@ -10,4 +10,38 @@
 #ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_DAMAGE_H
 #define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_DAMAGE_H
 
+#include <ECS.h>
+#include <EventBus.h>
+
+#include <Logger.h>
+
+#include <Events/Collision.h>
+#include <Components/BoxCollider.h>
+
+class DamageSystem : public System
+{
+public:
+    DamageSystem()
+    {
+        this->requireComponent<BoxColliderComponent>();
+    }
+
+    void subscriptToEvents(std::unique_ptr<EventBus> &eventBus)
+    {
+        eventBus->subscribeToEvent<CollisionEvent>(this, &DamageSystem::onCollision);
+    }
+
+    void onCollision(CollisionEvent &event)
+    {
+        Logger::Debug("Damage: Got an event from A id#%d and B id#%d", event.a.getId(), event.b.getId());
+        event.a.kill();
+        event.b.kill();
+    }
+
+    void Update()
+    {
+
+    }
+};
+
 #endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_DAMAGE_H */

+ 41 - 0
source/include/Systems/KeyboardMovement.h

@@ -0,0 +1,41 @@
+/*
+ * 2D Game Engine 
+ * KeyboardMovement.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_KEYBOARDMOVEMENT_H
+#define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_KEYBOARDMOVEMENT_H
+
+#include <ECS.h>
+#include <Events/KeyPressed.h>
+
+class KeyboardMovementSystem : public System
+{
+public:
+    KeyboardMovementSystem()
+    {
+        /* No component to register */
+    }
+
+    void subscriptToEvents(std::unique_ptr<EventBus> &eventBus)
+    {
+        eventBus->subscribeToEvent<KeyPressedEvent>(this, &KeyboardMovementSystem::onKeyPressed);
+    }
+
+    void onKeyPressed(KeyPressedEvent &event)
+    {
+        Logger::Debug("Keyboard: the key '%c' was pressed", event.keyCode);
+    }
+
+    void Update()
+    {
+
+    }
+
+};
+
+#endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_KEYBOARDMOVEMENT_H */