switch.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. SpritedTrigger(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. m_bistable = reader.get("off-script", m_off_script);
  42. SoundManager::current()->preload(SWITCH_SOUND);
  43. }
  44. Switch::~Switch()
  45. {
  46. }
  47. ObjectSettings
  48. Switch::get_settings()
  49. {
  50. ObjectSettings result = SpritedTrigger::get_settings();
  51. result.add_direction(_("Direction"), &m_dir,
  52. { Direction::NONE, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }, "direction");
  53. result.add_script(_("Turn on script"), &m_script, "script");
  54. result.add_script(_("Turn off script"), &m_off_script, "off-script");
  55. result.reorder({"direction", "script", "off-script", "sprite", "x", "y"});
  56. return result;
  57. }
  58. void
  59. Switch::update(float )
  60. {
  61. switch (m_state) {
  62. case OFF:
  63. break;
  64. case TURN_ON:
  65. if (m_sprite->animation_done()) {
  66. std::ostringstream location;
  67. location << "switch" << m_col.m_bbox.p1();
  68. Sector::get().run_script(m_script, location.str());
  69. set_action("on", m_dir, 1);
  70. m_state = ON;
  71. }
  72. break;
  73. case ON:
  74. if (m_sprite->animation_done() && !m_bistable) {
  75. set_action("turnoff", m_dir, 1);
  76. m_state = TURN_OFF;
  77. }
  78. break;
  79. case TURN_OFF:
  80. if (m_sprite->animation_done()) {
  81. if (m_bistable) {
  82. std::ostringstream location;
  83. location << "switch" << m_col.m_bbox.p1();
  84. Sector::get().run_script(m_off_script, location.str());
  85. }
  86. set_action("off", m_dir);
  87. m_state = OFF;
  88. }
  89. break;
  90. }
  91. }
  92. void
  93. Switch::event(Player& , EventType type)
  94. {
  95. if (type != EVENT_ACTIVATE) return;
  96. switch (m_state) {
  97. case OFF:
  98. set_action("turnon", m_dir, 1);
  99. SoundManager::current()->play(SWITCH_SOUND, get_pos());
  100. m_state = TURN_ON;
  101. break;
  102. case TURN_ON:
  103. break;
  104. case ON:
  105. if (m_bistable) {
  106. set_action("turnoff", m_dir, 1);
  107. SoundManager::current()->play(SWITCH_SOUND, get_pos());
  108. m_state = TURN_OFF;
  109. }
  110. break;
  111. case TURN_OFF:
  112. break;
  113. }
  114. }
  115. void
  116. Switch::after_editor_set()
  117. {
  118. SpritedTrigger::after_editor_set();
  119. set_action("off", m_dir);
  120. }
  121. void
  122. Switch::on_flip(float height)
  123. {
  124. SpritedTrigger::on_flip(height);
  125. FlipLevelTransformer::transform_flip(m_flip);
  126. }
  127. /* EOF */