unit_layer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <QImage>
  3. #include <cstdint>
  4. #include <functional>
  5. #include <vector>
  6. class QPainter;
  7. namespace Game::Map::Minimap {
  8. struct UnitMarker {
  9. float world_x = 0.0F;
  10. float world_z = 0.0F;
  11. int owner_id = 0;
  12. bool is_selected = false;
  13. bool is_building = false;
  14. };
  15. using VisibilityCheckFn = std::function<bool(float world_x, float world_z)>;
  16. using PlayerColorFn = std::function<bool(int owner_id, std::uint8_t &r,
  17. std::uint8_t &g, std::uint8_t &b)>;
  18. struct TeamColors {
  19. struct ColorSet {
  20. std::uint8_t r, g, b;
  21. std::uint8_t border_r, border_g, border_b;
  22. };
  23. static constexpr ColorSet PLAYER_1 = {70, 100, 160, 35, 50, 80};
  24. static constexpr ColorSet PLAYER_2 = {180, 60, 50, 90, 30, 25};
  25. static constexpr ColorSet PLAYER_3 = {60, 130, 70, 30, 65, 35};
  26. static constexpr ColorSet PLAYER_4 = {190, 160, 60, 95, 80, 30};
  27. static constexpr ColorSet PLAYER_5 = {120, 60, 140, 60, 30, 70};
  28. static constexpr ColorSet PLAYER_6 = {60, 140, 140, 30, 70, 70};
  29. static constexpr ColorSet NEUTRAL = {100, 95, 85, 50, 48, 43};
  30. static constexpr std::uint8_t SELECT_R = 255;
  31. static constexpr std::uint8_t SELECT_G = 215;
  32. static constexpr std::uint8_t SELECT_B = 0;
  33. static constexpr auto get_color(int owner_id) -> ColorSet {
  34. switch (owner_id) {
  35. case 1:
  36. return PLAYER_1;
  37. case 2:
  38. return PLAYER_2;
  39. case 3:
  40. return PLAYER_3;
  41. case 4:
  42. return PLAYER_4;
  43. case 5:
  44. return PLAYER_5;
  45. case 6:
  46. return PLAYER_6;
  47. default:
  48. return NEUTRAL;
  49. }
  50. }
  51. };
  52. class UnitLayer {
  53. public:
  54. UnitLayer() = default;
  55. void init(int width, int height, float world_width, float world_height);
  56. [[nodiscard]] auto is_initialized() const -> bool {
  57. return !m_image.isNull();
  58. }
  59. void update(const std::vector<UnitMarker> &markers);
  60. void update(const std::vector<UnitMarker> &markers, int local_owner_id,
  61. const VisibilityCheckFn &visibility_check,
  62. const PlayerColorFn &player_color_fn = nullptr);
  63. [[nodiscard]] auto get_image() const -> const QImage & { return m_image; }
  64. void set_unit_radius(float radius) { m_unit_radius = radius; }
  65. void set_building_size(float size) { m_building_half_size = size; }
  66. private:
  67. [[nodiscard]] auto
  68. world_to_pixel(float world_x, float world_z) const -> std::pair<float, float>;
  69. [[nodiscard]] auto get_color_for_owner(int owner_id,
  70. const PlayerColorFn &player_color_fn)
  71. -> TeamColors::ColorSet;
  72. void draw_unit_marker(QPainter &painter, float px, float py,
  73. const TeamColors::ColorSet &colors, bool is_selected);
  74. void draw_building_marker(QPainter &painter, float px, float py,
  75. const TeamColors::ColorSet &colors,
  76. bool is_selected);
  77. QImage m_image;
  78. int m_width = 0;
  79. int m_height = 0;
  80. float m_world_width = 0.0F;
  81. float m_world_height = 0.0F;
  82. float m_unit_radius = 3.0F;
  83. float m_building_half_size = 5.0F;
  84. float m_scale_x = 1.0F;
  85. float m_scale_y = 1.0F;
  86. float m_offset_x = 0.0F;
  87. float m_offset_y = 0.0F;
  88. };
  89. } // namespace Game::Map::Minimap