rock.cpp 7.2 KB

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