thunderstorm.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SuperTux - Thunderstorm Game Object
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/thunderstorm.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "editor/editor.hpp"
  19. #include "object/background.hpp"
  20. #include "object/electrifier.hpp"
  21. #include "supertux/level.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "supertux/tile_manager.hpp"
  24. #include "supertux/tile_set.hpp"
  25. #include "util/reader.hpp"
  26. #include "util/reader_mapping.hpp"
  27. #include "video/drawing_context.hpp"
  28. namespace {
  29. const float LIGHTNING_DELAY = 2.0f;
  30. const float FLASH_DISPLAY_TIME = 1.3f;
  31. const float ELECTRIFY_TIME = 0.5f;
  32. const float RESTORE_BACKGROUND_COLOR_TIME = 0.1f;
  33. } // namespace
  34. Thunderstorm::Thunderstorm(const ReaderMapping& reader) :
  35. GameObject(reader),
  36. ExposedObject<Thunderstorm, scripting::Thunderstorm>(this),
  37. running(true),
  38. interval(10.0f),
  39. layer(LAYER_BACKGROUNDTILES-1),
  40. m_strike_script(),
  41. time_to_thunder(),
  42. time_to_lightning(),
  43. flash_display_timer(),
  44. restore_background_color_timer(),
  45. m_background_colors(),
  46. changing_tiles(TileManager::current()->get_tileset(Level::current()->get_tileset())->m_thunderstorm_tiles),
  47. m_flash_color()
  48. {
  49. reader.get("running", running);
  50. reader.get("interval", interval);
  51. reader.get("strike-script", m_strike_script, "");
  52. if (interval <= 0) {
  53. log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
  54. }
  55. layer = reader_get_layer (reader, LAYER_BACKGROUNDTILES - 1);
  56. SoundManager::current()->preload("sounds/thunder.wav");
  57. SoundManager::current()->preload("sounds/lightning.wav");
  58. if (running) {
  59. running = false; // else start() is ignored
  60. start();
  61. }
  62. }
  63. ObjectSettings
  64. Thunderstorm::get_settings()
  65. {
  66. ObjectSettings result = GameObject::get_settings();
  67. result.add_int(_("Z-pos"), &layer, "z-pos", LAYER_BACKGROUNDTILES - 1);
  68. result.add_bool(_("Running"), &running, "running", true);
  69. result.add_float(_("Interval"), &interval, "interval", 10.0f);
  70. result.add_text(_("Strike Script"), &m_strike_script, "strike-script");
  71. result.reorder({"interval", "name", "z-pos" "strike-script"});
  72. result.add_remove();
  73. return result;
  74. }
  75. void
  76. Thunderstorm::update(float )
  77. {
  78. // need this out here for lone lightning strikes
  79. if (restore_background_color_timer.check()) {
  80. restore_background_colors();
  81. restore_background_color_timer.stop();
  82. }
  83. if (flash_display_timer.started())
  84. {
  85. float alpha = 0.9f;
  86. if (flash_display_timer.get_timegone() > 0.1f)
  87. {
  88. auto progress = flash_display_timer.get_timegone() / flash_display_timer.get_timeleft() - 0.1f;
  89. if (progress < 0.0f)
  90. progress = 0.0f;
  91. alpha = 0.9f - progress;
  92. }
  93. if (alpha < 0.0f)
  94. {
  95. flash_display_timer.stop();
  96. return;
  97. }
  98. m_flash_color = Color(alpha, alpha, alpha, 1.0);
  99. }
  100. if (!running) return;
  101. if (time_to_thunder.check()) {
  102. thunder();
  103. time_to_lightning.start(LIGHTNING_DELAY);
  104. }
  105. if (time_to_lightning.check()) {
  106. lightning_in_sequence();
  107. time_to_thunder.start(interval);
  108. }
  109. }
  110. void
  111. Thunderstorm::draw(DrawingContext& context)
  112. {
  113. if (!flash_display_timer.started()) return;
  114. context.push_transform();
  115. context.set_translation(Vector(0, 0));
  116. context.transform().scale = 1.f;
  117. context.color().draw_gradient(m_flash_color, m_flash_color, 500, GradientDirection::HORIZONTAL, context.get_rect(), Blend::ADD);
  118. context.pop_transform();
  119. }
  120. void
  121. Thunderstorm::start()
  122. {
  123. if (running) return;
  124. running = true;
  125. time_to_thunder.start(interval);
  126. time_to_lightning.stop();
  127. }
  128. void
  129. Thunderstorm::stop()
  130. {
  131. if (!running) return;
  132. running = false;
  133. time_to_thunder.stop();
  134. time_to_lightning.stop();
  135. }
  136. void
  137. Thunderstorm::thunder()
  138. {
  139. SoundManager::current()->play("sounds/thunder.wav");
  140. change_background_colors(false);
  141. }
  142. void
  143. Thunderstorm::lightning_general()
  144. {
  145. flash();
  146. electrify();
  147. if (!m_strike_script.empty()) {
  148. Sector::get().run_script(m_strike_script, "strike-script");
  149. }
  150. }
  151. void
  152. Thunderstorm::lightning()
  153. {
  154. lightning_general();
  155. change_background_colors(true, true);
  156. }
  157. void
  158. Thunderstorm::lightning_in_sequence()
  159. {
  160. lightning_general();
  161. change_background_colors(true, false);
  162. }
  163. void
  164. Thunderstorm::flash()
  165. {
  166. SoundManager::current()->play("sounds/lightning.wav");
  167. flash_display_timer.start(FLASH_DISPLAY_TIME);
  168. }
  169. void
  170. Thunderstorm::electrify()
  171. {
  172. Sector::get().add<Electrifier>(changing_tiles, ELECTRIFY_TIME);
  173. }
  174. void
  175. Thunderstorm::change_background_colors(bool is_lightning, bool is_scripted)
  176. {
  177. auto factor = is_lightning ? (1.0f / 0.7f) : 0.7f;
  178. auto backgrounds = Sector::current()->get_objects_by_type<Background>();
  179. for(auto& background : backgrounds)
  180. {
  181. auto color = background.get_color();
  182. auto new_color = color * factor;
  183. if (is_scripted) {
  184. m_background_colors.push_back(color);
  185. }
  186. new_color.a = color.alpha;
  187. background.fade_color(new_color.validate(), RESTORE_BACKGROUND_COLOR_TIME);
  188. }
  189. if (is_scripted) {
  190. restore_background_color_timer.start(RESTORE_BACKGROUND_COLOR_TIME);
  191. }
  192. }
  193. void
  194. Thunderstorm::restore_background_colors()
  195. {
  196. auto backgrounds = Sector::current()->get_objects_by_type<Background>();
  197. for (auto& background : backgrounds)
  198. {
  199. auto color = m_background_colors.front();
  200. background.fade_color(color, RESTORE_BACKGROUND_COLOR_TIME);
  201. m_background_colors.pop_front();
  202. }
  203. }
  204. /* EOF */