home_renderer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "home_renderer.h"
  2. #include "../../../../game/core/component.h"
  3. #include "../../../../game/visuals/team_colors.h"
  4. #include "../../../geom/math_utils.h"
  5. #include "../../../geom/transforms.h"
  6. #include "../../../gl/primitives.h"
  7. #include "../../../gl/resources.h"
  8. #include "../../../submitter.h"
  9. #include "../../building_state.h"
  10. #include "../../registry.h"
  11. #include <QMatrix4x4>
  12. #include <QVector3D>
  13. #include <algorithm>
  14. #include <cmath>
  15. namespace Render::GL::Carthage {
  16. namespace {
  17. using Render::Geom::clamp01;
  18. using Render::Geom::clamp_vec_01;
  19. using Render::Geom::cylinder_between;
  20. struct CarthagePalette {
  21. QVector3D stone_light{0.62F, 0.60F, 0.58F};
  22. QVector3D stone_dark{0.50F, 0.48F, 0.46F};
  23. QVector3D stone_base{0.55F, 0.53F, 0.51F};
  24. QVector3D brick{0.75F, 0.52F, 0.42F};
  25. QVector3D brick_dark{0.62F, 0.42F, 0.32F};
  26. QVector3D tile_red{0.72F, 0.40F, 0.30F};
  27. QVector3D tile_dark{0.58F, 0.30F, 0.22F};
  28. QVector3D wood{0.42F, 0.28F, 0.16F};
  29. QVector3D wood_dark{0.32F, 0.20F, 0.10F};
  30. QVector3D team{0.8F, 0.9F, 1.0F};
  31. QVector3D team_trim{0.48F, 0.54F, 0.60F};
  32. };
  33. inline auto make_palette(const QVector3D &team) -> CarthagePalette {
  34. CarthagePalette p;
  35. p.team = clamp_vec_01(team);
  36. p.team_trim = clamp_vec_01(
  37. QVector3D(team.x() * 0.6F, team.y() * 0.6F, team.z() * 0.6F));
  38. return p;
  39. }
  40. inline void draw_box(ISubmitter &out, Mesh *unit, Texture *white,
  41. const QMatrix4x4 &model, const QVector3D &pos,
  42. const QVector3D &size, const QVector3D &color) {
  43. QMatrix4x4 m = model;
  44. m.translate(pos);
  45. m.scale(size);
  46. out.mesh(unit, m, color, white, 1.0F);
  47. }
  48. inline void draw_cyl(ISubmitter &out, const QMatrix4x4 &model,
  49. const QVector3D &a, const QVector3D &b, float r,
  50. const QVector3D &color, Texture *white) {
  51. out.mesh(get_unit_cylinder(), model * cylinder_between(a, b, r), color, white,
  52. 1.0F);
  53. }
  54. void drawHomeBase(const DrawContext &p, ISubmitter &out, Mesh *unit,
  55. Texture *white, const CarthagePalette &c) {
  56. draw_box(out, unit, white, p.model, QVector3D(0.0F, 0.1F, 0.0F),
  57. QVector3D(1.0F, 0.1F, 1.0F), c.stone_base);
  58. }
  59. void drawHomeWalls(const DrawContext &p, ISubmitter &out, Mesh *unit,
  60. Texture *white, const CarthagePalette &c,
  61. BuildingState state) {
  62. float const wall_height = 0.8F;
  63. float height_multiplier = 1.0F;
  64. if (state == BuildingState::Damaged) {
  65. height_multiplier = 0.7F;
  66. } else if (state == BuildingState::Destroyed) {
  67. height_multiplier = 0.4F;
  68. }
  69. draw_box(
  70. out, unit, white, p.model,
  71. QVector3D(0.0F, wall_height * 0.5F * height_multiplier + 0.2F, -0.9F),
  72. QVector3D(0.85F, wall_height * 0.5F * height_multiplier, 0.08F), c.brick);
  73. draw_box(out, unit, white, p.model,
  74. QVector3D(0.0F, wall_height * 0.5F * height_multiplier + 0.2F, 0.9F),
  75. QVector3D(0.85F, wall_height * 0.5F * height_multiplier, 0.08F),
  76. c.brick);
  77. draw_box(
  78. out, unit, white, p.model,
  79. QVector3D(-0.9F, wall_height * 0.5F * height_multiplier + 0.2F, 0.0F),
  80. QVector3D(0.08F, wall_height * 0.5F * height_multiplier, 0.8F), c.brick);
  81. draw_box(out, unit, white, p.model,
  82. QVector3D(0.9F, wall_height * 0.5F * height_multiplier + 0.2F, 0.0F),
  83. QVector3D(0.08F, wall_height * 0.5F * height_multiplier, 0.8F),
  84. c.brick);
  85. }
  86. void drawHomeRoof(const DrawContext &p, ISubmitter &out, Mesh *unit,
  87. Texture *white, const CarthagePalette &c,
  88. BuildingState state) {
  89. if (state == BuildingState::Destroyed) {
  90. return;
  91. }
  92. draw_box(out, unit, white, p.model, QVector3D(0.0F, 1.15F, 0.0F),
  93. QVector3D(1.0F, 0.05F, 1.0F), c.tile_red);
  94. for (float z = -0.8F; z <= 0.8F; z += 0.3F) {
  95. draw_box(out, unit, white, p.model, QVector3D(0.0F, 1.18F, z),
  96. QVector3D(0.95F, 0.02F, 0.06F), c.tile_dark);
  97. }
  98. }
  99. void drawHomeDoor(const DrawContext &p, ISubmitter &out, Mesh *unit,
  100. Texture *white, const CarthagePalette &c) {
  101. draw_box(out, unit, white, p.model, QVector3D(0.0F, 0.4F, 0.95F),
  102. QVector3D(0.3F, 0.4F, 0.05F), c.wood_dark);
  103. }
  104. void draw_health_bar(const DrawContext &p, ISubmitter &out, Mesh *unit,
  105. Texture *white) {
  106. if (p.entity == nullptr) {
  107. return;
  108. }
  109. auto *u = p.entity->get_component<Engine::Core::UnitComponent>();
  110. if (u == nullptr) {
  111. return;
  112. }
  113. float const ratio =
  114. std::clamp(u->health / float(std::max(1, u->max_health)), 0.0F, 1.0F);
  115. if (ratio <= 0.0F) {
  116. return;
  117. }
  118. auto *capture = p.entity->get_component<Engine::Core::CaptureComponent>();
  119. bool under_attack = (capture != nullptr && capture->is_being_captured);
  120. if (!under_attack && u->health >= u->max_health) {
  121. return;
  122. }
  123. float const bar_width = 1.0F;
  124. float const bar_height = 0.08F;
  125. float const bar_y = 1.5F;
  126. float const border_thickness = 0.012F;
  127. if (under_attack) {
  128. float pulse = HEALTHBAR_PULSE_MIN +
  129. HEALTHBAR_PULSE_AMPLITUDE *
  130. sinf(p.animation_time * HEALTHBAR_PULSE_SPEED);
  131. draw_box(out, unit, white, p.model, QVector3D(0.0F, bar_y, 0.0F),
  132. QVector3D(bar_width * 0.5F + border_thickness * 3.0F,
  133. bar_height * 0.5F + border_thickness * 3.0F, 0.095F),
  134. HealthBarColors::GLOW_ATTACK * pulse * 0.6F);
  135. }
  136. draw_box(out, unit, white, p.model, QVector3D(0.0F, bar_y, 0.0F),
  137. QVector3D(bar_width * 0.5F + border_thickness,
  138. bar_height * 0.5F + border_thickness, 0.09F),
  139. HealthBarColors::BORDER);
  140. draw_box(out, unit, white, p.model, QVector3D(0.0F, bar_y, 0.0F),
  141. QVector3D(bar_width * 0.5F + border_thickness * 0.5F,
  142. bar_height * 0.5F + border_thickness * 0.5F, 0.088F),
  143. HealthBarColors::INNER_BORDER);
  144. draw_box(out, unit, white, p.model, QVector3D(0.0F, bar_y + 0.003F, 0.0F),
  145. QVector3D(bar_width * 0.5F, bar_height * 0.5F, 0.085F),
  146. HealthBarColors::BACKGROUND);
  147. QVector3D fg_color;
  148. QVector3D fg_dark;
  149. if (ratio >= HEALTH_THRESHOLD_NORMAL) {
  150. fg_color = HealthBarColors::NORMAL_BRIGHT;
  151. fg_dark = HealthBarColors::NORMAL_DARK;
  152. } else if (ratio >= HEALTH_THRESHOLD_DAMAGED) {
  153. float t = (ratio - HEALTH_THRESHOLD_DAMAGED) /
  154. (HEALTH_THRESHOLD_NORMAL - HEALTH_THRESHOLD_DAMAGED);
  155. fg_color = HealthBarColors::NORMAL_BRIGHT * t +
  156. HealthBarColors::DAMAGED_BRIGHT * (1.0F - t);
  157. fg_dark = HealthBarColors::NORMAL_DARK * t +
  158. HealthBarColors::DAMAGED_DARK * (1.0F - t);
  159. } else {
  160. float t = ratio / HEALTH_THRESHOLD_DAMAGED;
  161. fg_color = HealthBarColors::DAMAGED_BRIGHT * t +
  162. HealthBarColors::CRITICAL_BRIGHT * (1.0F - t);
  163. fg_dark = HealthBarColors::DAMAGED_DARK * t +
  164. HealthBarColors::CRITICAL_DARK * (1.0F - t);
  165. }
  166. draw_box(
  167. out, unit, white, p.model,
  168. QVector3D(-(bar_width * (1.0F - ratio)) * 0.5F, bar_y + 0.005F, 0.0F),
  169. QVector3D(bar_width * ratio * 0.5F, bar_height * 0.48F, 0.08F), fg_dark);
  170. draw_box(
  171. out, unit, white, p.model,
  172. QVector3D(-(bar_width * (1.0F - ratio)) * 0.5F, bar_y + 0.008F, 0.0F),
  173. QVector3D(bar_width * ratio * 0.5F, bar_height * 0.40F, 0.078F),
  174. fg_color);
  175. QVector3D const highlight = fg_color * 1.6F;
  176. draw_box(out, unit, white, p.model,
  177. QVector3D(-(bar_width * (1.0F - ratio)) * 0.5F,
  178. bar_y + bar_height * 0.35F, 0.0F),
  179. QVector3D(bar_width * ratio * 0.5F, bar_height * 0.20F, 0.075F),
  180. clamp_vec_01(highlight));
  181. draw_box(out, unit, white, p.model,
  182. QVector3D(-(bar_width * (1.0F - ratio)) * 0.5F,
  183. bar_y + bar_height * 0.48F, 0.0F),
  184. QVector3D(bar_width * ratio * 0.5F, bar_height * 0.08F, 0.073F),
  185. HealthBarColors::SHINE * 0.8F);
  186. float marker_70_x = bar_width * 0.5F * (HEALTH_THRESHOLD_NORMAL - 0.5F);
  187. draw_box(out, unit, white, p.model, QVector3D(marker_70_x, bar_y, 0.0F),
  188. QVector3D(0.015F, bar_height * 0.55F, 0.09F),
  189. HealthBarColors::SEGMENT);
  190. float marker_30_x = bar_width * 0.5F * (HEALTH_THRESHOLD_DAMAGED - 0.5F);
  191. draw_box(out, unit, white, p.model, QVector3D(marker_30_x, bar_y, 0.0F),
  192. QVector3D(0.015F, bar_height * 0.55F, 0.09F),
  193. HealthBarColors::SEGMENT);
  194. }
  195. void draw_selection(const DrawContext &p, ISubmitter &out) {
  196. QMatrix4x4 m;
  197. QVector3D const pos = p.model.column(3).toVector3D();
  198. m.translate(pos.x(), 0.0F, pos.z());
  199. m.scale(1.4F, 1.0F, 1.4F);
  200. if (p.selected) {
  201. out.selection_smoke(m, QVector3D(0.2F, 0.85F, 0.2F), 0.35F);
  202. } else if (p.hovered) {
  203. out.selection_smoke(m, QVector3D(0.95F, 0.92F, 0.25F), 0.22F);
  204. }
  205. }
  206. void draw_home(const DrawContext &p, ISubmitter &out) {
  207. if (!p.resources || !p.entity) {
  208. return;
  209. }
  210. auto *t = p.entity->get_component<Engine::Core::TransformComponent>();
  211. auto *r = p.entity->get_component<Engine::Core::RenderableComponent>();
  212. auto *u = p.entity->get_component<Engine::Core::UnitComponent>();
  213. if (!t || !r) {
  214. return;
  215. }
  216. BuildingState state = BuildingState::Normal;
  217. if (u != nullptr) {
  218. float const health_ratio =
  219. std::clamp(u->health / float(std::max(1, u->max_health)), 0.0F, 1.0F);
  220. state = get_building_state(health_ratio);
  221. }
  222. Mesh *unit = p.resources->unit();
  223. Texture *white = p.resources->white();
  224. QVector3D const team(r->color[0], r->color[1], r->color[2]);
  225. CarthagePalette const c = make_palette(team);
  226. drawHomeBase(p, out, unit, white, c);
  227. drawHomeWalls(p, out, unit, white, c, state);
  228. drawHomeRoof(p, out, unit, white, c, state);
  229. drawHomeDoor(p, out, unit, white, c);
  230. draw_health_bar(p, out, unit, white);
  231. draw_selection(p, out);
  232. }
  233. } // namespace
  234. void register_home_renderer(Render::GL::EntityRendererRegistry &registry) {
  235. registry.register_renderer("troops/carthage/home", draw_home);
  236. }
  237. } // namespace Render::GL::Carthage