00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/thunderstorm.hpp"
00018
00019 #include "audio/sound_manager.hpp"
00020 #include "object/electrifier.hpp"
00021 #include "scripting/squirrel_util.hpp"
00022 #include "supertux/globals.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "supertux/sector.hpp"
00025 #include "util/reader.hpp"
00026
00027 namespace {
00028 const float LIGHTNING_DELAY = 2.0f;
00029 const float FLASH_DISPLAY_TIME = 0.1f;
00030 const float ELECTRIFY_TIME = 0.5f;
00031 }
00032
00033 Thunderstorm::Thunderstorm(const Reader& reader) :
00034 running(true),
00035 interval(10.0f),
00036 layer(LAYER_BACKGROUNDTILES-1),
00037 time_to_thunder(),
00038 time_to_lightning(),
00039 flash_display_timer()
00040 {
00041 reader.get("name", name);
00042 reader.get("running", running);
00043 reader.get("interval", interval);
00044 if(interval <= 0) {
00045 log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
00046 }
00047 layer = reader_get_layer (reader, LAYER_BACKGROUNDTILES-1);
00048
00049 sound_manager->preload("sounds/thunder.wav");
00050 sound_manager->preload("sounds/lightning.wav");
00051
00052 if (running) {
00053 running = false;
00054 start();
00055 }
00056 }
00057
00058 void
00059 Thunderstorm::update(float )
00060 {
00061 if (!running) return;
00062 if (time_to_thunder.check()) {
00063 thunder();
00064 time_to_lightning.start(LIGHTNING_DELAY);
00065 }
00066 if (time_to_lightning.check()) {
00067 lightning();
00068 time_to_thunder.start(interval);
00069 }
00070 }
00071
00072 void
00073 Thunderstorm::draw(DrawingContext& context)
00074 {
00075 if (!flash_display_timer.started()) return;
00076
00077 float alpha = 0.33f;
00078 context.push_transform();
00079 context.set_translation(Vector(0, 0));
00080 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), layer);
00081 context.pop_transform();
00082
00083 }
00084
00085 void
00086 Thunderstorm::expose(HSQUIRRELVM vm, SQInteger table_idx)
00087 {
00088 if (name == "") return;
00089 scripting::Thunderstorm* _this = new scripting::Thunderstorm(this);
00090 expose_object(vm, table_idx, _this, name, true);
00091 }
00092
00093 void
00094 Thunderstorm::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00095 {
00096 if (name == "") return;
00097 scripting::unexpose_object(vm, table_idx, name);
00098 }
00099
00100 void
00101 Thunderstorm::start()
00102 {
00103 if (running) return;
00104 running = true;
00105 time_to_thunder.start(interval);
00106 time_to_lightning.stop();
00107 }
00108
00109 void
00110 Thunderstorm::stop()
00111 {
00112 if (!running) return;
00113 running = false;
00114 time_to_thunder.stop();
00115 time_to_lightning.stop();
00116 }
00117
00118 void
00119 Thunderstorm::thunder()
00120 {
00121 sound_manager->play("sounds/thunder.wav");
00122 }
00123
00124 void
00125 Thunderstorm::lightning()
00126 {
00127 flash();
00128 electrify();
00129 }
00130
00131 void
00132 Thunderstorm::flash()
00133 {
00134 sound_manager->play("sounds/lightning.wav");
00135 flash_display_timer.start(FLASH_DISPLAY_TIME);
00136 }
00137
00138 void
00139 Thunderstorm::electrify()
00140 {
00141 Sector::current()->add_object(new Electrifier(200, 1421, ELECTRIFY_TIME));
00142 Sector::current()->add_object(new Electrifier(201, 1422, ELECTRIFY_TIME));
00143 }
00144
00145