Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / dart.cpp
1 //  Dart - Your average poison dart
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/dart.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "audio/sound_source.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
25 const float SPEED = 200;
26 }
27
28 static const std::string SOUNDFILE = "sounds/flame.wav";
29
30 Dart::Dart(const Reader& reader) :
31   BadGuy(reader, "images/creatures/dart/dart.sprite"), 
32   parent(0),
33   sound_source()
34 {
35   physic.enable_gravity(false);
36   countMe = false;
37   sound_manager->preload(SOUNDFILE);
38   sound_manager->preload("sounds/darthit.wav");
39   sound_manager->preload("sounds/stomp.wav");
40 }
41
42 Dart::Dart(const Vector& pos, Direction d, const BadGuy* parent = 0) :
43   BadGuy(pos, d, "images/creatures/dart/dart.sprite"), 
44   parent(parent),
45   sound_source()
46 {
47   physic.enable_gravity(false);
48   countMe = false;
49   sound_manager->preload(SOUNDFILE);
50   sound_manager->preload("sounds/darthit.wav");
51   sound_manager->preload("sounds/stomp.wav");
52 }
53
54 Dart::~Dart()
55 {
56 }
57
58 bool
59 Dart::updatePointers(const GameObject* from_object, GameObject* to_object)
60 {
61   if (from_object == parent) {
62     parent = dynamic_cast<Dart*>(to_object);
63     return true;
64   }
65   return false;
66 }
67
68 void
69 Dart::initialize()
70 {
71   physic.set_velocity_x(dir == LEFT ? -::SPEED : ::SPEED);
72   sprite->set_action(dir == LEFT ? "flying-left" : "flying-right");
73 }
74
75 void
76 Dart::activate()
77 {
78   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
79   sound_source->set_position(get_pos());
80   sound_source->set_looping(true);
81   sound_source->set_gain(1.0);
82   sound_source->set_reference_distance(32);
83   sound_source->play();
84 }
85
86 void
87 Dart::deactivate()
88 {
89   sound_source.reset();
90   remove_me();
91 }
92
93 void
94 Dart::active_update(float elapsed_time)
95 {
96   BadGuy::active_update(elapsed_time);
97   sound_source->set_position(get_pos());
98 }
99
100 void
101 Dart::collision_solid(const CollisionHit& )
102 {
103   sound_manager->play("sounds/darthit.wav", get_pos());
104   remove_me();
105 }
106
107 HitResponse
108 Dart::collision_badguy(BadGuy& badguy, const CollisionHit& )
109 {
110   // ignore collisions with parent
111   if (&badguy == parent) {
112     return FORCE_MOVE;
113   }
114   sound_manager->play("sounds/stomp.wav", get_pos());
115   remove_me();
116   badguy.kill_fall();
117   return ABORT_MOVE;
118 }
119
120 HitResponse
121 Dart::collision_player(Player& player, const CollisionHit& hit)
122 {
123   sound_manager->play("sounds/stomp.wav", get_pos());
124   remove_me();
125   return BadGuy::collision_player(player, hit);
126 }
127
128 IMPLEMENT_FACTORY(Dart, "dart");
129
130 /* EOF */