invisible_block.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/invisible_block.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "editor/editor.hpp"
  19. #include "object/player.hpp"
  20. #include "supertux/constants.hpp"
  21. InvisibleBlock::InvisibleBlock(const ReaderMapping& mapping) :
  22. Block(mapping, "images/objects/bonus_block/invisibleblock.sprite"),
  23. visible(false)
  24. {
  25. parse_type(mapping);
  26. SoundManager::current()->preload("sounds/brick.wav");
  27. }
  28. GameObjectTypes
  29. InvisibleBlock::get_types() const
  30. {
  31. return {
  32. { "normal", _("Normal") },
  33. { "retro", _("Retro") }
  34. };
  35. }
  36. std::string
  37. InvisibleBlock::get_default_sprite_name() const
  38. {
  39. switch (m_type)
  40. {
  41. case RETRO:
  42. return "images/objects/bonus_block/retro_invisibleblock.sprite";
  43. default:
  44. return m_default_sprite_name;
  45. }
  46. }
  47. void
  48. InvisibleBlock::draw(DrawingContext& context)
  49. {
  50. if (visible || Editor::is_active())
  51. m_sprite->draw(context.color(), get_pos(), LAYER_OBJECTS);
  52. }
  53. bool
  54. InvisibleBlock::collides(GameObject& other, const CollisionHit& ) const
  55. {
  56. if (visible)
  57. return true;
  58. // if we're not visible, only register a collision if this will make us visible
  59. auto player = dynamic_cast<Player*> (&other);
  60. if ((player)
  61. && (player->get_movement().y <= 0)
  62. && (player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA)) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. HitResponse
  68. InvisibleBlock::collision(GameObject& other, const CollisionHit& hit_)
  69. {
  70. return Block::collision(other, hit_);
  71. }
  72. void
  73. InvisibleBlock::hit(Player& player)
  74. {
  75. SoundManager::current()->play("sounds/brick.wav", get_pos());
  76. if (visible)
  77. return;
  78. set_action("empty");
  79. start_bounce(&player);
  80. set_group(COLGROUP_STATIC);
  81. visible = true;
  82. }
  83. /* EOF */