rublight.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SuperTux
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #include "object/rublight.hpp"
  16. #include "object/explosion.hpp"
  17. #include "badguy/walking_badguy.hpp"
  18. #include "object/player.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "sprite/sprite_manager.hpp"
  21. #include "supertux/constants.hpp"
  22. #include "supertux/flip_level_transformer.hpp"
  23. #include "video/color.hpp"
  24. #include "util/reader_mapping.hpp"
  25. RubLight::RubLight(const ReaderMapping& mapping) :
  26. MovingSprite(mapping, "images/objects/rublight/rublight.sprite", LAYER_TILES,
  27. COLGROUP_STATIC),
  28. state(STATE_DARK),
  29. stored_energy(0),
  30. light(SpriteManager::current()->create(
  31. "images/objects/lightmap_light/lightmap_light.sprite")),
  32. color(1.f, 1.f, 1.f),
  33. fading_speed(5.0f),
  34. strength_multiplier(1.0f)
  35. {
  36. set_action("inactive");
  37. std::vector<float> vColor;
  38. if (mapping.get("color", vColor))
  39. color = Color(vColor);
  40. mapping.get("fading_speed", fading_speed);
  41. mapping.get("strength_multiplier", strength_multiplier);
  42. }
  43. ObjectSettings
  44. RubLight::get_settings()
  45. {
  46. ObjectSettings result = MovingSprite::get_settings();
  47. // The object settings and their default values shown in the Editor
  48. result.add_color(_("Color"), &color, "color", Color(1.0f, 0.5f, 0.3f));
  49. result.add_float(_("Fading Speed"), &fading_speed, "fading_speed", 5.0f);
  50. result.add_float(_("Glowing Strength"), &strength_multiplier,
  51. "strength_multiplier", 1.0f);
  52. result.reorder({"color", "fading_speed", "x", "y"});
  53. return result;
  54. }
  55. HitResponse
  56. RubLight::collision(GameObject& other, const CollisionHit&)
  57. {
  58. Player* player = dynamic_cast<Player*>(&other);
  59. if (player != nullptr &&
  60. player->get_bbox().get_bottom() < m_col.m_bbox.get_top() + SHIFT_DELTA) {
  61. Vector vel_player = player->get_velocity();
  62. float vel_horiz = fabsf(vel_player.x) / 32.0f;
  63. if (player->is_skidding())
  64. rub(vel_horiz * 0.3f);
  65. else
  66. rub(vel_horiz * 0.01f);
  67. return FORCE_MOVE;
  68. }
  69. if (dynamic_cast<Explosion*> (&other)) {
  70. rub(1.0f);
  71. return FORCE_MOVE;
  72. }
  73. WalkingBadguy* obj = dynamic_cast<WalkingBadguy*>(&other);
  74. if (obj != nullptr) {
  75. float vel_horiz = fabsf(obj->get_velocity_x()) / 32.0f;
  76. rub(vel_horiz * 0.01f);
  77. }
  78. return FORCE_MOVE;
  79. }
  80. void RubLight::rub(float strength)
  81. {
  82. if (strength <= 0)
  83. return;
  84. set_action("active");
  85. strength *= strength_multiplier;
  86. stored_energy = std::max<float>(stored_energy, strength);
  87. if (state == STATE_DARK)
  88. state = STATE_FADING;
  89. }
  90. void
  91. RubLight::update(float dt_sec)
  92. {
  93. if (m_sprite->get_action() == "active" && m_sprite->animation_done())
  94. {
  95. set_action("inactive");
  96. }
  97. switch (state) {
  98. case STATE_DARK:
  99. set_action("inactive");
  100. break;
  101. case STATE_FADING:
  102. // Exponential fading
  103. stored_energy *= expf(-dt_sec * fading_speed);
  104. if (get_brightness() < 0.000001f) {
  105. stored_energy = 0;
  106. state = STATE_DARK;
  107. }
  108. break;
  109. }
  110. }
  111. float
  112. RubLight::get_brightness() const
  113. {
  114. return stored_energy / (1.0f + stored_energy);
  115. }
  116. void
  117. RubLight::draw(DrawingContext& context)
  118. {
  119. if (state == STATE_FADING) {
  120. float brightness = get_brightness();
  121. Color col = color.multiply_linearly(brightness);
  122. light->set_color(col);
  123. light->set_blend(Blend::ADD);
  124. light->draw(context.light(), get_pos(), m_layer);
  125. }
  126. m_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
  127. }
  128. void
  129. RubLight::on_flip(float height)
  130. {
  131. MovingSprite::on_flip(height);
  132. FlipLevelTransformer::transform_flip(m_flip);
  133. }
  134. /* EOF */