d9b0759f95ba6169be447642d0ac42a9d2a483d7
[supertux.git] / src / badguy / flame.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "flame.hpp"
23
24 Flame::Flame(const lisp::Lisp& reader)
25   : angle(0), radius(100), speed(2), source(0)
26 {
27   reader.get("x", start_position.x);
28   reader.get("y", start_position.y);
29   reader.get("radius", radius);
30   reader.get("speed", speed);
31   bbox.set_pos(Vector(start_position.x + cos(angle) * radius,
32                       start_position.y + sin(angle) * radius));
33   bbox.set_size(32, 32);  
34   sprite = sprite_manager->create("images/creatures/flame/flame.sprite");
35   countMe = false;
36 }
37
38 Flame::~Flame()
39 {
40   delete source;
41 }
42
43 void
44 Flame::write(lisp::Writer& writer)
45 {
46   writer.start_list("flame");
47
48   writer.write_float("x", start_position.x);
49   writer.write_float("y", start_position.y);
50   writer.write_float("radius", radius);
51   writer.write_float("speed", speed);
52
53   writer.end_list("flame");
54 }
55
56 void
57 Flame::active_update(float elapsed_time)
58 {
59   angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
60   Vector newpos(start_position.x + cos(angle) * radius,
61                 start_position.y + sin(angle) * radius);
62   movement = newpos - get_pos();
63
64   if (sound_manager->is_sound_enabled())
65     source->set_position(get_pos());
66 }
67
68 void
69 Flame::activate()
70 {
71   if (!sound_manager->is_sound_enabled())
72     return;
73
74   delete source;
75   source = sound_manager->create_sound_source("sounds/flame.wav");
76   if(!source) {
77     std::cerr << "Couldn't start flame sound.\n";
78     return;
79   }
80   source->set_position(get_pos());
81   source->set_looping(true);
82   source->set_gain(2.0);
83   source->set_reference_distance(32);
84   source->play();
85 }
86
87 void
88 Flame::deactivate()
89 {
90   delete source;
91   source = 0;
92 }
93
94 void
95 Flame::kill_fall()
96 {
97 }
98
99 IMPLEMENT_FACTORY(Flame, "flame")
100