Fix for coverity #29360
[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 KAMIKAZE_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   SoundManager::current()->preload(SPLAT_SOUND);
36   set_action (dir == LEFT ? "left" : "right", /* loops = */ -1);
37 }
38
39 KamikazeSnowball::KamikazeSnowball(const Vector& pos, Direction d)
40   : BadGuy(pos, d, "images/creatures/snowball/kamikaze-snowball.sprite")
41 {
42   SoundManager::current()->preload(SPLAT_SOUND);
43   set_action (dir == LEFT ? "left" : "right", /* loops = */ -1);
44 }
45
46 void
47 KamikazeSnowball::initialize()
48 {
49   physic.set_velocity_x(dir == LEFT ? -KAMIKAZE_SPEED : KAMIKAZE_SPEED);
50   physic.enable_gravity(false);
51   sprite->set_action(dir == LEFT ? "left" : "right");
52 }
53
54 bool
55 KamikazeSnowball::collision_squished(GameObject& object)
56 {
57   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
58   kill_squished(object);
59   return true;
60 }
61
62 void
63 KamikazeSnowball::collision_solid(const CollisionHit& hit)
64 {
65   if(hit.top || hit.bottom) {
66     physic.set_velocity_y(0);
67   }
68   if(hit.left || hit.right) {
69     kill_collision();
70   }
71 }
72
73 void
74 KamikazeSnowball::kill_collision()
75 {
76   sprite->set_action(dir == LEFT ? "collision-left" : "collision-right");
77   SoundManager::current()->play(SPLAT_SOUND, get_pos());
78   physic.set_velocity_x(0);
79   physic.set_velocity_y(0);
80   physic.enable_gravity(true);
81   set_state(STATE_FALLING);
82
83   run_dead_script();
84 }
85
86 HitResponse
87 KamikazeSnowball::collision_player(Player& player, const CollisionHit& hit)
88 {
89   //Hack to tell if we should die
90   HitResponse response = BadGuy::collision_player(player, hit);
91   if(response == FORCE_MOVE) {
92     kill_collision();
93   }
94
95   return ABORT_MOVE;
96 }
97
98 /* EOF */