firefly.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/firefly.hpp"
  17. #include <math.h>
  18. #include "audio/sound_manager.hpp"
  19. #include "math/random.hpp"
  20. #include "math/util.hpp"
  21. #include "object/player.hpp"
  22. #include "object/sprite_particle.hpp"
  23. #include "sprite/sprite.hpp"
  24. #include "sprite/sprite_manager.hpp"
  25. #include "supertux/flip_level_transformer.hpp"
  26. #include "supertux/game_session.hpp"
  27. #include "supertux/sector.hpp"
  28. #include "util/reader_mapping.hpp"
  29. static const Color TORCH_LIGHT_COLOR = Color(0.87f, 0.64f, 0.12f); /** Color of the light specific to the torch firefly sprite. */
  30. static const Vector TORCH_LIGHT_OFFSET = Vector(0, 12); /** Offset of the light specific to the torch firefly sprite. */
  31. Firefly::Firefly(const ReaderMapping& mapping) :
  32. MovingSprite(mapping, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
  33. m_sprite_light(),
  34. activated(false),
  35. initial_position(get_pos())
  36. {
  37. if (m_sprite_name.find("torch", 0) != std::string::npos) {
  38. m_sprite_light = SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite");
  39. m_sprite_light->set_blend(Blend::ADD);
  40. m_sprite_light->set_color(TORCH_LIGHT_COLOR);
  41. }
  42. update_state();
  43. // Load sound.
  44. if ( m_sprite_name.find("vbell", 0) != std::string::npos ) {
  45. SoundManager::current()->preload("sounds/savebell_low.wav");
  46. }
  47. else if ( m_sprite_name.find("torch", 0) != std::string::npos ) {
  48. SoundManager::current()->preload("sounds/fire.ogg");
  49. }
  50. else {
  51. SoundManager::current()->preload("sounds/savebell2.wav");
  52. }
  53. }
  54. void
  55. Firefly::draw(DrawingContext& context)
  56. {
  57. MovingSprite::draw(context);
  58. if (m_sprite_name.find("torch", 0) != std::string::npos && (activated ||
  59. m_sprite->get_action() == "ringing")) {
  60. m_sprite_light->draw(context.light(), m_col.m_bbox.get_middle() + (m_flip == NO_FLIP ? -TORCH_LIGHT_OFFSET : TORCH_LIGHT_OFFSET), 0);
  61. }
  62. }
  63. void
  64. Firefly::update(float dt_sec)
  65. {
  66. MovingSprite::update(dt_sec);
  67. update_state(); // Update the state of the checkpoint.
  68. }
  69. void
  70. Firefly::update_state()
  71. {
  72. if (!GameSession::current()) return;
  73. auto* active_checkpoint_spawnpoint = GameSession::current()->get_active_checkpoint_spawnpoint();
  74. if (active_checkpoint_spawnpoint &&
  75. active_checkpoint_spawnpoint->sector == Sector::get().get_name() &&
  76. active_checkpoint_spawnpoint->position == initial_position) // Is activated.
  77. {
  78. set_action("ringing");
  79. }
  80. else // Is deactivated.
  81. {
  82. set_action("normal");
  83. }
  84. }
  85. HitResponse
  86. Firefly::collision(GameObject& other, const CollisionHit& )
  87. {
  88. // If the bell is already activated, don't ring it again!
  89. if (activated || m_sprite->get_action() == "ringing")
  90. return ABORT_MOVE;
  91. auto player = dynamic_cast<Player*> (&other);
  92. if (player) {
  93. activated = true;
  94. // Spawn some particles.
  95. // TODO: provide convenience function in MovingSprite or MovingObject?
  96. for (int i = 0; i < 5; i++) {
  97. Vector ppos = m_col.m_bbox.get_middle();
  98. float angle = graphicsRandom.randf(-math::PI_2, math::PI_2);
  99. float velocity = graphicsRandom.randf(450.0f, 900.0f);
  100. float vx = sinf(angle)*velocity;
  101. float vy = -cosf(angle)*velocity;
  102. Vector pspeed = Vector(vx, vy);
  103. Vector paccel = Vector(0.0f, 1000.0f);
  104. Sector::get().add<SpriteParticle>("images/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1);
  105. }
  106. if ( m_sprite_name.find("vbell", 0) != std::string::npos ) {
  107. SoundManager::current()->play("sounds/savebell_low.wav", get_pos());
  108. }
  109. else if ( m_sprite_name.find("torch", 0) != std::string::npos) {
  110. SoundManager::current()->play("sounds/fire.ogg", get_pos());
  111. }
  112. else {
  113. SoundManager::current()->play("sounds/savebell2.wav", get_pos());
  114. }
  115. set_action("ringing");
  116. GameSession::current()->set_checkpoint_pos(Sector::get().get_name(),
  117. initial_position);
  118. }
  119. return ABORT_MOVE;
  120. }
  121. ObjectSettings
  122. Firefly::get_settings()
  123. {
  124. ObjectSettings result = MovingSprite::get_settings();
  125. result.add_test_from_here();
  126. return result;
  127. }
  128. void
  129. Firefly::on_flip(float height)
  130. {
  131. MovingSprite::on_flip(height);
  132. FlipLevelTransformer::transform_flip(m_flip);
  133. }
  134. /* EOF */