growup.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/growup.hpp"
  17. #include <math.h>
  18. #include "audio/sound_manager.hpp"
  19. #include "math/util.hpp"
  20. #include "object/player.hpp"
  21. #include "sprite/sprite.hpp"
  22. #include "sprite/sprite_manager.hpp"
  23. GrowUp::GrowUp(const Vector& pos, Direction direction, const std::string& custom_sprite) :
  24. MovingSprite(pos, custom_sprite.empty() ? "images/powerups/egg/egg.sprite" : custom_sprite, LAYER_OBJECTS, COLGROUP_MOVING),
  25. physic(),
  26. m_custom_sprite(!custom_sprite.empty()),
  27. shadesprite(SpriteManager::current()->create("images/powerups/egg/egg.sprite")),
  28. lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
  29. {
  30. physic.enable_gravity(true);
  31. physic.set_velocity_x((direction == Direction::LEFT) ? -100.0f : 100.0f);
  32. SoundManager::current()->preload("sounds/grow.ogg");
  33. // Set the shadow action for the egg sprite, so it remains in place as the egg rolls.
  34. shadesprite->set_action("shadow");
  35. // Configure the light sprite for the glow effect.
  36. lightsprite->set_blend(Blend::ADD);
  37. lightsprite->set_color(Color(0.2f, 0.2f, 0.0f));
  38. }
  39. void
  40. GrowUp::update(float dt_sec)
  41. {
  42. if (!m_custom_sprite && physic.get_velocity_x() != 0)
  43. m_sprite->set_angle(get_pos().x * 360.0f / (32.0f * math::PI));
  44. m_col.set_movement(physic.get_movement(dt_sec));
  45. }
  46. void
  47. GrowUp::draw(DrawingContext& context)
  48. {
  49. MovingSprite::draw(context);
  50. if (m_custom_sprite)
  51. return;
  52. shadesprite->draw(context.color(), get_pos(), m_layer);
  53. lightsprite->draw(context.light(), get_bbox().get_middle(), 0);
  54. }
  55. void
  56. GrowUp::collision_solid(const CollisionHit& hit)
  57. {
  58. if (hit.top)
  59. physic.set_velocity_y(0);
  60. if (hit.bottom && physic.get_velocity_y() > 0)
  61. physic.set_velocity_y(0);
  62. if (hit.left || hit.right) {
  63. physic.set_velocity_x(-physic.get_velocity_x());
  64. }
  65. }
  66. HitResponse
  67. GrowUp::collision(GameObject& other, const CollisionHit& hit )
  68. {
  69. auto player = dynamic_cast<Player*>(&other);
  70. if (player != nullptr) {
  71. if (!player->add_bonus(GROWUP_BONUS, true)) {
  72. // Tux can't grow right now.
  73. collision_solid( hit );
  74. return ABORT_MOVE;
  75. }
  76. SoundManager::current()->play("sounds/grow.ogg", get_pos());
  77. remove_me();
  78. return ABORT_MOVE;
  79. }
  80. return FORCE_MOVE;
  81. }
  82. void
  83. GrowUp::do_jump()
  84. {
  85. physic.set_velocity_y(-300);
  86. }
  87. /* EOF */