Mr. Bomb now supports custom sprites.
[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   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
42 }
43
44 /* MrBomb created by a despencer always gets default sprite atm.*/
45 MrBomb::MrBomb(const Vector& pos, Direction d)
46         : BadGuy(pos, d, "images/creatures/mr_cherry/mr_cherry.sprite")
47 {
48 }
49
50 void
51 MrBomb::write(lisp::Writer& writer)
52 {
53   writer.start_list("mrbomb");
54
55   writer.write_float("x", start_position.x);
56   writer.write_float("y", start_position.y);
57
58   writer.end_list("mrbomb");
59 }
60
61 void
62 MrBomb::activate()
63 {
64   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
65   sprite->set_action(dir == LEFT ? "left" : "right");
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 HitResponse
91 MrBomb::collision_solid(GameObject& , const CollisionHit& hit)
92 {
93   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
94     physic.set_velocity_y(0);
95   } else { // hit right or left
96     dir = dir == LEFT ? RIGHT : LEFT;
97     sprite->set_action(dir == LEFT ? "left" : "right");
98     physic.set_velocity_x(-physic.get_velocity_x());
99   }
100
101   return CONTINUE;
102 }
103
104 HitResponse
105 MrBomb::collision_badguy(BadGuy& , const CollisionHit& hit)
106 {
107   if(fabsf(hit.normal.x) > .8) { // left or right
108     dir = dir == LEFT ? RIGHT : LEFT;
109     sprite->set_action(dir == LEFT ? "left" : "right");    
110     physic.set_velocity_x(-physic.get_velocity_x());
111   }
112
113   return CONTINUE;
114 }
115
116 void
117 MrBomb::kill_fall()
118 {
119   remove_me();
120   Bomb* bomb = new Bomb(get_pos(), dir, sprite_name );
121   Sector::current()->add_object(bomb);
122   bomb->explode();
123 }
124
125 IMPLEMENT_FACTORY(MrBomb, "mrbomb")