First attempts at making BadGuys cloneable
[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 #include "log.hpp"
24
25 Flame::Flame(const lisp::Lisp& reader)
26   : angle(0), radius(100), speed(2), source(0)
27 {
28   reader.get("x", start_position.x);
29   reader.get("y", start_position.y);
30   reader.get("radius", radius);
31   reader.get("speed", speed);
32   bbox.set_pos(Vector(start_position.x + cos(angle) * radius,
33                       start_position.y + sin(angle) * radius));
34   sprite = sprite_manager->create("images/creatures/flame/flame.sprite");
35   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
36   countMe = false;
37   layer = LAYER_FLOATINGOBJECTS;
38 }
39
40 Flame::Flame(const Flame& other)
41   : BadGuy(other), angle(other.angle), radius(other.radius), speed(other.speed)
42 {
43   if (sound_manager->is_sound_enabled()) {
44     source.reset(sound_manager->create_sound_source("sounds/flame.wav"));
45   }
46 }
47
48 void
49 Flame::write(lisp::Writer& writer)
50 {
51   writer.start_list("flame");
52
53   writer.write_float("x", start_position.x);
54   writer.write_float("y", start_position.y);
55   writer.write_float("radius", radius);
56   writer.write_float("speed", speed);
57
58   writer.end_list("flame");
59 }
60
61 void
62 Flame::active_update(float elapsed_time)
63 {
64   angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
65   Vector newpos(start_position.x + cos(angle) * radius,
66                 start_position.y + sin(angle) * radius);
67   movement = newpos - get_pos();
68
69   if (sound_manager->is_sound_enabled())
70     source->set_position(get_pos());
71 }
72
73 void
74 Flame::activate()
75 {
76   set_group(COLGROUP_TOUCHABLE);
77
78   if (!sound_manager->is_sound_enabled())
79     return;
80
81   source.reset(sound_manager->create_sound_source("sounds/flame.wav"));
82   if(source.get() == NULL) {
83     log_warning << "Couldn't start flame sound" << std::endl;
84     return;
85   }
86   source->set_position(get_pos());
87   source->set_looping(true);
88   source->set_gain(2.0);
89   source->set_reference_distance(32);
90   source->play();
91 }
92
93 void
94 Flame::deactivate()
95 {
96   source.release();
97 }
98
99 void
100 Flame::kill_fall()
101 {
102 }
103
104 IMPLEMENT_FACTORY(Flame, "flame")
105