torch.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <simplesquirrel/class.hpp>
  19. #include <simplesquirrel/vm.hpp>
  20. #include "object/player.hpp"
  21. #include "sprite/sprite.hpp"
  22. #include "sprite/sprite_manager.hpp"
  23. #include "supertux/flip_level_transformer.hpp"
  24. #include "util/reader_mapping.hpp"
  25. Torch::Torch(const ReaderMapping& reader) :
  26. MovingSprite(reader, "images/objects/torch/torch1.sprite", LAYER_TILES),
  27. m_light_color(1.f, 1.f, 1.f),
  28. m_flame(SpriteManager::current()->create("images/objects/torch/flame.sprite")),
  29. m_flame_glow(SpriteManager::current()->create("images/objects/torch/flame_glow.sprite")),
  30. m_flame_light(SpriteManager::current()->create("images/objects/torch/flame_light.sprite")),
  31. m_burning(true)
  32. {
  33. reader.get("burning", m_burning, true);
  34. reader.get("layer", m_layer); // Backwards compatibility
  35. std::vector<float> vColor;
  36. if (!reader.get("color", vColor)) vColor = { 1.f, 1.f, 1.f };
  37. m_flame_glow->set_blend(Blend::ADD);
  38. m_flame_light->set_blend(Blend::ADD);
  39. if (vColor.size() >= 3)
  40. {
  41. m_light_color = Color(vColor);
  42. m_flame->set_color(m_light_color);
  43. m_flame_glow->set_color(m_light_color);
  44. m_flame_light->set_color(m_light_color);
  45. }
  46. set_group(COLGROUP_TOUCHABLE);
  47. }
  48. void
  49. Torch::draw(DrawingContext& context)
  50. {
  51. if (m_burning)
  52. {
  53. Vector pos = get_pos();
  54. if (m_flip != NO_FLIP) pos.y -= 24.0f;
  55. m_flame->draw(context.color(), pos, m_layer - 1, m_flip);
  56. m_flame->set_action(m_light_color.greyscale() >= 1.f ? "default" : "greyscale");
  57. m_flame_light->draw(context.light(), pos, m_layer);
  58. m_flame_light->set_action(m_light_color.greyscale() >= 1.f ? "default" : "greyscale");
  59. m_flame_glow->draw(context.color(), pos, m_layer - 1, m_flip);
  60. m_flame_glow->set_action(m_light_color.greyscale() >= 1.f ? "default" : "greyscale");
  61. }
  62. m_sprite->draw(context.color(), get_pos(), m_layer - 1, m_flip);
  63. }
  64. void
  65. Torch::update(float)
  66. {
  67. }
  68. HitResponse
  69. Torch::collision(MovingObject& other, const CollisionHit& )
  70. {
  71. const auto* player = dynamic_cast<Player*>(&other);
  72. if (player != nullptr && !m_burning)
  73. {
  74. m_burning = true;
  75. }
  76. return ABORT_MOVE;
  77. }
  78. ObjectSettings
  79. Torch::get_settings()
  80. {
  81. ObjectSettings result = MovingSprite::get_settings();
  82. result.add_bool(_("Burning"), &m_burning, "burning", true);
  83. result.add_color(_("Color"), &m_light_color, "color", Color::WHITE);
  84. result.reorder({"burning", "sprite", "z-pos", "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. void
  96. Torch::on_flip(float height)
  97. {
  98. MovingObject::on_flip(height);
  99. FlipLevelTransformer::transform_flip(m_flip);
  100. }
  101. void
  102. Torch::register_class(ssq::VM& vm)
  103. {
  104. ssq::Class cls = vm.addAbstractClass<Torch>("Torch", vm.findClass("MovingSprite"));
  105. cls.addFunc("get_burning", &Torch::get_burning);
  106. cls.addFunc("set_burning", &Torch::set_burning);
  107. cls.addVar("burning", &Torch::m_burning);
  108. }
  109. /* EOF */