ae6b7900c1638f7abac3cb13bed3883a8b2a9090
[supertux.git] / src / badguy / flame.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 "flame.hpp"
23
24 #include "log.hpp"
25 #include "lisp/writer.hpp"
26 #include "lisp/lisp.hpp"
27 #include "object_factory.hpp"
28 #include "audio/sound_manager.hpp"
29
30 #include <math.h>
31
32 static const std::string SOUNDFILE = "sounds/flame.wav";
33
34 Flame::Flame(const lisp::Lisp& reader)
35   : BadGuy(reader, "images/creatures/flame/flame.sprite", LAYER_FLOATINGOBJECTS), angle(0), radius(100), speed(2)
36 {
37   reader.get("radius", radius);
38   reader.get("speed", speed);
39   bbox.set_pos(Vector(start_position.x + cos(angle) * radius,
40                       start_position.y + sin(angle) * radius));
41   countMe = false;
42   sound_manager->preload(SOUNDFILE);
43
44   set_colgroup_active(COLGROUP_TOUCHABLE);
45 }
46
47 void
48 Flame::write(lisp::Writer& writer)
49 {
50   writer.start_list("flame");
51
52   writer.write("x", start_position.x);
53   writer.write("y", start_position.y);
54   writer.write("radius", radius);
55   writer.write("speed", speed);
56
57   writer.end_list("flame");
58 }
59
60 void
61 Flame::active_update(float elapsed_time)
62 {
63   angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
64   Vector newpos(start_position.x + cos(angle) * radius,
65                 start_position.y + sin(angle) * radius);
66   movement = newpos - get_pos();
67
68   sound_source->set_position(get_pos());
69 }
70
71 void
72 Flame::activate()
73 {
74   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
75   sound_source->set_position(get_pos());
76   sound_source->set_looping(true);
77   sound_source->set_gain(2.0);
78   sound_source->set_reference_distance(32);
79   sound_source->play();
80 }
81
82 void
83 Flame::deactivate()
84 {
85   sound_source.reset();
86 }
87
88 void
89 Flame::kill_fall()
90 {
91 }
92
93 IMPLEMENT_FACTORY(Flame, "flame")