thunderstorm.cpp 6.4 KB

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