d59b9582a585af059c7e8a0a82b60818b74d22d6
[supertux.git] / src / badguy / mrbomb.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "mrbomb.hpp"
23 #include "bomb.hpp"
24 #include "sprite/sprite_manager.hpp"
25
26 static const float WALKSPEED = 80;
27
28 MrBomb::MrBomb(const lisp::Lisp& reader)
29         : BadGuy(reader, "images/creatures/mr_cherry/mr_cherry.sprite")
30 {
31   //Check if we need another sprite
32   if( !reader.get( "sprite", sprite_name ) ){
33     return;
34   }
35   if( sprite_name == "" ){
36     sprite_name = "images/creatures/mr_cherry/mr_cherry.sprite";
37     return;
38   }
39   //Replace sprite 
40   sprite = sprite_manager->create( sprite_name );
41 }
42
43 /* MrBomb created by a despencer always gets default sprite atm.*/
44 MrBomb::MrBomb(const Vector& pos, Direction d)
45         : BadGuy(pos, d, "images/creatures/mr_cherry/mr_cherry.sprite")
46 {
47 }
48
49 void
50 MrBomb::write(lisp::Writer& writer)
51 {
52   writer.start_list("mrbomb");
53
54   writer.write_float("x", start_position.x);
55   writer.write_float("y", start_position.y);
56
57   writer.end_list("mrbomb");
58 }
59
60 void
61 MrBomb::activate()
62 {
63   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
64   sprite->set_action(dir == LEFT ? "left" : "right");
65   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
66 }
67
68 void
69 MrBomb::active_update(float elapsed_time)
70 {
71   if (might_fall())
72   {
73     dir = (dir == LEFT ? RIGHT : LEFT);
74     sprite->set_action(dir == LEFT ? "left" : "right");
75     physic.set_velocity_x(-physic.get_velocity_x());
76   }
77
78   BadGuy::active_update(elapsed_time);
79 }
80
81 bool
82 MrBomb::collision_squished(Player& player)
83 {
84   remove_me();
85   Sector::current()->add_object(new Bomb(get_pos(), dir, sprite_name ));
86   kill_squished(player);
87   return true;
88 }
89
90 void
91 MrBomb::collision_solid(const CollisionHit& hit)
92 {
93   if(hit.bottom || hit.top) {
94     physic.set_velocity_y(0);
95   }
96   if(hit.left || hit.right) {
97     dir = dir == LEFT ? RIGHT : LEFT;
98     sprite->set_action(dir == LEFT ? "left" : "right");
99     physic.set_velocity_x(-physic.get_velocity_x());
100   }
101 }
102
103 HitResponse
104 MrBomb::collision_badguy(BadGuy&, const CollisionHit& hit )
105 {
106   if(hit.left || hit.right) {
107     dir = (dir == LEFT) ? RIGHT : LEFT;
108     sprite->set_action(dir == LEFT ? "left" : "right");
109     physic.set_velocity_x(-physic.get_velocity_x());               
110   }
111   return CONTINUE;
112 }
113
114 void
115 MrBomb::kill_fall()
116 {
117   remove_me();
118   Bomb* bomb = new Bomb(get_pos(), dir, sprite_name );
119   Sector::current()->add_object(bomb);
120   bomb->explode();
121 }
122
123 IMPLEMENT_FACTORY(MrBomb, "mrbomb")