lantern.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SuperTux - Lantern
  2. // Copyright (C) 2006 Wolfgang Becker <uafr@gmx.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/lantern.hpp"
  17. #include <algorithm>
  18. #include "audio/sound_manager.hpp"
  19. #include "badguy/treewillowisp.hpp"
  20. #include "badguy/willowisp.hpp"
  21. #include "editor/editor.hpp"
  22. #include "sprite/sprite.hpp"
  23. #include "sprite/sprite_manager.hpp"
  24. #include "util/reader_mapping.hpp"
  25. Lantern::Lantern(const ReaderMapping& reader) :
  26. Rock(reader, "images/objects/lantern/lantern.sprite"),
  27. lightcolor(1.0f, 1.0f, 1.0f),
  28. lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
  29. {
  30. std::vector<float> vColor;
  31. if (reader.get("color", vColor)) {
  32. lightcolor = Color(vColor);
  33. } else {
  34. if (!Editor::is_active()) {
  35. lightcolor = Color(1, 1, 1);
  36. }
  37. }
  38. lightsprite->set_blend(Blend::ADD);
  39. updateColor();
  40. SoundManager::current()->preload("sounds/willocatch.wav");
  41. }
  42. Lantern::Lantern(const Vector& pos) :
  43. Rock(pos, "images/objects/lantern/lantern.sprite"),
  44. lightcolor(0.0f, 0.0f, 0.0f),
  45. lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
  46. {
  47. lightsprite->set_blend(Blend::ADD);
  48. updateColor();
  49. SoundManager::current()->preload("sounds/willocatch.wav");
  50. }
  51. ObjectSettings
  52. Lantern::get_settings()
  53. {
  54. ObjectSettings result = Rock::get_settings();
  55. result.add_color(_("Color"), &lightcolor, "color", Color::WHITE);
  56. result.reorder({"color", "name", "x", "y"});
  57. return result;
  58. }
  59. void
  60. Lantern::after_editor_set()
  61. {
  62. Rock::after_editor_set();
  63. updateColor();
  64. }
  65. void
  66. Lantern::updateColor(){
  67. lightsprite->set_color(lightcolor);
  68. //Turn lantern off if light is black
  69. if (lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0){
  70. set_action("off");
  71. m_sprite->set_color(Color(1.0f, 1.0f, 1.0f));
  72. } else {
  73. set_action("normal");
  74. m_sprite->set_color(lightcolor);
  75. }
  76. }
  77. void
  78. Lantern::draw(DrawingContext& context){
  79. //Draw the Sprite.
  80. MovingSprite::draw(context);
  81. //Let there be light.
  82. lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0);
  83. }
  84. HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
  85. WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
  86. if (wow && (is_open() || wow->get_color().greyscale() == 0.f)) {
  87. // collided with WillOWisp while grabbed and unlit
  88. SoundManager::current()->play("sounds/willocatch.wav", get_pos());
  89. lightcolor = wow->get_color();
  90. updateColor();
  91. wow->vanish();
  92. }
  93. TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
  94. if (twow && (is_open() || twow->get_color().greyscale() == 0.f)) {
  95. // collided with TreeWillOWisp while grabbed and unlit
  96. SoundManager::current()->play("sounds/willocatch.wav", get_pos());
  97. lightcolor = twow->get_color();
  98. updateColor();
  99. twow->vanish();
  100. }
  101. return Rock::collision(other, hit);
  102. }
  103. void
  104. Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
  105. {
  106. Rock::grab(object, pos, dir);
  107. // if lantern is not lit, draw it as opened
  108. if (is_open()) {
  109. set_action("off-open");
  110. }
  111. }
  112. void
  113. Lantern::ungrab(MovingObject& object, Direction dir)
  114. {
  115. // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
  116. if (is_open()) {
  117. set_action("off");
  118. }
  119. Rock::ungrab(object, dir);
  120. }
  121. bool
  122. Lantern::is_open() const
  123. {
  124. return (is_grabbed() && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
  125. }
  126. void
  127. Lantern::add_color(const Color& c)
  128. {
  129. lightcolor.red = std::min(1.0f, lightcolor.red + c.red);
  130. lightcolor.green = std::min(1.0f, lightcolor.green + c.green);
  131. lightcolor.blue = std::min(1.0f, lightcolor.blue + c.blue);
  132. lightcolor.alpha = std::min(1.0f, lightcolor.alpha + c.alpha);
  133. updateColor();
  134. }
  135. /* EOF */