fallblock.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/fallblock.hpp"
  16. #include "audio/sound_manager.hpp"
  17. #include "object/bumper.hpp"
  18. #include "object/player.hpp"
  19. #include "object/camera.hpp"
  20. #include "sprite/sprite.hpp"
  21. #include "supertux/flip_level_transformer.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "supertux/tile.hpp"
  24. #include "math/random.hpp"
  25. #include "util/reader_mapping.hpp"
  26. FallBlock::FallBlock(const ReaderMapping& reader) :
  27. MovingSprite(reader, "images/objects/fallblock/cave-4x4.sprite", LAYER_OBJECTS, COLGROUP_STATIC),
  28. m_state(IDLE),
  29. m_physic(),
  30. m_timer()
  31. {
  32. SoundManager::current()->preload("sounds/cracking.wav");
  33. SoundManager::current()->preload("sounds/thud.ogg");
  34. m_physic.enable_gravity(false);
  35. }
  36. void
  37. FallBlock::update(float dt_sec)
  38. {
  39. switch (m_state)
  40. {
  41. case IDLE:
  42. set_group(COLGROUP_STATIC);
  43. if (found_victim_down())
  44. {
  45. m_state = SHAKE;
  46. SoundManager::current()->play("sounds/cracking.wav", get_pos());
  47. m_timer.start(0.5f);
  48. }
  49. break;
  50. case SHAKE:
  51. if (m_timer.check())
  52. {
  53. m_state = FALL;
  54. m_physic.reset();
  55. m_physic.enable_gravity(true);
  56. }
  57. break;
  58. case FALL:
  59. case LAND:
  60. m_col.set_movement(m_physic.get_movement (dt_sec));
  61. set_group(COLGROUP_MOVING_STATIC);
  62. break;
  63. }
  64. for (auto& bumper : Sector::get().get_objects_by_type<Bumper>())
  65. {
  66. Rectf bumper_bbox = bumper.get_bbox();
  67. if ((bumper_bbox.get_left() < (m_col.m_bbox.get_right() + 8))
  68. && (bumper_bbox.get_right() > (m_col.m_bbox.get_left() - 8))
  69. && (bumper_bbox.get_bottom() > (m_col.m_bbox.get_top() - 8))
  70. && (bumper_bbox.get_top() < (m_col.m_bbox.get_bottom() + 8)))
  71. {
  72. switch (m_state)
  73. {
  74. case IDLE:
  75. break;
  76. case SHAKE:
  77. break;
  78. case FALL:
  79. bumper.get_physic().enable_gravity(true);
  80. break;
  81. case LAND:
  82. bumper.get_physic().enable_gravity(false);
  83. bumper.get_physic().set_gravity_modifier(0.f);
  84. bumper.get_physic().set_velocity_y(0.f);
  85. bumper.get_physic().reset();
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. HitResponse
  92. FallBlock::collision(GameObject& other, const CollisionHit& hit)
  93. {
  94. auto fallblock = dynamic_cast<FallBlock*> (&other);
  95. if (fallblock && hit.bottom && (m_state == FALL || m_state == LAND))
  96. {
  97. m_physic.set_velocity_y(0.0f);
  98. return CONTINUE;
  99. }
  100. auto player = dynamic_cast<Player*>(&other);
  101. if (m_state == IDLE && player && player->get_bbox().get_bottom() < m_col.m_bbox.get_top())
  102. {
  103. m_state = SHAKE;
  104. SoundManager::current()->play("sounds/cracking.wav", get_pos());
  105. m_timer.start(0.5f);
  106. }
  107. return FORCE_MOVE;
  108. }
  109. void
  110. FallBlock::collision_solid(const CollisionHit& hit)
  111. {
  112. if (hit.top || hit.bottom || hit.crush)
  113. {
  114. m_physic.set_velocity(0.0f, 0.0f);
  115. }
  116. if (m_state == FALL && hit.bottom)
  117. {
  118. Sector::get().get_camera().shake(0.125f, 0.0f, 10.0f);
  119. SoundManager::current()->play("sounds/thud.ogg", get_pos());
  120. m_state = LAND;
  121. }
  122. }
  123. void
  124. FallBlock::draw(DrawingContext& context)
  125. {
  126. Vector pos = get_pos();
  127. // shaking
  128. if (m_state == SHAKE)
  129. {
  130. pos.x += static_cast<float>(graphicsRandom.rand(-8, 8));
  131. pos.y += static_cast<float>(graphicsRandom.rand(-5, 5));
  132. }
  133. MovingSprite::draw(context);
  134. }
  135. bool
  136. FallBlock::found_victim_down() const
  137. {
  138. if (auto* player = Sector::get().get_nearest_player(m_col.m_bbox))
  139. {
  140. const Rectf& player_bbox = player->get_bbox();
  141. Rectf crush_area_down = Rectf(m_col.m_bbox.get_left()+1, m_col.m_bbox.get_bottom(),
  142. m_col.m_bbox.get_right()-1, std::max(m_col.m_bbox.get_bottom(),player_bbox.get_top()-1));
  143. if ((player_bbox.get_top() >= m_col.m_bbox.get_bottom())
  144. && (player_bbox.get_right() > (m_col.m_bbox.get_left() - 4))
  145. && (player_bbox.get_left() < (m_col.m_bbox.get_right() + 4))
  146. && (Sector::get().is_free_of_statics(crush_area_down, this, false)))
  147. {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. void
  154. FallBlock::on_flip(float height)
  155. {
  156. MovingSprite::on_flip(height);
  157. FlipLevelTransformer::transform_flip(m_flip);
  158. }
  159. /* EOF */