-// $Id$
-//
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-#include <config.h>
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include "audio/sound_manager.hpp"
+#include "badguy/bomb.hpp"
+#include "badguy/mrbomb.hpp"
+#include "object/explosion.hpp"
+#include "sprite/sprite.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "supertux/object_factory.hpp"
+#include "supertux/sector.hpp"
+
+MrBomb::MrBomb(const Reader& reader) :
+ WalkingBadguy(reader, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right"),
+ grabbed()
+{
+ walk_speed = 80;
+ max_drop_height = 16;
+ grabbed = false;
-#include "mrbomb.hpp"
-#include "bomb.hpp"
+ //Prevent stutter when Tux jumps on Mr Bomb
+ sound_manager->preload("sounds/explosion.wav");
-static const float WALKSPEED = 80;
+ //Check if we need another sprite
+ if( !reader.get( "sprite", sprite_name ) ){
+ return;
+ }
+ if( sprite_name == "" ){
+ sprite_name = "images/creatures/mr_bomb/mr_bomb.sprite";
+ return;
+ }
+ //Replace sprite
+ sprite = sprite_manager->create( sprite_name );
+}
-MrBomb::MrBomb(const lisp::Lisp& reader)
+/* MrBomb created by a dispenser always gets default sprite atm.*/
+MrBomb::MrBomb(const Vector& pos, Direction d) :
+ WalkingBadguy(pos, d, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right"),
+ grabbed()
{
- reader.get("x", start_position.x);
- reader.get("y", start_position.y);
- sprite = sprite_manager->create("images/creatures/mr_bomb/mr_bomb.sprite");
- bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
- set_direction = false;
+ walk_speed = 80;
+ max_drop_height = 16;
+ grabbed = false;
+ sound_manager->preload("sounds/explosion.wav");
}
-MrBomb::MrBomb(float pos_x, float pos_y, Direction d)
+HitResponse
+MrBomb::collision(GameObject& object, const CollisionHit& hit)
{
- start_position.x = pos_x;
- start_position.y = pos_y;
- sprite = sprite_manager->create("images/creatures/mr_bomb/mr_bomb.sprite");
- bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
- set_direction = true;
- initial_direction = d;
+ if(grabbed)
+ return FORCE_MOVE;
+ return WalkingBadguy::collision(object, hit);
}
-void
-MrBomb::write(lisp::Writer& writer)
+HitResponse
+MrBomb::collision_player(Player& player, const CollisionHit& hit)
{
- writer.start_list("mrbomb");
-
- writer.write_float("x", start_position.x);
- writer.write_float("y", start_position.y);
+ if(grabbed)
+ return FORCE_MOVE;
+ return WalkingBadguy::collision_player(player, hit);
+}
- writer.end_list("mrbomb");
+bool
+MrBomb::collision_squished(GameObject& object)
+{
+ if(is_valid()) {
+ remove_me();
+ Sector::current()->add_object(new Bomb(get_pos(), dir, sprite_name ));
+ }
+ kill_squished(object);
+ return true;
}
void
-MrBomb::activate()
+MrBomb::active_update(float elapsed_time)
{
- if (set_direction) {dir = initial_direction;}
- physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
- sprite->set_action(dir == LEFT ? "left" : "right");
+ if(grabbed)
+ return;
+ WalkingBadguy::active_update(elapsed_time);
}
void
-MrBomb::active_update(float elapsed_time)
+MrBomb::kill_fall()
{
- if (might_fall())
- {
- dir = (dir == LEFT ? RIGHT : LEFT);
- sprite->set_action(dir == LEFT ? "left" : "right");
- physic.set_velocity_x(-physic.get_velocity_x());
+ if(is_valid()) {
+ remove_me();
+ Explosion* explosion = new Explosion(get_bbox().get_middle());
+ Sector::current()->add_object(explosion);
}
- BadGuy::active_update(elapsed_time);
+ run_dead_script();
}
-bool
-MrBomb::collision_squished(Player& player)
+void
+MrBomb::grab(MovingObject&, const Vector& pos, Direction dir)
{
- remove_me();
- Sector::current()->add_object(new Bomb(get_pos(), dir));
- kill_squished(player);
- return true;
+ assert(frozen);
+ movement = pos - get_pos();
+ this->dir = dir;
+ sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
+ set_colgroup_active(COLGROUP_DISABLED);
+ grabbed = true;
}
-HitResponse
-MrBomb::collision_solid(GameObject& , const CollisionHit& hit)
+void
+MrBomb::ungrab(MovingObject& , Direction dir)
{
- if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
- physic.set_velocity_y(0);
- } else { // hit right or left
- dir = dir == LEFT ? RIGHT : LEFT;
- sprite->set_action(dir == LEFT ? "left" : "right");
- physic.set_velocity_x(-physic.get_velocity_x());
- }
-
- return CONTINUE;
+ this->dir = dir;
+ set_colgroup_active(COLGROUP_MOVING);
+ grabbed = false;
}
-HitResponse
-MrBomb::collision_badguy(BadGuy& , const CollisionHit& hit)
+void
+MrBomb::freeze()
{
- if(fabsf(hit.normal.x) > .8) { // left or right
- dir = dir == LEFT ? RIGHT : LEFT;
- sprite->set_action(dir == LEFT ? "left" : "right");
- physic.set_velocity_x(-physic.get_velocity_x());
- }
+ WalkingBadguy::freeze();
+ sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
+}
- return CONTINUE;
+bool
+MrBomb::is_freezable() const
+{
+ return true;
}
-void
-MrBomb::kill_fall()
+bool
+MrBomb::is_portable() const
{
- remove_me();
- Bomb* bomb = new Bomb(get_pos(), dir);
- Sector::current()->add_object(bomb);
- bomb->explode();
+ return frozen;
}
-IMPLEMENT_FACTORY(MrBomb, "mrbomb")
+IMPLEMENT_FACTORY(MrBomb, "mrbomb");
+
+/* EOF */