Add some more sound_manager->preloads into object constructors
[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   sound_manager->preload("sounds/darthit.wav");
37   sound_manager->preload("sounds/stomp.wav");
38 }
39
40 Dart::Dart(const Vector& pos, Direction d, const BadGuy* parent = 0)
41         : BadGuy(pos, "images/creatures/dart/dart.sprite"), set_direction(true), initial_direction(d), parent(parent)
42 {
43   physic.enable_gravity(false);
44   countMe = false;
45   sound_manager->preload("sounds/darthit.wav");
46   sound_manager->preload("sounds/stomp.wav");
47 }
48
49 Dart::Dart(const Dart& other)
50         : BadGuy(other), set_direction(other.set_direction), initial_direction(other.initial_direction), parent(other.parent)
51 {
52   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
53   sound_manager->preload("sounds/darthit.wav");
54   sound_manager->preload("sounds/stomp.wav");
55 }
56
57 Dart::~Dart()
58 {
59 }
60
61 bool 
62 Dart::updatePointers(const GameObject* from_object, GameObject* to_object)
63 {
64   if (from_object == parent) {
65     parent = dynamic_cast<Dart*>(to_object);
66     return true;
67   }
68   return false;
69 }
70
71 void
72 Dart::write(lisp::Writer& writer)
73 {
74   writer.start_list("dart");
75   writer.write_float("x", start_position.x);
76   writer.write_float("y", start_position.y);
77   writer.end_list("dart");
78 }
79
80 void
81 Dart::activate()
82 {  
83   if (set_direction) dir = initial_direction;
84   physic.set_velocity_x(dir == LEFT ? -::SPEED : ::SPEED);
85   sprite->set_action(dir == LEFT ? "flying-left" : "flying-right");
86
87   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
88   sound_source->set_position(get_pos());
89   sound_source->set_looping(true);
90   sound_source->set_gain(1.0);
91   sound_source->set_reference_distance(32);
92   sound_source->play();
93 }
94
95 void
96 Dart::deactivate()
97 {  
98   sound_source.reset();
99   remove_me();
100 }
101
102 void 
103 Dart::active_update(float elapsed_time)
104 {
105   BadGuy::active_update(elapsed_time);
106   sound_source->set_position(get_pos());
107 }
108
109
110 HitResponse 
111 Dart::collision_solid(GameObject& , const CollisionHit& )
112 {
113   sound_manager->play("sounds/darthit.wav", get_pos());
114   remove_me();
115   return ABORT_MOVE;
116 }
117
118 HitResponse 
119 Dart::collision_badguy(BadGuy& badguy, const CollisionHit& )
120 {
121   // ignore collisions with parent
122   if (&badguy == parent) {
123     return FORCE_MOVE;
124   }
125   sound_manager->play("sounds/stomp.wav", get_pos());
126   remove_me();
127   badguy.kill_fall();
128   return ABORT_MOVE;
129 }
130
131 HitResponse 
132 Dart::collision_player(Player& player, const CollisionHit& hit)
133 {
134   sound_manager->play("sounds/stomp.wav", get_pos());
135   remove_me();
136   return BadGuy::collision_player(player, hit);
137 }
138
139 IMPLEMENT_FACTORY(Dart, "dart")
140