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
17#include "object/thunderstorm.hpp"
18
19#include "audio/sound_manager.hpp"
20#include "editor/editor.hpp"
21#include "object/electrifier.hpp"
22#include "supertux/sector.hpp"
23#include "util/reader.hpp"
24#include "util/reader_mapping.hpp"
25#include "video/drawing_context.hpp"
26
27namespace {
28
29const float LIGHTNING_DELAY = 2.0f;
30const float FLASH_DISPLAY_TIME = 0.1f;
31const float ELECTRIFY_TIME = 0.5f;
32
33} // namespace
34
35Thunderstorm::Thunderstorm(const ReaderMapping& reader) :
36 GameObject(reader),
37 ExposedObject<Thunderstorm, scripting::Thunderstorm>(this),
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{
46 reader.get("running", running);
47 reader.get("interval", interval);
48 reader.get("strike-script", m_strike_script, "");
49 if (interval <= 0) {
50 log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
51 }
52 layer = reader_get_layer (reader, LAYER_BACKGROUNDTILES - 1);
53
54 SoundManager::current()->preload("sounds/thunder.wav");
55 SoundManager::current()->preload("sounds/lightning.wav");
56
57 if (running) {
58 running = false; // else start() is ignored
59 start();
60 }
61}
62
63ObjectSettings
64Thunderstorm::get_settings()
65{
66 ObjectSettings result = GameObject::get_settings();
67
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
73 result.reorder({"interval", "name", "z-pos" "strike-script"});
74
75 result.add_remove();
76
77 return result;
78}
79
80void
81Thunderstorm::update(float )
82{
83 if (!running) return;
84
85 if (time_to_thunder.check()) {
86 thunder();
87 time_to_lightning.start(LIGHTNING_DELAY);
88 }
89 if (time_to_lightning.check()) {
90 lightning();
91 time_to_thunder.start(interval);
92 }
93}
94
95void
96Thunderstorm::draw(DrawingContext& context)
97{
98 if (!flash_display_timer.started()) return;
99
100 float alpha = 0.33f;
101 context.push_transform();
102 context.set_translation(Vector(0, 0));
103 context.color().draw_filled_rect(Rectf(0, 0,
104 static_cast<float>(context.get_width()),
105 static_cast<float>(context.get_height())),
106 Color(1, 1, 1, alpha), layer);
107 context.pop_transform();
108
109}
110
111void
112Thunderstorm::start()
113{
114 if (running) return;
115 running = true;
116 time_to_thunder.start(interval);
117 time_to_lightning.stop();
118}
119
120void
121Thunderstorm::stop()
122{
123 if (!running) return;
124 running = false;
125 time_to_thunder.stop();
126 time_to_lightning.stop();
127}
128
129void
130Thunderstorm::thunder()
131{
132 SoundManager::current()->play("sounds/thunder.wav");
133}
134
135void
136Thunderstorm::lightning()
137{
138 flash();
139 electrify();
140 if (!m_strike_script.empty()) {
141 Sector::get().run_script(m_strike_script, "strike-script");
142 }
143}
144
145void
146Thunderstorm::flash()
147{
148 SoundManager::current()->play("sounds/lightning.wav");
149 flash_display_timer.start(FLASH_DISPLAY_TIME);
150}
151
152void
153Thunderstorm::electrify()
154{
155 auto changing_tiles = Electrifier::TileChangeMap({
156 {200, 1421}, {201, 1422},
157 {3419, 3523}, {3420, 3524},
158 {3421, 3525}, {3422, 3526},
159 {3423, 3527}, {3424, 3528},
160 {3425, 3529}, {3426, 3530},
161 {3427, 3523}, {3428, 3524},
162 {3429, 3525}, {3430, 3526},
163 {3431, 3527}, {3432, 3528},
164 {3433, 3529}, {3434, 3530}
165 });
166 Sector::get().add<Electrifier>(changing_tiles, ELECTRIFY_TIME);
167}
168
169/* EOF */
170