rock.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.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/rock.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "badguy/crusher.hpp"
  19. #include "badguy/badguy.hpp"
  20. #include "object/coin.hpp"
  21. #include "object/explosion.hpp"
  22. #include "object/lit_object.hpp"
  23. #include "object/pushbutton.hpp"
  24. #include "supertux/sector.hpp"
  25. #include "supertux/tile.hpp"
  26. #include "object/player.hpp"
  27. #include "util/reader_mapping.hpp"
  28. namespace {
  29. const std::string ROCK_SOUND = "sounds/brick.wav"; //TODO use own sound.
  30. const float GROUND_FRICTION = 0.1f; // Amount of friction to apply while on ground.
  31. } // namespace
  32. Rock::Rock(const ReaderMapping& reader, const std::string& spritename) :
  33. MovingSprite(reader, spritename),
  34. ExposedObject<Rock, scripting::Rock>(this),
  35. physic(),
  36. on_ground(false),
  37. last_movement(0.0f, 0.0f),
  38. on_grab_script(),
  39. on_ungrab_script()
  40. {
  41. parse_type(reader);
  42. reader.get("on-grab-script", on_grab_script, "");
  43. reader.get("on-ungrab-script", on_ungrab_script, "");
  44. SoundManager::current()->preload(ROCK_SOUND);
  45. set_group(COLGROUP_MOVING_STATIC);
  46. }
  47. Rock::Rock(const Vector& pos, const std::string& spritename) :
  48. MovingSprite(pos, spritename),
  49. ExposedObject<Rock, scripting::Rock>(this),
  50. physic(),
  51. on_ground(false),
  52. last_movement(0.0f, 0.0f),
  53. on_grab_script(),
  54. on_ungrab_script()
  55. {
  56. SoundManager::current()->preload(ROCK_SOUND);
  57. set_group(COLGROUP_MOVING_STATIC);
  58. }
  59. GameObjectTypes
  60. Rock::get_types() const
  61. {
  62. return {
  63. { "small", _("Small") },
  64. { "large", _("Large") }
  65. };
  66. }
  67. std::string
  68. Rock::get_default_sprite_name() const
  69. {
  70. switch (m_type)
  71. {
  72. case LARGE:
  73. return "images/objects/rock/rock-b.png";
  74. default:
  75. return m_default_sprite_name;
  76. }
  77. }
  78. void
  79. Rock::update(float dt_sec)
  80. {
  81. if (!is_grabbed())
  82. m_col.set_movement(physic.get_movement(dt_sec));
  83. }
  84. void
  85. Rock::collision_solid(const CollisionHit& hit)
  86. {
  87. if (is_grabbed()) {
  88. return;
  89. }
  90. if (hit.top || hit.bottom)
  91. physic.set_velocity_y(0);
  92. if (hit.left || hit.right) {
  93. // Bounce back slightly when hitting a wall
  94. float velx = physic.get_velocity_x();
  95. physic.set_velocity_x(-0.1f * velx);
  96. }
  97. if (hit.crush)
  98. physic.set_velocity(0, 0);
  99. if (hit.bottom && !on_ground && !is_grabbed()) {
  100. SoundManager::current()->play(ROCK_SOUND, get_pos());
  101. physic.set_velocity_x(0);
  102. on_ground = true;
  103. }
  104. if (on_ground) {
  105. // Full friction!
  106. physic.set_velocity_x(physic.get_velocity_x() * (1.f - GROUND_FRICTION));
  107. }
  108. }
  109. HitResponse
  110. Rock::collision(GameObject& other, const CollisionHit& hit)
  111. {
  112. auto heavy_coin = dynamic_cast<HeavyCoin*> (&other);
  113. if (heavy_coin) {
  114. return ABORT_MOVE;
  115. }
  116. auto explosion = dynamic_cast<Explosion*> (&other);
  117. if (explosion) {
  118. return ABORT_MOVE;
  119. }
  120. // Why is it necessary to list exceptions here? Why doesn't the rock just not
  121. // affect object that have ABORT_MOVE on all collisions?
  122. auto litobject = dynamic_cast<LitObject*> (&other);
  123. if (litobject) {
  124. return ABORT_MOVE;
  125. }
  126. auto pushbutton = dynamic_cast<PushButton*> (&other);
  127. if (pushbutton) {
  128. return ABORT_MOVE;
  129. }
  130. if (is_grabbed()) {
  131. return ABORT_MOVE;
  132. }
  133. auto crusher = dynamic_cast<Crusher*> (&other);
  134. if (crusher) {
  135. return FORCE_MOVE;
  136. }
  137. // Don't fall further if we are on a rock which is on the ground.
  138. // This is to avoid jittering.
  139. auto rock = dynamic_cast<Rock*> (&other);
  140. if (rock && rock->on_ground && hit.bottom) {
  141. physic.set_velocity_y(0);
  142. return CONTINUE;
  143. }
  144. if (!on_ground) {
  145. if (hit.bottom && physic.get_velocity_y() > 200) {
  146. auto badguy = dynamic_cast<BadGuy*> (&other);
  147. auto player = dynamic_cast<Player*> (&other);
  148. if (badguy && badguy->get_group() != COLGROUP_TOUCHABLE) {
  149. //Getting a rock on the head hurts. A lot.
  150. badguy->kill_fall();
  151. physic.set_velocity_y(0);
  152. }
  153. else if(player)
  154. {
  155. player->kill(false);
  156. physic.set_velocity_y(0);
  157. }
  158. }
  159. return FORCE_MOVE;
  160. }
  161. return FORCE_MOVE;
  162. }
  163. void
  164. Rock::grab(MovingObject& object, const Vector& pos, Direction dir_)
  165. {
  166. Portable::grab(object, pos, dir_);
  167. Vector movement = pos - get_pos();
  168. m_col.set_movement(movement);
  169. last_movement = movement;
  170. set_group(COLGROUP_TOUCHABLE); //needed for lanterns catching willowisps
  171. on_ground = false;
  172. if (!on_grab_script.empty()) {
  173. Sector::get().run_script(on_grab_script, "Rock::on_grab");
  174. }
  175. }
  176. void
  177. Rock::ungrab(MovingObject& object, Direction dir)
  178. {
  179. auto player = dynamic_cast<Player*> (&object);
  180. set_group(COLGROUP_MOVING_STATIC);
  181. on_ground = false;
  182. if (player)
  183. {
  184. if (player->is_swimming() || player->is_water_jumping())
  185. {
  186. float swimangle = player->get_swimming_angle();
  187. physic.set_velocity(player->get_velocity() + Vector(std::cos(swimangle), std::sin(swimangle)));
  188. }
  189. else
  190. {
  191. physic.set_velocity_x(fabsf(player->get_physic().get_velocity_x()) < 1.f ? 0.f :
  192. player->m_dir == Direction::LEFT ? -200.f : 200.f);
  193. physic.set_velocity_y((dir == Direction::UP) ? -500.f : (dir == Direction::DOWN) ? 500.f :
  194. (glm::length(last_movement) > 1) ? -200.f : 0.f);
  195. }
  196. }
  197. if (!on_ungrab_script.empty())
  198. {
  199. Sector::get().run_script(on_ungrab_script, "Rock::on_ungrab");
  200. }
  201. Portable::ungrab(object, dir);
  202. }
  203. ObjectSettings
  204. Rock::get_settings()
  205. {
  206. auto result = MovingSprite::get_settings();
  207. result.add_script(_("On-grab script"), &on_grab_script, "on-grab-script");
  208. result.add_script(_("On-ungrab script"), &on_ungrab_script, "on-ungrab-script");
  209. return result;
  210. }
  211. void
  212. Rock::add_wind_velocity(const Vector& velocity, const Vector& end_speed)
  213. {
  214. // only add velocity in the same direction as the wind
  215. if (end_speed.x > 0 && physic.get_velocity_x() < end_speed.x)
  216. physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
  217. if (end_speed.x < 0 && physic.get_velocity_x() > end_speed.x)
  218. physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
  219. if (end_speed.y > 0 && physic.get_velocity_y() < end_speed.y)
  220. physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
  221. if (end_speed.y < 0 && physic.get_velocity_y() > end_speed.y)
  222. physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
  223. }
  224. /* EOF */