switch.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SuperTux - Switch Trigger
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/switch.hpp"
  17. #include <sstream>
  18. #include "audio/sound_manager.hpp"
  19. #include "supertux/flip_level_transformer.hpp"
  20. #include "supertux/sector.hpp"
  21. #include "util/log.hpp"
  22. #include "util/reader_mapping.hpp"
  23. namespace {
  24. const std::string SWITCH_SOUND = "sounds/switch.ogg";
  25. } // namespace
  26. Switch::Switch(const ReaderMapping& reader) :
  27. StickyTrigger(reader, "images/objects/switch/switch.sprite"),
  28. m_script(),
  29. m_off_script(),
  30. m_state(OFF),
  31. m_bistable(),
  32. m_dir(Direction::NONE)
  33. {
  34. std::string dir_str;
  35. if (reader.get("direction", dir_str))
  36. m_dir = string_to_dir(dir_str);
  37. else
  38. m_dir = Direction::NONE;
  39. set_action("off", m_dir);
  40. reader.get("script", m_script);
  41. reader.get("sticky", m_sticky, false);
  42. m_bistable = reader.get("off-script", m_off_script);
  43. SoundManager::current()->preload(SWITCH_SOUND);
  44. }
  45. Switch::~Switch()
  46. {
  47. }
  48. ObjectSettings
  49. Switch::get_settings()
  50. {
  51. ObjectSettings result = StickyTrigger::get_settings();
  52. result.add_direction(_("Direction"), &m_dir,
  53. { Direction::NONE, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }, "direction");
  54. result.add_script(_("Turn on script"), &m_script, "script");
  55. result.add_script(_("Turn off script"), &m_off_script, "off-script");
  56. result.reorder({"direction", "script", "off-script", "sticky", "sprite", "x", "y"});
  57. return result;
  58. }
  59. void
  60. Switch::update(float dt_sec)
  61. {
  62. if (m_sticky && m_dir != Direction::NONE)
  63. StickyObject::update(dt_sec);
  64. switch (m_state) {
  65. case OFF:
  66. break;
  67. case TURN_ON:
  68. if (m_sprite->animation_done()) {
  69. std::ostringstream location;
  70. location << "switch" << m_col.m_bbox.p1();
  71. Sector::get().run_script(m_script, location.str());
  72. set_action("on", m_dir, 1);
  73. m_state = ON;
  74. }
  75. break;
  76. case ON:
  77. if (m_sprite->animation_done() && !m_bistable) {
  78. set_action("turnoff", m_dir, 1);
  79. m_state = TURN_OFF;
  80. }
  81. break;
  82. case TURN_OFF:
  83. if (m_sprite->animation_done()) {
  84. if (m_bistable) {
  85. std::ostringstream location;
  86. location << "switch" << m_col.m_bbox.p1();
  87. Sector::get().run_script(m_off_script, location.str());
  88. }
  89. set_action("off", m_dir);
  90. m_state = OFF;
  91. }
  92. break;
  93. }
  94. }
  95. void
  96. Switch::event(Player& , EventType type)
  97. {
  98. if (type != EVENT_ACTIVATE) return;
  99. switch (m_state) {
  100. case OFF:
  101. set_action("turnon", m_dir, 1);
  102. SoundManager::current()->play(SWITCH_SOUND, get_pos());
  103. m_state = TURN_ON;
  104. break;
  105. case TURN_ON:
  106. break;
  107. case ON:
  108. if (m_bistable) {
  109. set_action("turnoff", m_dir, 1);
  110. SoundManager::current()->play(SWITCH_SOUND, get_pos());
  111. m_state = TURN_OFF;
  112. }
  113. break;
  114. case TURN_OFF:
  115. break;
  116. }
  117. }
  118. void
  119. Switch::after_editor_set()
  120. {
  121. StickyTrigger::after_editor_set();
  122. set_action("off", m_dir);
  123. }
  124. void
  125. Switch::on_flip(float height)
  126. {
  127. StickyTrigger::on_flip(height);
  128. FlipLevelTransformer::transform_flip(m_flip);
  129. }
  130. /* EOF */