1aec3625f4c83a3079d34f789f351d487dad7d90
[supertux.git] / src / object / thunderstorm.cpp
1 //  $Id$
2 //
3 //  SuperTux - Thunderstorm Game Object
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "thunderstorm.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "audio/sound_manager.hpp"
25 #include "video/drawing_context.hpp"
26 #include "object_factory.hpp"
27 #include "object/electrifier.hpp"
28
29 #include <stdexcept>
30 #include <iostream>
31 #include "main.hpp"
32 #include "resources.hpp"
33 #include "sector.hpp"
34 #include "gettext.hpp"
35 #include "object/player.hpp"
36 #include "lisp/list_iterator.hpp"
37 #include "log.hpp"
38
39 namespace {
40         const float LIGHTNING_DELAY = 2.0;
41         const float FLASH_DISPLAY_TIME = 0.1;
42 }
43
44 Thunderstorm::Thunderstorm(const lisp::Lisp& reader)
45         : name(""), running(true), interval(10.0f)
46 {
47   reader.get("name", name);
48   reader.get("running", running);
49   reader.get("interval", interval);
50   if(interval <= 0) {
51     log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
52   }
53
54   sound_manager->preload("sounds/explosion.wav");
55   sound_manager->preload("sounds/upgrade.wav");
56
57   if (running) {
58     running = false; // else start() is ignored
59     start();
60   }
61 }
62
63 void
64 Thunderstorm::update(float )
65 {
66   if (!running) return;
67   if (time_to_thunder.check()) {
68     thunder();
69     time_to_lightning.start(LIGHTNING_DELAY);
70   }
71   if (time_to_lightning.check()) {
72     lightning();
73     time_to_thunder.start(interval);
74   }
75 }
76
77 void 
78 Thunderstorm::draw(DrawingContext& context) 
79 {
80   if (!flash_display_timer.started()) return;
81
82   float alpha = 0.33f;
83   context.push_transform();
84   context.set_translation(Vector(0, 0));
85   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), LAYER_BACKGROUNDTILES-1);
86   context.pop_transform();
87
88 }
89
90 void
91 Thunderstorm::expose(HSQUIRRELVM vm, SQInteger table_idx)
92 {
93   if (name == "") return;
94   Scripting::Thunderstorm* interface = new Scripting::Thunderstorm(this);
95   expose_object(vm, table_idx, interface, name, true);
96 }
97
98 void
99 Thunderstorm::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
100 {
101   if (name == "") return;
102   Scripting::unexpose_object(vm, table_idx, name);
103 }
104
105 void
106 Thunderstorm::start()
107 {
108   if (running) return;
109   running = true;
110   time_to_thunder.start(interval);
111   time_to_lightning.stop();
112 }
113
114 void
115 Thunderstorm::stop()
116 {
117   if (!running) return;
118   running = false;
119   time_to_thunder.stop();
120   time_to_lightning.stop();
121 }
122
123 void
124 Thunderstorm::thunder()
125 {
126   sound_manager->play("sounds/explosion.wav");
127 }
128
129 void
130 Thunderstorm::lightning()
131 {
132   flash();
133   electrify();
134 }
135
136 void
137 Thunderstorm::flash()
138 {
139   sound_manager->play("sounds/upgrade.wav");
140   sound_manager->play("sounds/explosion.wav");
141   flash_display_timer.start(FLASH_DISPLAY_TIME);
142 }
143
144 void
145 Thunderstorm::electrify()
146 {
147   Sector::current()->add_object(new Electrifier(75, 1421, 0.5));
148   Sector::current()->add_object(new Electrifier(76, 1422, 0.5));
149 }
150
151 IMPLEMENT_FACTORY(Thunderstorm, "thunderstorm");
152