- Remove ManagedSoundSource
[supertux.git] / src / badguy / dart.cpp
1 //  $Id$
2 //
3 //  Dart - Your average poison dart
4 //  Copyright (C) 2006 Christoph Sommer <supertux@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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "dart.hpp"
24
25 namespace {
26   const float SPEED = 200;
27 }
28
29 static const std::string SOUNDFILE = "sounds/flame.wav";
30
31 Dart::Dart(const lisp::Lisp& reader) 
32         : BadGuy(reader, "images/creatures/dart/dart.sprite"), set_direction(false), parent(0)
33 {
34   physic.enable_gravity(false);
35   countMe = false;
36 }
37
38 Dart::Dart(const Vector& pos, Direction d, const BadGuy* parent = 0)
39         : BadGuy(pos, "images/creatures/dart/dart.sprite"), set_direction(true), initial_direction(d), parent(parent)
40 {
41   physic.enable_gravity(false);
42   countMe = false;
43 }
44
45 Dart::Dart(const Dart& other)
46         : BadGuy(other), set_direction(other.set_direction), initial_direction(other.initial_direction), parent(other.parent)
47 {
48   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
49 }
50
51 Dart::~Dart()
52 {
53 }
54
55 bool 
56 Dart::updatePointers(const GameObject* from_object, GameObject* to_object)
57 {
58   if (from_object == parent) {
59     parent = dynamic_cast<Dart*>(to_object);
60     return true;
61   }
62   return false;
63 }
64
65 void
66 Dart::write(lisp::Writer& writer)
67 {
68   writer.start_list("dart");
69   writer.write_float("x", start_position.x);
70   writer.write_float("y", start_position.y);
71   writer.end_list("dart");
72 }
73
74 void
75 Dart::activate()
76 {  
77   if (set_direction) dir = initial_direction;
78   physic.set_velocity_x(dir == LEFT ? -::SPEED : ::SPEED);
79   sprite->set_action(dir == LEFT ? "flying-left" : "flying-right");
80
81   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
82   sound_source->set_position(get_pos());
83   sound_source->set_looping(true);
84   sound_source->set_gain(1.0);
85   sound_source->set_reference_distance(32);
86   sound_source->play();
87 }
88
89 void
90 Dart::deactivate()
91 {  
92   sound_source.release();
93   remove_me();
94 }
95
96 void 
97 Dart::active_update(float elapsed_time)
98 {
99   BadGuy::active_update(elapsed_time);
100   sound_source->set_position(get_pos());
101 }
102
103
104 HitResponse 
105 Dart::collision_solid(GameObject& , const CollisionHit& )
106 {
107   sound_manager->play("sounds/stomp.wav", get_pos());
108   remove_me();
109   return ABORT_MOVE;
110 }
111
112 HitResponse 
113 Dart::collision_badguy(BadGuy& badguy, const CollisionHit& )
114 {
115   // ignore collisions with parent
116   if (&badguy == parent) {
117     return FORCE_MOVE;
118   }
119   sound_manager->play("sounds/stomp.wav", get_pos());
120   remove_me();
121   badguy.kill_fall();
122   return ABORT_MOVE;
123 }
124
125 HitResponse 
126 Dart::collision_player(Player& player, const CollisionHit& hit)
127 {
128   sound_manager->play("sounds/stomp.wav", get_pos());
129   remove_me();
130   return BadGuy::collision_player(player, hit);
131 }
132
133 IMPLEMENT_FACTORY(Dart, "dart")
134