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