X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbadguy%2Fdispenser.cpp;h=1f48673bdb89c57866f7b9f31ce61243935721c8;hb=5f860f41a3f362de0a5dda951ff2ba4ebff95681;hp=d19815f2613a65d1c47fef779ceb050bd182f0f6;hpb=729bc6717bf68314dc9fad25db3a9f728062263e;p=supertux.git diff --git a/src/badguy/dispenser.cpp b/src/badguy/dispenser.cpp index d19815f26..1f48673bd 100644 --- a/src/badguy/dispenser.cpp +++ b/src/badguy/dispenser.cpp @@ -24,6 +24,8 @@ #include "supertux/sector.hpp" #include "util/reader.hpp" +#include + Dispenser::Dispenser(const Reader& reader) : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite"), cycle(), @@ -37,7 +39,7 @@ Dispenser::Dispenser(const Reader& reader) : type() { set_colgroup_active(COLGROUP_MOVING_STATIC); - sound_manager->preload("sounds/squish.wav"); + SoundManager::current()->preload("sounds/squish.wav"); reader.get("cycle", cycle); reader.get("badguy", badguys); random = false; // default @@ -109,7 +111,7 @@ Dispenser::collision_squished(GameObject& object) if (player){ player->bounce(*this); } - sound_manager->play("sounds/squish.wav", get_pos()); + SoundManager::current()->play("sounds/squish.wav", get_pos()); broken = true; return true; } @@ -143,7 +145,7 @@ Dispenser::active_update(float ) { if (dispense_timer.check()) { // auto always shoots in Tux's direction - if( autotarget ){ + if( autotarget ){ if( sprite->animation_done()) { sprite->set_action(dir == LEFT ? "working-left" : "working-right"); swivel = false; @@ -152,7 +154,7 @@ Dispenser::active_update(float ) Player* player = this->get_nearest_player(); if( player && !swivel ){ Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT; - if( dir != targetdir ){ // no target: swivel cannon + if( dir != targetdir ){ // no target: swivel cannon swivel = true; dir = targetdir; sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1); @@ -176,12 +178,12 @@ Dispenser::launch_badguy() 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 = systemRandom.rand(badguys.size()); + next_badguy = gameRandom.rand(badguys.size()); } else { next_badguy++; @@ -197,24 +199,48 @@ Dispenser::launch_badguy() log_warning << "random is outdated; use a list of badguys to select from." << std::endl; return; } - - GameObject* badguy_object = NULL; + 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(*game_object); + + object_bbox = bad_guy.get_bbox(); if (type == "dropper") - spawnpoint = Vector(get_pos().x, get_pos().y+32); - else if (type == "cannon") - spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y); - else if (type == "rocketlauncher") - spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y); + { + 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); - badguy_object = ObjectFactory::instance().create(badguy, spawnpoint, launchdir); + /* We don't want to count dispensed badguys in level stats */ + if(bad_guy.countMe) + bad_guy.countMe = false; - if (badguy_object) - Sector::current()->add_object(badguy_object); - } catch(std::exception& e) { + Sector::current()->add_object(game_object); + } catch(const std::exception& e) { log_warning << "Error dispensing badguy: " << e.what() << std::endl; return; }