fallblock.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  65. HitResponse
  66. FallBlock::collision(GameObject& other, const CollisionHit& hit)
  67. {
  68. auto fallblock = dynamic_cast<FallBlock*> (&other);
  69. if (fallblock && hit.bottom && (m_state == FALL || m_state == LAND))
  70. {
  71. m_physic.set_velocity_y(0.0f);
  72. return CONTINUE;
  73. }
  74. auto player = dynamic_cast<Player*>(&other);
  75. if (m_state == IDLE && player && player->get_bbox().get_bottom() < m_col.m_bbox.get_top())
  76. {
  77. m_state = SHAKE;
  78. SoundManager::current()->play("sounds/cracking.wav", get_pos());
  79. m_timer.start(0.5f);
  80. }
  81. return FORCE_MOVE;
  82. }
  83. void
  84. FallBlock::collision_solid(const CollisionHit& hit)
  85. {
  86. if (hit.top || hit.bottom || hit.crush)
  87. {
  88. m_physic.set_velocity(0.0f, 0.0f);
  89. }
  90. if (m_state == FALL && hit.bottom)
  91. {
  92. Sector::get().get_camera().shake(0.125f, 0.0f, 10.0f);
  93. SoundManager::current()->play("sounds/thud.ogg", get_pos());
  94. m_state = LAND;
  95. }
  96. }
  97. void
  98. FallBlock::draw(DrawingContext& context)
  99. {
  100. Vector pos = get_pos();
  101. // shaking
  102. if (m_state == SHAKE)
  103. {
  104. pos.x += static_cast<float>(graphicsRandom.rand(-8, 8));
  105. pos.y += static_cast<float>(graphicsRandom.rand(-5, 5));
  106. }
  107. m_sprite->draw(context.color(), pos, m_layer, m_flip);
  108. }
  109. bool
  110. FallBlock::found_victim_down() const
  111. {
  112. if (auto* player = Sector::get().get_nearest_player(m_col.m_bbox))
  113. {
  114. const Rectf& player_bbox = player->get_bbox();
  115. Rectf crush_area_down = Rectf(m_col.m_bbox.get_left()+1, m_col.m_bbox.get_bottom(),
  116. m_col.m_bbox.get_right()-1, std::max(m_col.m_bbox.get_bottom(),player_bbox.get_top()-1));
  117. if ((player_bbox.get_top() >= m_col.m_bbox.get_bottom())
  118. && (player_bbox.get_right() > (m_col.m_bbox.get_left() - 4))
  119. && (player_bbox.get_left() < (m_col.m_bbox.get_right() + 4))
  120. && (Sector::get().is_free_of_statics(crush_area_down, this, false)))
  121. {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. void
  128. FallBlock::on_flip(float height)
  129. {
  130. MovingSprite::on_flip(height);
  131. FlipLevelTransformer::transform_flip(m_flip);
  132. }
  133. /* EOF */