bumper.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2020 Daniel Ward <weluvgoatz@gmail.com>
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #include "object/bumper.hpp"
  16. #include "audio/sound_manager.hpp"
  17. #include "object/player.hpp"
  18. #include "sprite/sprite.hpp"
  19. #include "sprite/sprite_manager.hpp"
  20. #include "supertux/flip_level_transformer.hpp"
  21. #include "supertux/sector.hpp"
  22. #include "util/reader_mapping.hpp"
  23. namespace {
  24. const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
  25. const float BOUNCE_Y = -450.0f;
  26. const float BOUNCE_X = 700.0f;
  27. }
  28. Bumper::Bumper(const ReaderMapping& reader) :
  29. MovingSprite(reader, "images/objects/trampoline/bumper.sprite", LAYER_OBJECTS, COLGROUP_MOVING),
  30. m_physic(),
  31. m_dir(Direction::RIGHT)
  32. {
  33. std::string dir_str;
  34. bool old_facing_left = false;
  35. if (reader.get("direction", dir_str))
  36. m_dir = string_to_dir(dir_str);
  37. else if (reader.get("left", old_facing_left) && old_facing_left)
  38. m_dir = Direction::LEFT;
  39. set_action("normal", m_dir);
  40. m_physic.enable_gravity(false);
  41. }
  42. ObjectSettings
  43. Bumper::get_settings()
  44. {
  45. ObjectSettings result = MovingSprite::get_settings();
  46. result.add_direction(_("Direction"), &m_dir, { Direction::RIGHT, Direction::LEFT }, "direction");
  47. result.reorder({"direction", "sprite", "x", "y"});
  48. return result;
  49. }
  50. void
  51. Bumper::update(float dt_sec)
  52. {
  53. if (m_sprite->animation_done())
  54. set_action("normal", m_dir);
  55. m_col.set_movement(m_physic.get_movement (dt_sec));
  56. }
  57. HitResponse
  58. Bumper::collision(GameObject& other, const CollisionHit& hit)
  59. {
  60. auto player = dynamic_cast<Player*> (&other);
  61. if (player)
  62. {
  63. float BOUNCE_DIR = m_dir == Direction::LEFT ? -BOUNCE_X : BOUNCE_X;
  64. player->get_physic().set_velocity(0.f, player->is_swimming() ? 0.f : BOUNCE_Y);
  65. player->sideways_push(BOUNCE_DIR);
  66. SoundManager::current()->play(TRAMPOLINE_SOUND, get_pos());
  67. set_action("swinging", m_dir, 1);
  68. }
  69. auto bumper = dynamic_cast<Bumper*> (&other);
  70. if (bumper)
  71. {
  72. m_physic.set_velocity_y(0);
  73. return FORCE_MOVE;
  74. }
  75. return ABORT_MOVE;
  76. }
  77. Physic&
  78. Bumper::get_physic()
  79. {
  80. return m_physic;
  81. }
  82. void
  83. Bumper::after_editor_set()
  84. {
  85. MovingSprite::after_editor_set();
  86. set_action("normal", m_dir);
  87. }
  88. void
  89. Bumper::on_flip(float height)
  90. {
  91. MovingSprite::on_flip(height);
  92. FlipLevelTransformer::transform_flip(m_flip);
  93. }
  94. /* EOF */