5fc4d2ad3c2315d483a3af8edda8e77e3386142f
[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   bbox.set_size(9, 1);
34   sprite = sprite_manager->create("images/creatures/dart/dart.sprite");
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   bbox.set_size(9, 1);
44   sprite = sprite_manager->create("images/creatures/dart/dart.sprite");
45   physic.enable_gravity(false);
46   countMe = false;
47 }
48
49 Dart::~Dart()
50 {
51   delete soundSource;
52 }
53
54 void
55 Dart::write(lisp::Writer& writer)
56 {
57   writer.start_list("dart");
58   writer.write_float("x", start_position.x);
59   writer.write_float("y", start_position.y);
60   writer.end_list("dart");
61 }
62
63 void
64 Dart::activate()
65 {  
66   if (set_direction) dir = initial_direction;
67   physic.set_velocity_x(dir == LEFT ? -::SPEED : ::SPEED);
68   sprite->set_action(dir == LEFT ? "flying-left" : "flying-right");
69
70   delete soundSource;
71   soundSource = sound_manager->create_sound_source("sounds/flame.wav");
72   if(soundSource) {
73     soundSource->set_position(get_pos());
74     soundSource->set_looping(true);
75     soundSource->set_gain(1.0);
76     soundSource->set_reference_distance(32);
77     soundSource->play();
78   } else {
79     log_warning << "Couldn't start Dart ambient sound" << std::endl;
80   }
81 }
82
83 void
84 Dart::deactivate()
85 {  
86   delete soundSource;
87   soundSource = 0;
88   remove_me();
89 }
90
91 void 
92 Dart::active_update(float elapsed_time)
93 {
94   BadGuy::active_update(elapsed_time);
95   if (soundSource) soundSource->set_position(get_pos());
96 }
97
98
99 HitResponse 
100 Dart::collision_solid(GameObject& , const CollisionHit& )
101 {
102   sound_manager->play("sounds/stomp.wav", get_pos());
103   remove_me();
104   return ABORT_MOVE;
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