127691fe2472b4cd505484da52ef4982b7aef800
[supertux.git] / src / badguy / short_fuse.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "audio/sound_manager.hpp"
19 #include "badguy/bomb.hpp"
20 #include "badguy/short_fuse.hpp"
21 #include "object/bullet.hpp"
22 #include "object/explosion.hpp"
23 #include "object/player.hpp"
24 #include "sprite/sprite.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "supertux/object_factory.hpp"
27 #include "supertux/sector.hpp"
28 #include "util/reader.hpp"
29 #include "util/log.hpp"
30
31 #define EXPLOSION_FORCE 1000.0f
32
33 ShortFuse::ShortFuse(const Reader& reader) :
34   WalkingBadguy(reader, "images/creatures/short_fuse/short_fuse.sprite", "left", "right")
35 {
36   walk_speed = 100;
37   max_drop_height = -1;
38
39   //Prevent stutter when Tux jumps on Mr Bomb
40   sound_manager->preload("sounds/explosion.wav");
41
42   //Check if we need another sprite
43   if( !reader.get( "sprite", sprite_name ) ){
44     return;
45   }
46   if( sprite_name == "" ){
47     sprite_name = "images/creatures/short_fuse/short_fuse.sprite";
48     return;
49   }
50   //Replace sprite
51   sprite = sprite_manager->create( sprite_name );
52 }
53
54 void
55 ShortFuse::explode (void)
56 {
57   if (!is_valid ())
58     return;
59
60   Explosion *explosion = new Explosion (get_bbox ().get_middle ());
61
62   explosion->hurts (false);
63   explosion->pushes (true);
64   Sector::current()->add_object (explosion);
65
66   run_dead_script ();
67   remove_me ();
68 }
69
70 bool
71 ShortFuse::collision_squished(GameObject& obj)
72 {
73   if (!is_valid ())
74     return true;
75
76   Player* player = dynamic_cast<Player*>(&obj);
77   if(player)
78     player->bounce(*this);
79
80   explode ();
81
82   return true;
83 }
84
85 HitResponse
86 ShortFuse::collision_player (Player& player, const CollisionHit&)
87 {
88   player.bounce (*this);
89   explode ();
90   return ABORT_MOVE;
91 }
92
93 HitResponse
94 ShortFuse::collision_bullet (Bullet& bullet, const CollisionHit& )
95 {
96   // All bullets cause the unstable short fuse to explode
97   bullet.remove_me();
98   explode();
99   return ABORT_MOVE;
100 }
101
102 void
103 ShortFuse::kill_fall (void)
104 {
105   explode ();
106 }
107
108 /* vim: set sw=2 sts=2 et : */
109 /* EOF */