Renamed namespaces to all lowercase
[supertux.git] / src / object / thunderstorm.cpp
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 "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"
26
27 namespace {
28 const float LIGHTNING_DELAY = 2.0f;
29 const float FLASH_DISPLAY_TIME = 0.1f;
30 }
31
32 Thunderstorm::Thunderstorm(const Reader& reader) :
33   running(true),
34   interval(10.0f), 
35   layer(LAYER_BACKGROUNDTILES-1),
36   time_to_thunder(),
37   time_to_lightning(),
38   flash_display_timer()
39 {
40   reader.get("name", name);
41   reader.get("running", running);
42   reader.get("interval", interval);
43   if(interval <= 0) {
44     log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
45   }
46   reader.get("layer", layer);
47
48   sound_manager->preload("sounds/explosion.wav");
49   sound_manager->preload("sounds/upgrade.wav");
50
51   if (running) {
52     running = false; // else start() is ignored
53     start();
54   }
55 }
56
57 void
58 Thunderstorm::update(float )
59 {
60   if (!running) return;
61   if (time_to_thunder.check()) {
62     thunder();
63     time_to_lightning.start(LIGHTNING_DELAY);
64   }
65   if (time_to_lightning.check()) {
66     lightning();
67     time_to_thunder.start(interval);
68   }
69 }
70
71 void
72 Thunderstorm::draw(DrawingContext& context)
73 {
74   if (!flash_display_timer.started()) return;
75
76   float alpha = 0.33f;
77   context.push_transform();
78   context.set_translation(Vector(0, 0));
79   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), layer);
80   context.pop_transform();
81
82 }
83
84 void
85 Thunderstorm::expose(HSQUIRRELVM vm, SQInteger table_idx)
86 {
87   if (name == "") return;
88   scripting::Thunderstorm* interface = new scripting::Thunderstorm(this);
89   expose_object(vm, table_idx, interface, name, true);
90 }
91
92 void
93 Thunderstorm::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
94 {
95   if (name == "") return;
96   scripting::unexpose_object(vm, table_idx, name);
97 }
98
99 void
100 Thunderstorm::start()
101 {
102   if (running) return;
103   running = true;
104   time_to_thunder.start(interval);
105   time_to_lightning.stop();
106 }
107
108 void
109 Thunderstorm::stop()
110 {
111   if (!running) return;
112   running = false;
113   time_to_thunder.stop();
114   time_to_lightning.stop();
115 }
116
117 void
118 Thunderstorm::thunder()
119 {
120   sound_manager->play("sounds/explosion.wav");
121 }
122
123 void
124 Thunderstorm::lightning()
125 {
126   flash();
127   electrify();
128 }
129
130 void
131 Thunderstorm::flash()
132 {
133   sound_manager->play("sounds/upgrade.wav");
134   sound_manager->play("sounds/explosion.wav");
135   flash_display_timer.start(FLASH_DISPLAY_TIME);
136 }
137
138 void
139 Thunderstorm::electrify()
140 {
141   Sector::current()->add_object(new Electrifier(75, 1421, 0.5));
142   Sector::current()->add_object(new Electrifier(76, 1422, 0.5));
143 }
144
145 IMPLEMENT_FACTORY(Thunderstorm, "thunderstorm");
146
147 /* EOF */