Replaced Ref and RefCounter with std::shared_ptr<>
[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 "badguy/bomb.hpp"
19 #include "badguy/short_fuse.hpp"
20 #include "object/bullet.hpp"
21 #include "object/explosion.hpp"
22 #include "object/player.hpp"
23 #include "sprite/sprite.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
28 #include "util/log.hpp"
29
30 #define EXPLOSION_FORCE 1000.0f
31
32 ShortFuse::ShortFuse(const Reader& reader) :
33   WalkingBadguy(reader, "images/creatures/short_fuse/short_fuse.sprite", "left", "right")
34 {
35   walk_speed = 100;
36   max_drop_height = -1;
37
38   //Check if we need another sprite
39   if( !reader.get( "sprite", sprite_name ) ){
40     return;
41   }
42   if( sprite_name == "" ){
43     sprite_name = "images/creatures/short_fuse/short_fuse.sprite";
44     return;
45   }
46   //Replace sprite
47   sprite = SpriteManager::current()->create( sprite_name );
48 }
49
50 void
51 ShortFuse::explode()
52 {
53   if (!is_valid())
54     return;
55
56   auto explosion = std::make_shared<Explosion>(get_bbox ().get_middle());
57
58   explosion->hurts(false);
59   explosion->pushes(true);
60   Sector::current()->add_object(explosion);
61
62   run_dead_script();
63   remove_me();
64 }
65
66 bool
67 ShortFuse::collision_squished(GameObject& obj)
68 {
69   if (!is_valid ())
70     return true;
71
72   Player* player = dynamic_cast<Player*>(&obj);
73   if(player)
74     player->bounce(*this);
75
76   explode ();
77
78   return true;
79 }
80
81 HitResponse
82 ShortFuse::collision_player (Player& player, const CollisionHit&)
83 {
84   player.bounce (*this);
85   explode ();
86   return ABORT_MOVE;
87 }
88
89 HitResponse
90 ShortFuse::collision_bullet (Bullet& bullet, const CollisionHit& )
91 {
92   // All bullets cause the unstable short fuse to explode
93   bullet.remove_me();
94   explode();
95   return ABORT_MOVE;
96 }
97
98 void
99 ShortFuse::kill_fall (void)
100 {
101   explode ();
102 }
103
104 /* vim: set sw=2 sts=2 et : */
105 /* EOF */