level_time.cpp 4.6 KB

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