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