b9cf34ff2a43e3f71e081530149064604efef32b
[supertux.git] / src / badguy / kamikazesnowball.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2008 Wolfgang Becker <uafr@gmx.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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "kamikazesnowball.hpp"
23
24 #include "object_factory.hpp"
25 #include "audio/sound_manager.hpp"
26 #include "sprite/sprite.hpp"
27
28 /* 
29  * Kamikaze Snowball will fly in one direction until he hits something.
30  * On impact he is destroyed, trying to kill what he hit or hit him. 
31  */
32 namespace{
33   static const float SPEED = 200;
34   const std::string SPLAT_SOUND = "sounds/splat.wav";
35 }
36
37 KamikazeSnowball::KamikazeSnowball(const lisp::Lisp& reader)
38   : BadGuy(reader, "images/creatures/snowball/kamikaze-snowball.sprite")
39 {
40   sound_manager->preload(SPLAT_SOUND);
41 }
42
43 KamikazeSnowball::KamikazeSnowball(const Vector& pos, Direction d)
44   : BadGuy(pos, d, "images/creatures/snowball/kamikaze-snowball.sprite")
45 {
46   sound_manager->preload(SPLAT_SOUND);
47 }
48
49 void
50 KamikazeSnowball::initialize()
51 {
52   physic.set_velocity_x(dir == LEFT ? -SPEED : SPEED);
53   physic.enable_gravity(false);
54   sprite->set_action(dir == LEFT ? "left" : "right");
55 }
56
57 bool
58 KamikazeSnowball::collision_squished(GameObject& object)
59 {
60   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
61   kill_squished(object);
62   return true;
63 }
64
65 void
66 KamikazeSnowball::collision_solid(const CollisionHit& hit)
67 {
68   if(hit.top || hit.bottom) {
69     physic.set_velocity_y(0);
70   } else if(hit.left || hit.right) {
71     kill_collision();
72   }
73 }
74
75 void
76 KamikazeSnowball::kill_collision()
77 {
78     sprite->set_action(dir == LEFT ? "collision-left" : "collision-right");
79     sound_manager->play(SPLAT_SOUND, get_pos());
80     physic.set_velocity_x(0);
81     physic.set_velocity_y(0);
82     physic.enable_gravity(true);
83     set_state(STATE_FALLING);
84 }
85
86 HitResponse
87 KamikazeSnowball::collision_player(Player& player, const CollisionHit& hit)
88 {
89   HitResponse response = BadGuy::collision_player(player, hit);
90   if(response == FORCE_MOVE){
91     kill_collision();
92     response = ABORT_MOVE;
93   }
94   return response;
95 }
96
97
98 IMPLEMENT_FACTORY(KamikazeSnowball, "kamikazesnowball")