secretarea_trigger.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "trigger/secretarea_trigger.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "editor/editor.hpp"
  19. #include "object/tilemap.hpp"
  20. #include "supertux/debug.hpp"
  21. #include "supertux/level.hpp"
  22. #include "supertux/resources.hpp"
  23. #include "supertux/sector.hpp"
  24. #include "util/reader_mapping.hpp"
  25. #include "video/drawing_context.hpp"
  26. #include "video/font.hpp"
  27. #include "video/video_system.hpp"
  28. #include "video/viewport.hpp"
  29. static const float MESSAGE_TIME=3.5;
  30. SecretAreaTrigger::SecretAreaTrigger(const ReaderMapping& reader) :
  31. Trigger(reader),
  32. message_timer(),
  33. message_displayed(false),
  34. message(),
  35. fade_tilemap(),
  36. script()
  37. {
  38. reader.get("fade-tilemap", fade_tilemap);
  39. reader.get("message", message);
  40. reader.get("script", script);
  41. if (message.empty() && !Editor::is_active())
  42. message = _("You found a secret area!");
  43. }
  44. ObjectSettings
  45. SecretAreaTrigger::get_settings()
  46. {
  47. ObjectSettings result = Trigger::get_settings();
  48. result.add_text(_("Name"), &m_name);
  49. result.add_text(_("Fade tilemap"), &fade_tilemap, "fade-tilemap");
  50. result.add_translatable_text(_("Message"), &message, "message");
  51. result.add_script(_("Script"), &script, "script");
  52. result.reorder({"fade-tilemap", "script", "sprite", "message", "region", "name", "x", "y"});
  53. return result;
  54. }
  55. void
  56. SecretAreaTrigger::draw(DrawingContext& context)
  57. {
  58. if (message_timer.started()) {
  59. context.push_transform();
  60. context.set_translation(Vector(0, 0));
  61. context.transform().scale = 1.f;
  62. Vector pos = Vector(context.get_width() / 2.0f, context.get_height() / 2.0f - Resources::normal_font->get_height() / 2.0f);
  63. context.color().draw_text(Resources::normal_font, message, pos, FontAlignment::ALIGN_CENTER, LAYER_HUD, SecretAreaTrigger::text_color);
  64. context.pop_transform();
  65. }
  66. if (Editor::is_active() || g_debug.show_collision_rects) {
  67. context.color().draw_filled_rect(m_col.m_bbox, Color(0.0f, 1.0f, 0.0f, 0.6f),
  68. 0.0f, LAYER_OBJECTS);
  69. } else if (message_timer.check()) {
  70. remove_me();
  71. }
  72. }
  73. void
  74. SecretAreaTrigger::event(Player& , EventType type)
  75. {
  76. if (type == EVENT_TOUCH) {
  77. if (!message_displayed) {
  78. message_timer.start(MESSAGE_TIME);
  79. message_displayed = true;
  80. Sector::get().get_level().m_stats.increment_secrets();
  81. SoundManager::current()->play("sounds/welldone.ogg");
  82. if (!fade_tilemap.empty()) {
  83. // fade away tilemaps
  84. for (auto& tm : Sector::get().get_objects_by_type<TileMap>()) {
  85. if (tm.get_name() == fade_tilemap) {
  86. tm.fade(0.0, 1.0);
  87. }
  88. }
  89. }
  90. if (!script.empty()) {
  91. Sector::get().run_script(script, "SecretAreaScript");
  92. }
  93. }
  94. }
  95. }
  96. /* EOF */