pushbutton.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SuperTux - PushButton running a script
  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 "object/pushbutton.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "object/player.hpp"
  19. #include "object/rock.hpp"
  20. #include "sprite/sprite.hpp"
  21. #include "supertux/flip_level_transformer.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "util/reader_mapping.hpp"
  24. namespace {
  25. const std::string BUTTON_SOUND = "sounds/switch.ogg";
  26. //14 -> 8
  27. }
  28. PushButton::PushButton(const ReaderMapping& mapping) :
  29. MovingSprite(mapping, "images/objects/pushbutton/pushbutton.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING),
  30. m_script(),
  31. m_state(OFF),
  32. m_dir(Direction::UP)
  33. {
  34. SoundManager::current()->preload(BUTTON_SOUND);
  35. if (!mapping.get("script", m_script))
  36. {
  37. log_warning << "No script set for pushbutton." << std::endl;
  38. }
  39. bool old_upside_down;
  40. std::string dir_str;
  41. if (mapping.get("direction", dir_str))
  42. m_dir = string_to_dir(dir_str);
  43. else if (mapping.get("upside-down", old_upside_down) && old_upside_down)
  44. m_dir = Direction::DOWN;
  45. set_action("off", m_dir, -1);
  46. }
  47. ObjectSettings
  48. PushButton::get_settings()
  49. {
  50. ObjectSettings result = MovingSprite::get_settings();
  51. result.add_direction(_("Direction"), &m_dir, { Direction::UP, Direction::DOWN }, "direction");
  52. result.add_script(_("Script"), &m_script, "script");
  53. result.reorder({"direction", "script", "x", "y"});
  54. return result;
  55. }
  56. void
  57. PushButton::after_editor_set()
  58. {
  59. MovingSprite::after_editor_set();
  60. set_action("off", m_dir);
  61. }
  62. void
  63. PushButton::update(float /*dt_sec*/)
  64. {
  65. }
  66. HitResponse
  67. PushButton::collision(GameObject& other, const CollisionHit& hit)
  68. {
  69. auto player = dynamic_cast<Player*>(&other);
  70. auto rock = dynamic_cast<Rock*>(&other);
  71. if (!player && !rock)
  72. return FORCE_MOVE;
  73. if (player)
  74. {
  75. float vy = player->get_physic().get_velocity_y();
  76. if (m_dir == Direction::DOWN)
  77. {
  78. if (vy >= 0)
  79. return FORCE_MOVE;
  80. if (hit.bottom)
  81. player->get_physic().set_velocity_y(0);
  82. }
  83. else
  84. {
  85. if (vy <= 0)
  86. return FORCE_MOVE;
  87. if (hit.top)
  88. {
  89. player->get_physic().set_velocity_y(0);
  90. player->set_on_ground(true);
  91. }
  92. }
  93. }
  94. if (m_state != OFF || !(m_dir == Direction::DOWN ? hit.bottom : hit.top))
  95. return FORCE_MOVE;
  96. // change appearance
  97. m_state = ON;
  98. float old_bbox_height = m_col.m_bbox.get_height();
  99. set_action("on", m_dir, -1);
  100. float new_bbox_height = m_col.m_bbox.get_height();
  101. Vector delta(0, old_bbox_height - new_bbox_height);
  102. set_pos(get_pos() + delta * (m_dir == Direction::DOWN ? 0 : 1.f));
  103. // play sound
  104. SoundManager::current()->play(BUTTON_SOUND, get_pos());
  105. // run script
  106. Sector::get().run_script(m_script, "PushButton");
  107. return FORCE_MOVE;
  108. }
  109. void
  110. PushButton::on_flip(float height)
  111. {
  112. MovingSprite::on_flip(height);
  113. m_dir = m_dir == Direction::UP ? Direction::DOWN : Direction::UP;
  114. set_action(m_state == OFF ? "off" : "on", m_dir);
  115. }
  116. /* EOF */