4f3fd9d145d6cb874b22d62cd4dafeaaaf3f550f
[supertux.git] / src / object / pulsing_light.cpp
1 //  $Id$
2 //
3 //  SuperTux - Pulsing Light
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 "pulsing_light.hpp"
23 #include "video/color.hpp"
24 #include <math.h>
25 #include "random_generator.hpp"
26
27 PulsingLight::PulsingLight(const Vector& center, float cycle_len, float min_alpha, float max_alpha, const Color& color)
28         : Light(center, color), min_alpha(min_alpha), max_alpha(max_alpha), cycle_len(cycle_len), t(0)
29 {
30   assert(cycle_len > 0);
31
32   // start with random phase offset
33   t = systemRandom.randf(0.0, cycle_len);
34 }
35
36 PulsingLight::~PulsingLight()
37 {
38 }
39
40 void
41 PulsingLight::update(float elapsed_time)
42 {
43   Light::update(elapsed_time);
44
45   t += elapsed_time;
46   if (t > cycle_len) t -= cycle_len;
47 }
48
49 void
50 PulsingLight::draw(DrawingContext& context)
51 {
52   Color old_color = color;
53
54   color.alpha *= min_alpha + ((max_alpha - min_alpha) * cos(2 * M_PI * t / cycle_len));
55   Light::draw(context);
56
57   color = old_color;
58 }
59