level_time.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  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 "object/level_time.hpp"
  17. #include <algorithm>
  18. #include "editor/editor.hpp"
  19. #include "object/player.hpp"
  20. #include "supertux/game_session.hpp"
  21. #include "supertux/resources.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "util/reader_mapping.hpp"
  24. #include "video/drawing_context.hpp"
  25. #include "video/surface.hpp"
  26. /** When to alert player they're low on time! */
  27. static const float TIME_WARNING = 20;
  28. LevelTime::LevelTime(const ReaderMapping& reader) :
  29. GameObject(reader),
  30. ExposedObject<LevelTime, scripting::LevelTime>(this),
  31. time_surface(Surface::from_file("images/engine/hud/time-0.png")),
  32. running(!Editor::is_active()),
  33. time_left()
  34. {
  35. reader.get("time", time_left, 0.0f);
  36. if (time_left <= 0 && !Editor::is_active()) {
  37. log_warning << "No or invalid leveltime specified." << std::endl;
  38. remove_me();
  39. }
  40. }
  41. ObjectSettings
  42. LevelTime::get_settings()
  43. {
  44. ObjectSettings result = GameObject::get_settings();
  45. result.add_float(_("Time"), &time_left, "time");
  46. result.add_remove();
  47. return result;
  48. }
  49. void
  50. LevelTime::update(float dt_sec)
  51. {
  52. if (!running) return;
  53. int players_alive = Sector::current() ? Sector::current()->get_object_count<Player>([](const Player& p) {
  54. return !p.is_dead() && !p.is_dying() && !p.is_winning();
  55. }) : 0;
  56. if (!players_alive)
  57. return;
  58. int prev_time = static_cast<int>(floorf(time_left*5));
  59. time_left -= dt_sec;
  60. if (time_left <= 0) {
  61. // Needed to avoid charging a player coins if they had a checkpoint
  62. if (GameSession::current())
  63. GameSession::current()->clear_respawn_points();
  64. if (time_left <= -5 || !Sector::get().get_players()[0]->get_coins())
  65. {
  66. for (auto& p : Sector::get().get_players())
  67. {
  68. p->kill(true);
  69. }
  70. stop();
  71. }
  72. if (prev_time != static_cast<int>(floorf(time_left*5)))
  73. {
  74. for (auto& p : Sector::get().get_players())
  75. {
  76. if (p->is_dead() || p->is_dying() || p->is_winning())
  77. continue;
  78. p->add_coins(-1);
  79. // FIXME: Find a cleaner way to handle this
  80. // (Remove only one coin per second, not per player per second)
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. void
  87. LevelTime::draw(DrawingContext& context)
  88. {
  89. if (Editor::is_active())
  90. return;
  91. context.push_transform();
  92. context.set_translation(Vector(0, 0));
  93. context.transform().scale = 1.f;
  94. if ((time_left > TIME_WARNING) || (int(g_game_time * 2.5f) % 2)) {
  95. std::stringstream ss;
  96. ss << int(time_left);
  97. std::string time_text = ss.str();
  98. if (time_surface)
  99. {
  100. float all_width = static_cast<float>(time_surface->get_width()) + Resources::normal_font->get_text_width(time_text);
  101. context.color().draw_surface(time_surface,
  102. Vector((context.get_width() - all_width) / 2.0f,
  103. BORDER_Y + 1),
  104. LAYER_HUD);
  105. context.color().draw_text(Resources::normal_font, time_text,
  106. Vector((context.get_width() - all_width) / 2.0f + static_cast<float>(time_surface->get_width()),
  107. BORDER_Y + 14),
  108. ALIGN_LEFT, LAYER_HUD, LevelTime::text_color);
  109. }
  110. }
  111. context.pop_transform();
  112. }
  113. void
  114. LevelTime::start()
  115. {
  116. running = true;
  117. }
  118. void
  119. LevelTime::stop()
  120. {
  121. running = false;
  122. }
  123. float
  124. LevelTime::get_time() const
  125. {
  126. return time_left;
  127. }
  128. void
  129. LevelTime::set_time(float time_left_)
  130. {
  131. time_left = std::min(std::max(time_left_, 0.0f), 999.0f);
  132. }
  133. /* EOF */