Browse Source

Lesson 21.3

Godzil 3 years ago
parent
commit
80b5916977
3 changed files with 106 additions and 3 deletions
  1. 5 1
      source/Game.cpp
  2. 98 0
      source/include/Systems/RenderHealth.h
  3. 3 2
      source/include/Systems/RenderText.h

+ 5 - 1
source/Game.cpp

@@ -40,6 +40,7 @@
 #include <Systems/ProjectileEmit.h>
 #include <Systems/ProjectileLifeCycle.h>
 #include <Systems/RenderText.h>
+#include <Systems/RenderHealth.h>
 
 uint32_t Game::windowsWidth = 0;
 uint32_t Game::windowsHeight = 0;
@@ -155,6 +156,7 @@ void Game::LoadLevel(int level)
     this->registry->addSystem<ProjectileEmitSystem>();
     this->registry->addSystem<ProjectileLifeCycleSystem>();
     this->registry->addSystem<RenderTextSystem>();
+    this->registry->addSystem<RenderHealthSystem>();
 
     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");
@@ -165,7 +167,7 @@ void Game::LoadLevel(int level)
 
     this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
     this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
-    this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 20);
+    this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 10);
 
     Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
 
@@ -318,6 +320,8 @@ void Game::Render()
 
     registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
     registry->getSystem<RenderTextSystem>().update(this->renderer, this->assetStore, this->camera);
+    registry->getSystem<RenderHealthSystem>().update(this->renderer, this->assetStore, this->camera);
+
     if (this->isDebug)
     {
         registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);

+ 98 - 0
source/include/Systems/RenderHealth.h

@@ -0,0 +1,98 @@
+/*
+ * 2D Game Engine 
+ * RenderHealth.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 27/02/2021.
+ */
+
+#ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERHEALTH_H
+#define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERHEALTH_H
+
+
+#include <SDL.h>
+#include <SDL_ttf.h>
+
+#include <AssetStore.h>
+#include <ECS.h>
+#include <Components/Transform.h>
+#include <Components/Heakth.h>
+#include <Components/Sprite.h>
+
+class RenderHealthSystem: public System
+{
+public:
+    RenderHealthSystem()
+    {
+        this->requireComponent<TransformComponent>();
+        this->requireComponent<HealthComponent>();
+        this->requireComponent<SpriteComponent>();
+    }
+
+    void update(SDL_Renderer *renderer, std::unique_ptr<AssetStore> &assetStore, SDL_Rect &camera)
+    {
+        for(auto entity: this->getSystemEntities())
+        {
+            const auto health = entity.getComponent<HealthComponent>();
+            const auto transform = entity.getComponent<TransformComponent>();
+            const auto sprite = entity.getComponent<SpriteComponent>();
+
+            TTF_Font *font = assetStore->getFont("pico8-font");
+
+            char text[10];
+            sprintf(text, "%d%%", health.healthPercentage);
+
+            const SDL_Color green = {32, 255, 127, 255};
+            const SDL_Color orange = { 255, 199, 20, 255};
+            const SDL_Color red = {255, 12, 0, 255};
+
+            SDL_Color displayColor = green;
+
+            if (health.healthPercentage <= 80)
+            {
+               displayColor = orange;
+            }
+            if (health.healthPercentage <= 30)
+            {
+                displayColor = red;
+            }
+
+            SDL_Surface *surface = TTF_RenderText_Blended(font, text, displayColor);
+            SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
+
+            int labelWidth, labelHeight;
+
+            SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
+
+            SDL_Rect destRect =
+            {
+                static_cast<int>(transform.position.x - (sprite.isFixed?0.:camera.x) + (sprite.width * transform.scale.x) / 2),
+                static_cast<int>(transform.position.y - (sprite.isFixed?0.:camera.y) - 17),
+                    labelWidth, labelHeight
+            };
+            SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
+
+            SDL_SetRenderDrawColor(renderer, displayColor.r, displayColor.g, displayColor.b, displayColor.a);
+
+            SDL_Rect healthBar =
+            {
+                static_cast<int>(transform.position.x - (sprite.isFixed?0.:camera.x) + (sprite.width * transform.scale.x) / 2),
+                static_cast<int>(transform.position.y - (sprite.isFixed?0.:camera.y) - 5),
+                25,
+                5
+            };
+            SDL_RenderDrawRect(renderer, &healthBar);
+
+            healthBar.w = health.healthPercentage * 25 / 100;
+
+            SDL_RenderFillRect(renderer, &healthBar);
+
+            SDL_FreeSurface(surface);
+            SDL_DestroyTexture(texture);
+        }
+    }
+};
+
+
+#endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERHEALTH_H */

+ 3 - 2
source/include/Systems/RenderText.h

@@ -10,7 +10,6 @@
 #ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
 #define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
 
-
 #include <SDL.h>
 #include <SDL_ttf.h>
 
@@ -45,8 +44,10 @@ public:
                     static_cast<int>(textLabel.position.y - (textLabel.isFixed?0.:camera.y)),
                     labelWidth, labelHeight
                 };
-            SDL_FreeSurface(surface);
             SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
+
+            SDL_FreeSurface(surface);
+            SDL_DestroyTexture(texture);
         }
     }
 };