shard.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SuperTux
  2. // Copyright (C) 2021 Daniel Ward <weluvgoatz@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/shard.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "badguy/badguy.hpp"
  19. #include "math/util.hpp"
  20. #include "object/player.hpp"
  21. #include "sprite/sprite.hpp"
  22. #include "util/reader_mapping.hpp"
  23. Shard::Shard(const ReaderMapping& reader) :
  24. MovingSprite(reader, "images/creatures/crystallo/shard.sprite", LAYER_TILES - 2, COLGROUP_MOVING),
  25. m_physic(),
  26. m_stick_timer()
  27. {
  28. m_physic.enable_gravity(true);
  29. SoundManager::current()->preload("sounds/crystallo-shardhit.ogg");
  30. }
  31. Shard::Shard(const Vector& pos, const Vector& velocity, const std::string& sprite) :
  32. MovingSprite(pos, sprite, LAYER_TILES - 2, COLGROUP_MOVING),
  33. m_physic(),
  34. m_stick_timer()
  35. {
  36. m_physic.enable_gravity(true);
  37. m_physic.set_velocity(velocity);
  38. set_action("default");
  39. SoundManager::current()->preload("sounds/crystallo-shardhit.ogg");
  40. }
  41. void
  42. Shard::update(float dt_sec)
  43. {
  44. if (m_physic.get_velocity() != Vector(0.f, 0.f))
  45. m_sprite->set_angle(math::degrees(math::angle(Vector(m_physic.get_velocity_x(), m_physic.get_velocity_y()))));
  46. m_col.set_movement(m_physic.get_movement(dt_sec));
  47. if (m_stick_timer.check())
  48. remove_me();
  49. }
  50. void
  51. Shard::collision_solid(const CollisionHit& hit)
  52. {
  53. m_physic.set_velocity(0.f, 0.f);
  54. m_physic.set_acceleration(0.f, 0.f);
  55. m_physic.enable_gravity(hit.bottom);
  56. if (!m_stick_timer.started())
  57. {
  58. m_stick_timer.start(5.f);
  59. SoundManager::current()->play("sounds/crystallo-shardhit.ogg", get_pos());
  60. }
  61. }
  62. HitResponse
  63. Shard::collision(GameObject& other, const CollisionHit&)
  64. {
  65. // ignore collisions with other shards
  66. auto shard = dynamic_cast<Shard*>(&other);
  67. if (&other == shard)
  68. return ABORT_MOVE;
  69. // kill badguys
  70. auto badguy = dynamic_cast<BadGuy*>(&other);
  71. if (badguy != nullptr)
  72. badguy->kill_fall();
  73. // kill players
  74. auto player = dynamic_cast<Player*>(&other);
  75. if (player != nullptr)
  76. player->kill(false);
  77. return ABORT_MOVE;
  78. }
  79. /* EOF */