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