1 // SuperTux - Thunderstorm Game Object
2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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/>.
17 #include "object/thunderstorm.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "object/electrifier.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25 #include "util/reader.hpp"
28 const float LIGHTNING_DELAY = 2.0f;
29 const float FLASH_DISPLAY_TIME = 0.1f;
30 const float ELECTRIFY_TIME = 0.5f;
33 Thunderstorm::Thunderstorm(const Reader& reader) :
36 layer(LAYER_BACKGROUNDTILES-1),
41 reader.get("name", name);
42 reader.get("running", running);
43 reader.get("interval", interval);
45 log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
47 layer = reader_get_layer (reader, /* default = */ LAYER_BACKGROUNDTILES-1);
49 SoundManager::current()->preload("sounds/thunder.wav");
50 SoundManager::current()->preload("sounds/lightning.wav");
53 running = false; // else start() is ignored
59 Thunderstorm::update(float )
62 if (time_to_thunder.check()) {
64 time_to_lightning.start(LIGHTNING_DELAY);
66 if (time_to_lightning.check()) {
68 time_to_thunder.start(interval);
73 Thunderstorm::draw(DrawingContext& context)
75 if (!flash_display_timer.started()) return;
78 context.push_transform();
79 context.set_translation(Vector(0, 0));
80 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), layer);
81 context.pop_transform();
86 Thunderstorm::expose(HSQUIRRELVM vm, SQInteger table_idx)
88 if (name == "") return;
89 scripting::Thunderstorm* _this = new scripting::Thunderstorm(this);
90 expose_object(vm, table_idx, _this, name, true);
94 Thunderstorm::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
96 if (name == "") return;
97 scripting::unexpose_object(vm, table_idx, name);
101 Thunderstorm::start()
105 time_to_thunder.start(interval);
106 time_to_lightning.stop();
112 if (!running) return;
114 time_to_thunder.stop();
115 time_to_lightning.stop();
119 Thunderstorm::thunder()
121 SoundManager::current()->play("sounds/thunder.wav");
125 Thunderstorm::lightning()
132 Thunderstorm::flash()
134 SoundManager::current()->play("sounds/lightning.wav");
135 flash_display_timer.start(FLASH_DISPLAY_TIME);
139 Thunderstorm::electrify()
141 Sector::current()->add_object(std::make_shared<Electrifier>(200, 1421, ELECTRIFY_TIME));
142 Sector::current()->add_object(std::make_shared<Electrifier>(201, 1422, ELECTRIFY_TIME));