torch.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SuperTux
  2. // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. // Copyright (C) 2017 M. Teufel <mteufel@supertux.org>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/torch.hpp"
  18. #include "object/player.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "sprite/sprite_manager.hpp"
  21. #include "supertux/flip_level_transformer.hpp"
  22. #include "util/reader_mapping.hpp"
  23. Torch::Torch(const ReaderMapping& reader) :
  24. MovingSprite(reader, "images/objects/torch/torch1.sprite"),
  25. ExposedObject<Torch, scripting::Torch>(this),
  26. m_light_color(1.f, 1.f, 1.f),
  27. m_flame(SpriteManager::current()->create("images/objects/torch/flame.sprite")),
  28. m_flame_glow(SpriteManager::current()->create("images/objects/torch/flame_glow.sprite")),
  29. m_flame_light(SpriteManager::current()->create("images/objects/torch/flame_light.sprite")),
  30. m_burning(true)
  31. {
  32. reader.get("burning", m_burning, true);
  33. reader.get("layer", m_layer, 0);
  34. std::vector<float> vColor;
  35. if (!reader.get("color", vColor)) vColor = { 1.f, 1.f, 1.f };
  36. m_flame_glow->set_blend(Blend::ADD);
  37. m_flame_light->set_blend(Blend::ADD);
  38. if (vColor.size() >= 3)
  39. {
  40. m_light_color = Color(vColor);
  41. m_flame->set_color(m_light_color);
  42. m_flame_glow->set_color(m_light_color);
  43. m_flame_light->set_color(m_light_color);
  44. }
  45. set_group(COLGROUP_TOUCHABLE);
  46. }
  47. void
  48. Torch::draw(DrawingContext& context)
  49. {
  50. if (m_burning)
  51. {
  52. Vector pos = get_pos();
  53. if (m_flip != NO_FLIP) pos.y -= 24.0f;
  54. m_flame->draw(context.color(), pos, m_layer - 1, m_flip);
  55. m_flame->set_action(m_light_color.greyscale() >= 1.f ? "default" : "greyscale");
  56. m_flame_light->draw(context.light(), pos, m_layer);
  57. m_flame_light->set_action(m_light_color.greyscale() >= 1.f ? "default" : "greyscale");
  58. m_flame_glow->draw(context.color(), pos, m_layer - 1, m_flip);
  59. m_flame_glow->set_action(m_light_color.greyscale() >= 1.f ? "default" : "greyscale");
  60. }
  61. m_sprite->draw(context.color(), get_pos(), m_layer - 1, m_flip);
  62. }
  63. void
  64. Torch::update(float)
  65. {
  66. }
  67. HitResponse
  68. Torch::collision(GameObject& other, const CollisionHit& )
  69. {
  70. const auto* player = dynamic_cast<Player*>(&other);
  71. if (player != nullptr && !m_burning)
  72. {
  73. m_burning = true;
  74. }
  75. return ABORT_MOVE;
  76. }
  77. ObjectSettings
  78. Torch::get_settings()
  79. {
  80. ObjectSettings result = MovingSprite::get_settings();
  81. result.add_bool(_("Burning"), &m_burning, "burning", true);
  82. result.add_int(_("Layer"), &m_layer, "layer", 0);
  83. result.add_color(_("Color"), &m_light_color, "color", Color::WHITE);
  84. result.reorder({"burning", "sprite", "layer", "color", "x", "y"});
  85. return result;
  86. }
  87. void
  88. Torch::after_editor_set()
  89. {
  90. MovingSprite::after_editor_set();
  91. m_flame->set_color(m_light_color);
  92. m_flame_glow->set_color(m_light_color);
  93. m_flame_light->set_color(m_light_color);
  94. }
  95. bool
  96. Torch::get_burning() const
  97. {
  98. return m_burning;
  99. }
  100. void
  101. Torch::set_burning(bool burning_)
  102. {
  103. m_burning = burning_;
  104. }
  105. void
  106. Torch::on_flip(float height)
  107. {
  108. MovingObject::on_flip(height);
  109. FlipLevelTransformer::transform_flip(m_flip);
  110. }
  111. /* EOF */