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