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