Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / kamikazesnowball.cpp
1 //  SuperTux
2 //  Copyright (C) 2008 Wolfgang Becker <uafr@gmx.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/kamikazesnowball.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
22
23 /* 
24  * Kamikaze Snowball will fly in one direction until he hits something.
25  * On impact he is destroyed, trying to kill what he hit or hit him. 
26  */
27 namespace{
28   static const float SPEED = 200;
29   const std::string SPLAT_SOUND = "sounds/splat.wav";
30 }
31
32 KamikazeSnowball::KamikazeSnowball(const Reader& reader) :
33   BadGuy(reader, "images/creatures/snowball/kamikaze-snowball.sprite")
34 {
35   sound_manager->preload(SPLAT_SOUND);
36 }
37
38 KamikazeSnowball::KamikazeSnowball(const Vector& pos, Direction d)
39   : BadGuy(pos, d, "images/creatures/snowball/kamikaze-snowball.sprite")
40 {
41   sound_manager->preload(SPLAT_SOUND);
42 }
43
44 void
45 KamikazeSnowball::initialize()
46 {
47   physic.set_velocity_x(dir == LEFT ? -SPEED : SPEED);
48   physic.enable_gravity(false);
49   sprite->set_action(dir == LEFT ? "left" : "right");
50 }
51
52 bool
53 KamikazeSnowball::collision_squished(GameObject& object)
54 {
55   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
56   kill_squished(object);
57   return true;
58 }
59
60 void
61 KamikazeSnowball::collision_solid(const CollisionHit& hit)
62 {
63   if(hit.top || hit.bottom) {
64     physic.set_velocity_y(0);
65   } else if(hit.left || hit.right) {
66     kill_collision();
67   }
68 }
69
70 void
71 KamikazeSnowball::kill_collision()
72 {
73   sprite->set_action(dir == LEFT ? "collision-left" : "collision-right");
74   sound_manager->play(SPLAT_SOUND, get_pos());
75   physic.set_velocity_x(0);
76   physic.set_velocity_y(0);
77   physic.enable_gravity(true);
78   set_state(STATE_FALLING);
79
80   run_dead_script();
81 }
82
83 HitResponse
84 KamikazeSnowball::collision_player(Player& player, const CollisionHit& hit)
85 {
86   HitResponse response = BadGuy::collision_player(player, hit);
87   if(response == FORCE_MOVE){
88     kill_collision();
89     response = ABORT_MOVE;
90   }
91   return response;
92 }
93
94 IMPLEMENT_FACTORY(KamikazeSnowball, "kamikazesnowball");
95
96 /* EOF */