Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / mrbomb.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 "audio/sound_manager.hpp"
18 #include "badguy/bomb.hpp"
19 #include "badguy/mrbomb.hpp"
20 #include "object/explosion.hpp"
21 #include "sprite/sprite.hpp"
22 #include "sprite/sprite_manager.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 MrBomb::MrBomb(const Reader& reader) :
27   WalkingBadguy(reader, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right"),
28   grabbed()
29 {
30   walk_speed = 80;
31   max_drop_height = 16;
32   grabbed = false;
33
34   //Prevent stutter when Tux jumps on Mr Bomb
35   sound_manager->preload("sounds/explosion.wav");
36
37   //Check if we need another sprite
38   if( !reader.get( "sprite", sprite_name ) ){
39     return;
40   }
41   if( sprite_name == "" ){
42     sprite_name = "images/creatures/mr_bomb/mr_bomb.sprite";
43     return;
44   }
45   //Replace sprite
46   sprite = sprite_manager->create( sprite_name );
47 }
48
49 /* MrBomb created by a dispenser always gets default sprite atm.*/
50 MrBomb::MrBomb(const Vector& pos, Direction d) :
51   WalkingBadguy(pos, d, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right"),
52   grabbed()
53 {
54   walk_speed = 80;
55   max_drop_height = 16;
56   grabbed = false;
57   sound_manager->preload("sounds/explosion.wav");
58 }
59
60 HitResponse
61 MrBomb::collision(GameObject& object, const CollisionHit& hit)
62 {
63   if(grabbed)
64     return FORCE_MOVE;
65   return WalkingBadguy::collision(object, hit);
66 }
67
68 HitResponse
69 MrBomb::collision_player(Player& player, const CollisionHit& hit)
70 {
71   if(grabbed)
72     return FORCE_MOVE;
73   return WalkingBadguy::collision_player(player, hit);
74 }
75
76 bool
77 MrBomb::collision_squished(GameObject& object)
78 {
79   if(is_valid()) {
80     remove_me();
81     Sector::current()->add_object(new Bomb(get_pos(), dir, sprite_name ));
82   }
83   kill_squished(object);
84   return true;
85 }
86
87 void
88 MrBomb::active_update(float elapsed_time)
89 {
90   if(grabbed)
91     return;
92   WalkingBadguy::active_update(elapsed_time);
93 }
94
95 void
96 MrBomb::kill_fall()
97 {
98   if(is_valid()) {
99     remove_me();
100     Explosion* explosion = new Explosion(get_bbox().get_middle());
101     Sector::current()->add_object(explosion);
102   }
103
104   run_dead_script();
105 }
106
107 void
108 MrBomb::grab(MovingObject&, const Vector& pos, Direction dir)
109 {
110   assert(frozen);
111   movement = pos - get_pos();
112   this->dir = dir;
113   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
114   set_colgroup_active(COLGROUP_DISABLED);
115   grabbed = true;
116 }
117
118 void
119 MrBomb::ungrab(MovingObject& , Direction dir)
120 {
121   this->dir = dir;
122   set_colgroup_active(COLGROUP_MOVING);
123   grabbed = false;
124 }
125
126 void
127 MrBomb::freeze()
128 {
129   WalkingBadguy::freeze();
130   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
131 }
132
133 bool
134 MrBomb::is_freezable() const
135 {
136   return true;
137 }
138
139 bool
140 MrBomb::is_portable() const
141 {
142   return frozen;
143 }
144
145 IMPLEMENT_FACTORY(MrBomb, "mrbomb");
146
147 /* EOF */