- if (badguy == "snowball")
- Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y, dir));
- else if (badguy == "bouncingsnowball")
- Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y, dir));
- else if (badguy == "mrbomb")
- Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y, dir));
- else if (badguy == "mriceblock")
- Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y, dir));
- else if (badguy == "random")
- {}
+ Direction launchdir = dir;
+ if( !autotarget && start_dir == AUTO ){
+ Player* player = this->get_nearest_player();
+ if( player ){
+ launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
+ }
+ }
+
+ if (badguys.size() > 1) {
+ if (random) {
+ next_badguy = gameRandom.rand(badguys.size());
+ }
+ else {
+ next_badguy++;
+
+ if (next_badguy >= badguys.size())
+ next_badguy = 0;
+ }
+ }
+
+ std::string badguy = badguys[next_badguy];
+
+ if(badguy == "random") {
+ log_warning << "random is outdated; use a list of badguys to select from." << std::endl;
+ return;
+ }
+ if(badguy == "goldbomb") {
+ log_warning << "goldbomb is not allowed to be dispensed" << std::endl;
+ return;
+ }
+
+ try {
+ GameObjectPtr game_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.");
+
+ BadGuy& bad_guy = dynamic_cast<BadGuy&>(*game_object);
+
+ object_bbox = bad_guy.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;
+ }
+
+ /* Now we set the real spawn position */
+ bad_guy.set_pos(spawnpoint);
+
+ /* We don't want to count dispensed badguys in level stats */
+ if(bad_guy.countMe)
+ bad_guy.countMe = false;
+
+ Sector::current()->add_object(game_object);
+ } catch(const std::exception& e) {
+ log_warning << "Error dispensing badguy: " << e.what() << std::endl;
+ return;
+ }