From: florianf Date: Thu, 4 Mar 2010 13:13:53 +0000 (+0000) Subject: Cannon: Use the cannon's and badguy's bounding boxes to calculate the starting position. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=04c7508e56ecfc6d934f82192d020fc8b93e512c;p=supertux.git Cannon: Use the cannon's and badguy's bounding boxes to calculate the starting position. The previous code assumed width of no more than 32 pixels to work correctly. mathnerd314's change in revision 6530 widened that a bit, but could only be considered a dirty hack. This change should work for arbitrary badguy widths, moving narrow badguys (Short Fuse, for example) closer to the Cannon when shot to the left. git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6542 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- diff --git a/src/badguy/dispenser.cpp b/src/badguy/dispenser.cpp index 19ac634e2..a3db52a59 100644 --- a/src/badguy/dispenser.cpp +++ b/src/badguy/dispenser.cpp @@ -200,22 +200,42 @@ Dispenser::launch_badguy() return; } - GameObject* badguy_object = NULL; - try { + GameObject *game_object; + MovingObject *moving_object; Vector spawnpoint; + Rectf object_bbox; + + /* Need to allocate the badguy first to figure out its bounding box. */ + game_object = ObjectFactory::instance().create(badguy, get_pos(), launchdir); + if (game_object == NULL) + throw std::runtime_error("Creating " + badguy + " object failed."); + + moving_object = dynamic_cast (game_object); + if (moving_object == NULL) + throw std::runtime_error(badguy + " is not a moving object."); - if (type == "dropper") - spawnpoint = Vector(get_pos().x, get_pos().y+get_bbox().get_height()+8); - else if (type == "cannon") - spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : get_bbox().get_width()+3), get_pos().y); - else if (type == "rocketlauncher") - spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : get_bbox().get_width()+3), get_pos().y); + object_bbox = moving_object->get_bbox (); + + if (type == "dropper") { + spawnpoint = get_anchor_pos (get_bbox (), ANCHOR_BOTTOM); + spawnpoint.x -= 0.5 * object_bbox.get_width (); + } + else if ((type == "cannon") || (type == "rocketlauncher")) { + spawnpoint = get_pos (); /* top-left corner of the cannon */ + if (launchdir == LEFT) + spawnpoint.x -= object_bbox.get_width () + 1; + else + spawnpoint.x += get_bbox ().get_width () + 1; + } - badguy_object = ObjectFactory::instance().create(badguy, spawnpoint, launchdir); + /* Now we set the real spawn position */ + log_debug << "Cannong bbox: " << get_bbox () << std::endl; + log_debug << "Badguy width: " << object_bbox.get_width () << std::endl; + log_debug << "New badguy's spawnpoint: " << spawnpoint << std::endl; + moving_object->set_pos (spawnpoint); - if (badguy_object) - Sector::current()->add_object(badguy_object); + Sector::current()->add_object(moving_object); } catch(std::exception& e) { log_warning << "Error dispensing badguy: " << e.what() << std::endl; return;