RenderHealth.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * 2D Game Engine
  3. * RenderHealth.h:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 27/02/2021.
  8. */
  9. #ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERHEALTH_H
  10. #define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERHEALTH_H
  11. #include <SDL.h>
  12. #include <SDL_ttf.h>
  13. #include <AssetStore.h>
  14. #include <ECS.h>
  15. #include <Components/Transform.h>
  16. #include <Components/Heakth.h>
  17. #include <Components/Sprite.h>
  18. class RenderHealthSystem: public System
  19. {
  20. public:
  21. RenderHealthSystem()
  22. {
  23. this->requireComponent<TransformComponent>();
  24. this->requireComponent<HealthComponent>();
  25. this->requireComponent<SpriteComponent>();
  26. }
  27. void update(SDL_Renderer *renderer, std::unique_ptr<AssetStore> &assetStore, SDL_Rect &camera)
  28. {
  29. for(auto entity: this->getSystemEntities())
  30. {
  31. const auto health = entity.getComponent<HealthComponent>();
  32. const auto transform = entity.getComponent<TransformComponent>();
  33. const auto sprite = entity.getComponent<SpriteComponent>();
  34. TTF_Font *font = assetStore->getFont("pico8-font-5");
  35. char text[10];
  36. sprintf(text, "%d%%", health.healthPercentage);
  37. const SDL_Color green = {32, 255, 127, 255};
  38. const SDL_Color orange = { 255, 199, 20, 255};
  39. const SDL_Color red = {255, 12, 0, 255};
  40. SDL_Color displayColor = green;
  41. if (health.healthPercentage <= 80)
  42. {
  43. displayColor = orange;
  44. }
  45. if (health.healthPercentage <= 30)
  46. {
  47. displayColor = red;
  48. }
  49. SDL_Surface *surface = TTF_RenderText_Blended(font, text, displayColor);
  50. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  51. int labelWidth, labelHeight;
  52. SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
  53. SDL_Rect destRect =
  54. {
  55. static_cast<int>(transform.position.x - (sprite.isFixed?0.:camera.x) + (sprite.width * transform.scale.x) / 2),
  56. static_cast<int>(transform.position.y - (sprite.isFixed?0.:camera.y) - 17),
  57. labelWidth, labelHeight
  58. };
  59. SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
  60. SDL_SetRenderDrawColor(renderer, displayColor.r, displayColor.g, displayColor.b, displayColor.a);
  61. SDL_Rect healthBar =
  62. {
  63. static_cast<int>(transform.position.x - (sprite.isFixed?0.:camera.x) + (sprite.width * transform.scale.x) / 2),
  64. static_cast<int>(transform.position.y - (sprite.isFixed?0.:camera.y) - 5),
  65. 25,
  66. 5
  67. };
  68. SDL_RenderDrawRect(renderer, &healthBar);
  69. healthBar.w = health.healthPercentage * 25 / 100;
  70. SDL_RenderFillRect(renderer, &healthBar);
  71. SDL_FreeSurface(surface);
  72. SDL_DestroyTexture(texture);
  73. }
  74. }
  75. };
  76. #endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERHEALTH_H */