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