|
@@ -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 */
|