player_status_hud.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/player_status_hud.hpp"
  17. #include "supertux/game_object.hpp"
  18. #include "supertux/player_status.hpp"
  19. #include "supertux/resources.hpp"
  20. #include "video/drawing_context.hpp"
  21. #include "video/surface.hpp"
  22. #include "editor/editor.hpp"
  23. static const int DISPLAYED_COINS_UNSET = -1;
  24. PlayerStatusHUD::PlayerStatusHUD(PlayerStatus& player_status) :
  25. m_player_status(player_status),
  26. displayed_coins(DISPLAYED_COINS_UNSET),
  27. displayed_coins_frame(0),
  28. coin_surface(Surface::from_file("images/engine/hud/coins-0.png")),
  29. fire_surface(Surface::from_file("images/engine/hud/fire-0.png")),
  30. ice_surface(Surface::from_file("images/engine/hud/ice-0.png"))
  31. {
  32. }
  33. void
  34. PlayerStatusHUD::reset()
  35. {
  36. displayed_coins = DISPLAYED_COINS_UNSET;
  37. }
  38. void
  39. PlayerStatusHUD::update(float dt_sec)
  40. {
  41. }
  42. void
  43. PlayerStatusHUD::draw(DrawingContext& context)
  44. {
  45. if (Editor::is_active())
  46. return;
  47. if ((displayed_coins == DISPLAYED_COINS_UNSET) ||
  48. (std::abs(displayed_coins - m_player_status.coins) > 100)) {
  49. displayed_coins = m_player_status.coins;
  50. displayed_coins_frame = 0;
  51. }
  52. if (++displayed_coins_frame > 2) {
  53. displayed_coins_frame = 0;
  54. if (displayed_coins < m_player_status.coins) displayed_coins++;
  55. if (displayed_coins > m_player_status.coins) displayed_coins--;
  56. }
  57. displayed_coins = std::min(std::max(displayed_coins, 0), m_player_status.get_max_coins());
  58. float hudpos = BORDER_Y + 1.0f;
  59. const std::string coins_text = std::to_string(displayed_coins);
  60. context.push_transform();
  61. context.set_translation(Vector(0, 0));
  62. context.transform().scale = 1.f;
  63. if (!Editor::is_active())
  64. {
  65. if (coin_surface)
  66. {
  67. context.color().draw_surface(coin_surface,
  68. Vector(context.get_width() - BORDER_X - static_cast<float>(coin_surface->get_width()) - Resources::fixed_font->get_text_width(coins_text),
  69. hudpos),
  70. LAYER_HUD);
  71. }
  72. context.color().draw_text(Resources::fixed_font,
  73. coins_text,
  74. Vector(static_cast<float>(context.get_width()) - BORDER_X - Resources::fixed_font->get_text_width(coins_text),
  75. hudpos + 13.f),
  76. ALIGN_LEFT,
  77. LAYER_HUD,
  78. PlayerStatusHUD::text_color);
  79. }
  80. hudpos += 8.f;
  81. for (int target = 0; target < InputManager::current()->get_num_users(); target++)
  82. {
  83. SurfacePtr surface;
  84. std::string ammo_text;
  85. if (m_player_status.bonus[target] == FIRE_BONUS)
  86. {
  87. surface = fire_surface;
  88. ammo_text = std::to_string(m_player_status.max_fire_bullets[target]);
  89. }
  90. else if (m_player_status.bonus[target] == ICE_BONUS)
  91. {
  92. surface = ice_surface;
  93. ammo_text = std::to_string(m_player_status.max_ice_bullets[target]);
  94. }
  95. else
  96. {
  97. continue;
  98. }
  99. hudpos += static_cast<float>(surface->get_height());
  100. const float ammo_text_width = Resources::fixed_font->get_text_width(ammo_text);
  101. if (InputManager::current()->get_num_users() > 1)
  102. {
  103. const std::string player_text = std::to_string(target + 1) + ":";
  104. context.color().draw_text(Resources::fixed_font,
  105. player_text,
  106. Vector(context.get_width() - BORDER_X - ammo_text_width -
  107. static_cast<float>(surface->get_width()) -
  108. Resources::fixed_font->get_text_width(player_text) - 3.f,
  109. hudpos + 13.f),
  110. ALIGN_LEFT,
  111. LAYER_HUD,
  112. PlayerStatusHUD::text_color);
  113. }
  114. context.color().draw_surface(surface,
  115. Vector(context.get_width() - BORDER_X - ammo_text_width -
  116. static_cast<float>(surface->get_width()),
  117. hudpos),
  118. LAYER_HUD);
  119. context.color().draw_text(Resources::fixed_font,
  120. ammo_text,
  121. Vector(context.get_width() - BORDER_X - ammo_text_width,
  122. hudpos + 13.f),
  123. ALIGN_LEFT,
  124. LAYER_HUD,
  125. PlayerStatusHUD::text_color);
  126. }
  127. context.pop_transform();
  128. }
  129. /* EOF */