4e761a11ccc4280aa9b8276f2541b2658a3d9626
[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.0f;
41   const float FLASH_DISPLAY_TIME = 0.1f;
42 }
43
44 Thunderstorm::Thunderstorm(const lisp::Lisp& reader)
45   : running(true), interval(10.0f), layer(LAYER_BACKGROUNDTILES-1)
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   reader.get("layer", layer);
54
55   sound_manager->preload("sounds/explosion.wav");
56   sound_manager->preload("sounds/upgrade.wav");
57
58   if (running) {
59     running = false; // else start() is ignored
60     start();
61   }
62 }
63
64 void
65 Thunderstorm::update(float )
66 {
67   if (!running) return;
68   if (time_to_thunder.check()) {
69     thunder();
70     time_to_lightning.start(LIGHTNING_DELAY);
71   }
72   if (time_to_lightning.check()) {
73     lightning();
74     time_to_thunder.start(interval);
75   }
76 }
77
78 void
79 Thunderstorm::draw(DrawingContext& context)
80 {
81   if (!flash_display_timer.started()) return;
82
83   float alpha = 0.33f;
84   context.push_transform();
85   context.set_translation(Vector(0, 0));
86   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), layer);
87   context.pop_transform();
88
89 }
90
91 void
92 Thunderstorm::expose(HSQUIRRELVM vm, SQInteger table_idx)
93 {
94   if (name == "") return;
95   Scripting::Thunderstorm* interface = new Scripting::Thunderstorm(this);
96   expose_object(vm, table_idx, interface, name, true);
97 }
98
99 void
100 Thunderstorm::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
101 {
102   if (name == "") return;
103   Scripting::unexpose_object(vm, table_idx, name);
104 }
105
106 void
107 Thunderstorm::start()
108 {
109   if (running) return;
110   running = true;
111   time_to_thunder.start(interval);
112   time_to_lightning.stop();
113 }
114
115 void
116 Thunderstorm::stop()
117 {
118   if (!running) return;
119   running = false;
120   time_to_thunder.stop();
121   time_to_lightning.stop();
122 }
123
124 void
125 Thunderstorm::thunder()
126 {
127   sound_manager->play("sounds/explosion.wav");
128 }
129
130 void
131 Thunderstorm::lightning()
132 {
133   flash();
134   electrify();
135 }
136
137 void
138 Thunderstorm::flash()
139 {
140   sound_manager->play("sounds/upgrade.wav");
141   sound_manager->play("sounds/explosion.wav");
142   flash_display_timer.start(FLASH_DISPLAY_TIME);
143 }
144
145 void
146 Thunderstorm::electrify()
147 {
148   Sector::current()->add_object(new Electrifier(75, 1421, 0.5));
149   Sector::current()->add_object(new Electrifier(76, 1422, 0.5));
150 }
151
152 IMPLEMENT_FACTORY(Thunderstorm, "thunderstorm");