spotlight.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SuperTux
  2. // Copyright (C) 2006 Ingo Ruhnke <grumbel@gmail.com>
  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/spotlight.hpp"
  17. #include "sprite/sprite.hpp"
  18. #include "sprite/sprite_manager.hpp"
  19. #include "util/reader_mapping.hpp"
  20. Spotlight::Direction
  21. Spotlight::Direction_from_string(const std::string& s)
  22. {
  23. if (s == "clockwise") {
  24. return Direction::CLOCKWISE;
  25. } else if (s == "counter-clockwise") {
  26. return Direction::COUNTERCLOCKWISE;
  27. } else if (s == "stopped") {
  28. return Direction::STOPPED;
  29. }
  30. throw std::runtime_error("Invalid spotlight direction from string '" + s + "'");
  31. }
  32. std::string
  33. Spotlight::Direction_to_string(Direction dir)
  34. {
  35. switch(dir) {
  36. case Direction::CLOCKWISE:
  37. return "clockwise";
  38. case Direction::COUNTERCLOCKWISE:
  39. return "counter-clockwise";
  40. case Direction::STOPPED:
  41. return "stopped";
  42. }
  43. throw std::runtime_error("Invalid spotlight direction '" + std::to_string(static_cast<int>(dir)) + "'");
  44. }
  45. Spotlight::Spotlight(const ReaderMapping& mapping) :
  46. MovingObject(mapping),
  47. ExposedObject<Spotlight, scripting::Spotlight>(this),
  48. angle(),
  49. center(SpriteManager::current()->create("images/objects/spotlight/spotlight_center.sprite")),
  50. base(SpriteManager::current()->create("images/objects/spotlight/spotlight_base.sprite")),
  51. lights(SpriteManager::current()->create("images/objects/spotlight/spotlight_lights.sprite")),
  52. light(SpriteManager::current()->create("images/objects/spotlight/light.sprite")),
  53. lightcone(SpriteManager::current()->create("images/objects/spotlight/lightcone.sprite")),
  54. color(1.0f, 1.0f, 1.0f),
  55. speed(50.0f),
  56. m_direction(),
  57. m_layer(0),
  58. m_enabled(true)
  59. {
  60. m_col.m_group = COLGROUP_DISABLED;
  61. mapping.get("x", m_col.m_bbox.get_left(), 0.0f);
  62. mapping.get("y", m_col.m_bbox.get_top(), 0.0f);
  63. m_col.m_bbox.set_size(32, 32);
  64. mapping.get("angle", angle, 0.0f);
  65. mapping.get("speed", speed, 50.0f);
  66. if (!mapping.get_custom("r-direction", m_direction, Direction_from_string))
  67. {
  68. // Retrocompatibility
  69. bool counter_clockwise;
  70. mapping.get("counter-clockwise", counter_clockwise, false);
  71. m_direction = counter_clockwise ? Direction::COUNTERCLOCKWISE : Direction::CLOCKWISE;
  72. }
  73. std::vector<float> vColor;
  74. if ( mapping.get( "color", vColor ) ){
  75. color = Color( vColor );
  76. }
  77. mapping.get("layer", m_layer, 0);
  78. mapping.get("enabled", m_enabled, true);
  79. }
  80. Spotlight::~Spotlight()
  81. {
  82. }
  83. ObjectSettings
  84. Spotlight::get_settings()
  85. {
  86. ObjectSettings result = MovingObject::get_settings();
  87. result.add_bool(_("Enabled"), &m_enabled, "enabled", true);
  88. result.add_float(_("Angle"), &angle, "angle");
  89. result.add_color(_("Color"), &color, "color", Color::WHITE);
  90. result.add_float(_("Speed"), &speed, "speed", 50.0f);
  91. result.add_enum(_("Direction"), reinterpret_cast<int*>(&m_direction),
  92. {_("Clockwise"), _("Counter-clockwise"), _("Stopped")},
  93. {"clockwise", "counter-clockwise", "stopped"},
  94. static_cast<int>(Direction::CLOCKWISE), "r-direction");
  95. result.add_int(_("Layer"), &m_layer, "layer", 0);
  96. result.reorder({"angle", "color", "layer", "x", "y"});
  97. return result;
  98. }
  99. void
  100. Spotlight::update(float dt_sec)
  101. {
  102. GameObject::update(dt_sec);
  103. if (!m_enabled)
  104. return;
  105. switch (m_direction)
  106. {
  107. case Direction::CLOCKWISE:
  108. angle += dt_sec * speed;
  109. break;
  110. case Direction::COUNTERCLOCKWISE:
  111. angle -= dt_sec * speed;
  112. break;
  113. case Direction::STOPPED:
  114. break;
  115. }
  116. }
  117. void
  118. Spotlight::draw(DrawingContext& context)
  119. {
  120. if (m_enabled)
  121. {
  122. light->set_color(color);
  123. light->set_blend(Blend::ADD);
  124. light->set_angle(angle);
  125. light->draw(context.light(), m_col.m_bbox.p1(), m_layer);
  126. //lightcone->set_angle(angle);
  127. //lightcone->draw(context.color(), position, m_layer);
  128. lights->set_angle(angle);
  129. lights->draw(context.color(), m_col.m_bbox.p1(), m_layer);
  130. }
  131. base->set_angle(angle);
  132. base->draw(context.color(), m_col.m_bbox.p1(), m_layer);
  133. center->draw(context.color(), m_col.m_bbox.p1(), m_layer);
  134. if (m_enabled)
  135. {
  136. lightcone->set_angle(angle);
  137. lightcone->draw(context.color(), m_col.m_bbox.p1(), LAYER_FOREGROUND1 + 10);
  138. }
  139. }
  140. HitResponse
  141. Spotlight::collision(GameObject& other, const CollisionHit& hit_)
  142. {
  143. return FORCE_MOVE;
  144. }
  145. /* EOF */