shard.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "supertux/sector.hpp"
  23. #include "util/reader_mapping.hpp"
  24. constexpr float STICKING_TIME = 0.7f;
  25. constexpr float FADEOUT_TIME = 0.3f;
  26. Shard::Shard(const ReaderMapping& reader) :
  27. StickyObject(reader, "images/creatures/crystallo/shard.sprite", LAYER_TILES - 2, COLGROUP_MOVING),
  28. m_physic(),
  29. m_stick_timer(),
  30. m_fadeout_timer()
  31. {
  32. m_physic.enable_gravity(true);
  33. SoundManager::current()->preload("sounds/crystallo-shardhit.ogg");
  34. }
  35. Shard::Shard(const Vector& pos, const Vector& velocity, const std::string& sprite) :
  36. StickyObject(pos, sprite, LAYER_TILES - 2, COLGROUP_MOVING),
  37. m_physic(),
  38. m_stick_timer(),
  39. m_fadeout_timer()
  40. {
  41. m_physic.enable_gravity(true);
  42. m_physic.set_velocity(velocity);
  43. set_action("default");
  44. SoundManager::current()->preload("sounds/crystallo-shardhit.ogg");
  45. }
  46. void
  47. Shard::update(float dt_sec)
  48. {
  49. m_sticky = true;
  50. if (m_physic.get_velocity() != Vector(0.f, 0.f) && !m_sticking)
  51. m_sprite->set_angle(math::degrees(math::angle(Vector(m_physic.get_velocity_x(), m_physic.get_velocity_y()))));
  52. if (m_stick_timer.check())
  53. m_fadeout_timer.start(FADEOUT_TIME);
  54. if (m_fadeout_timer.check())
  55. remove_me();
  56. else if (m_fadeout_timer.started())
  57. m_sprite->set_alpha(1.0f - m_fadeout_timer.get_progress());
  58. m_col.set_movement(m_physic.get_movement(dt_sec));
  59. StickyObject::update(dt_sec);
  60. }
  61. void
  62. Shard::collision_solid(const CollisionHit& hit)
  63. {
  64. m_physic.set_velocity(0.f, 0.f);
  65. m_physic.set_acceleration(0.f, 0.f);
  66. m_physic.enable_gravity(hit.bottom);
  67. m_sticking = true;
  68. if (!m_stick_timer.started())
  69. {
  70. m_stick_timer.start(STICKING_TIME);
  71. SoundManager::current()->play("sounds/crystallo-shardhit.ogg", get_pos());
  72. }
  73. }
  74. HitResponse
  75. Shard::collision(MovingObject& other, const CollisionHit&)
  76. {
  77. // Do not hurt anyone while fading out
  78. if (m_fadeout_timer.started())
  79. return ABORT_MOVE;
  80. // ignore collisions with other shards
  81. auto shard = dynamic_cast<Shard*>(&other);
  82. if (&other == shard)
  83. return ABORT_MOVE;
  84. // kill badguys
  85. auto badguy = dynamic_cast<BadGuy*>(&other);
  86. if (badguy != nullptr)
  87. badguy->kill_fall();
  88. // kill players
  89. auto player = dynamic_cast<Player*>(&other);
  90. if (player != nullptr)
  91. player->kill(false);
  92. return ABORT_MOVE;
  93. }
  94. /* EOF */