瀏覽代碼

Lesson 12.3

Godzil 3 年之前
父節點
當前提交
5f48a4c6b1
共有 4 個文件被更改,包括 82 次插入4 次删除
  1. 12 1
      source/Game.cpp
  2. 22 0
      source/include/Components/Sprite.h
  3. 0 3
      source/include/Systems/Movement.h
  4. 48 0
      source/include/Systems/Render.h

+ 12 - 1
source/Game.cpp

@@ -18,8 +18,10 @@
 
 #include <Components/Transform.h>
 #include <Components/RigidBody.h>
+#include <Components/Sprite.h>
 
 #include <Systems/Movement.h>
+#include <Systems/Render.h>
 
 Game::Game()
 {
@@ -99,11 +101,19 @@ void Game::Run()
 void Game::Setup()
 {
     this->registry->addSystem<MovementSystem>();
+    this->registry->addSystem<RenderSystem>();
 
     Entity tank = this->registry->createEntity();
 
     tank.addComponent<TransformComponent>(glm::vec2(10, 30), glm::vec2(1, 1), 0);
-    tank.addComponent<RigidBodyComponent>(glm::vec2(50, 0));
+    tank.addComponent<RigidBodyComponent>(glm::vec2(40, 0));
+    tank.addComponent<SpriteComponent>(10, 10);
+
+    Entity truck = this->registry->createEntity();
+
+    truck.addComponent<TransformComponent>(glm::vec2(50, 100), glm::vec2(1, 1), 0);
+    truck.addComponent<RigidBodyComponent>(glm::vec2(0, 50));
+    truck.addComponent<SpriteComponent>(10, 50);
 }
 
 void Game::Update()
@@ -153,6 +163,7 @@ void Game::Render()
     SDL_RenderClear(this->renderer);
 
     // Do something here
+    registry->getSystem<RenderSystem>().update(this->renderer);
 
     SDL_RenderPresent(this->renderer);
 }

+ 22 - 0
source/include/Components/Sprite.h

@@ -0,0 +1,22 @@
+/*
+ * 2D Game Engine 
+ * Sprite.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 12/02/2021.
+ */
+
+#ifndef GAMEENGINE_SPRITE_H
+#define GAMEENGINE_SPRITE_H
+
+struct SpriteComponent
+{
+    uint32_t width;
+    uint32_t height;
+
+    explicit SpriteComponent(uint32_t width = 0, uint32_t height = 0):width(width), height(height) {};
+};
+
+
+#endif /* GAMEENGINE_SPRITE_H */

+ 0 - 3
source/include/Systems/Movement.h

@@ -34,9 +34,6 @@ public:
 
             transform.position.x += rigidbody.velocity.x * deltaTime;
             transform.position.y += rigidbody.velocity.y * deltaTime;
-
-            Logger::Debug("Entity id#%d - Position is now (%f, %f)",
-                          entity.getId(), transform.position.x, transform.position.y);
         }
     }
 };

+ 48 - 0
source/include/Systems/Render.h

@@ -0,0 +1,48 @@
+/*
+ * 2D Game Engine 
+ * Render.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 12/02/2021.
+ */
+
+#ifndef GAMEENGINE_RENDER_H
+#define GAMEENGINE_RENDER_H
+
+#include <SDL.h>
+
+#include <ECS.h>
+#include <Components/Transform.h>
+#include <Components/Sprite.h>
+
+class RenderSystem: public System
+{
+public:
+    RenderSystem()
+    {
+        this->requireComponent<TransformComponent>();
+        this->requireComponent<SpriteComponent>();
+    }
+
+    void update(SDL_Renderer *renderer)
+    {
+        for (auto entity: this->getSystemEntities())
+        {
+            const auto tranform = entity.getComponent<TransformComponent>();
+            const auto sprite = entity.getComponent<SpriteComponent>();
+
+            SDL_Rect objRect =
+            {
+                static_cast<int>(tranform.position.x), static_cast<int>(tranform.position.y),
+                static_cast<int>(sprite.width), static_cast<int>(sprite.height)
+            };
+
+            SDL_SetRenderDrawColor(renderer, 255, 127, 64, 255);
+            SDL_RenderFillRect(renderer, &objRect);
+
+        }
+    }
+};
+
+#endif /* GAMEENGINE_RENDER_H */