water_drop.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SuperTux
  2. // Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
  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/water_drop.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "math/random.hpp"
  19. #include "object/sprite_particle.hpp"
  20. #include "sprite/sprite.hpp"
  21. #include "supertux/sector.hpp"
  22. WaterDrop::WaterDrop(const Vector& pos, const std::string& sprite_path_, const Vector& velocity) :
  23. MovingSprite(pos, sprite_path_, LAYER_OBJECTS - 1, COLGROUP_MOVING_ONLY_STATIC),
  24. physic(),
  25. wd_state(WDS_FALLING),
  26. sprite_path(sprite_path_)
  27. {
  28. physic.enable_gravity(true);
  29. physic.set_velocity(velocity);
  30. }
  31. void
  32. WaterDrop::update(float dt_sec)
  33. {
  34. m_col.set_movement(physic.get_movement(dt_sec));
  35. if ( m_sprite->animation_done() ) {
  36. remove_me();
  37. }
  38. }
  39. void
  40. WaterDrop::collision_solid(const CollisionHit& hit)
  41. {
  42. if (hit.bottom && wd_state == WDS_FALLING) {
  43. wd_state = WDS_SPLASH;
  44. physic.enable_gravity(false);
  45. SoundManager::current()->play("sounds/splash.ogg", get_pos());
  46. set_action("splash", 1);
  47. // spawn water particles
  48. for (int i = 50; i; i--) {
  49. int pa = graphicsRandom.rand(0, 3);
  50. float px = graphicsRandom.randf(m_col.m_bbox.get_left(), m_col.m_bbox.get_right());
  51. float py = graphicsRandom.randf(m_col.m_bbox.get_top(), m_col.m_bbox.get_bottom());
  52. Vector ppos = Vector(px, py);
  53. Vector pspeed = ppos - m_col.m_bbox.get_middle();
  54. pspeed.x *= 12;
  55. pspeed.y *= 12;
  56. Sector::get().add<SpriteParticle>(sprite_path, "particle_" + std::to_string(pa),
  57. ppos, ANCHOR_MIDDLE,
  58. pspeed, Vector(0, 100 * Sector::get().get_gravity()),
  59. LAYER_OBJECTS + 1);
  60. }
  61. }
  62. }
  63. HitResponse
  64. WaterDrop::collision(GameObject&, const CollisionHit& )
  65. {
  66. return FORCE_MOVE;
  67. }
  68. /* EOF */