Dispenser Class Reference

#include <dispenser.hpp>

Inherits BadGuy.

List of all members.

Public Member Functions

 Dispenser (const Reader &reader)
void activate ()
 called when the badguy has been activated.
void deactivate ()
 called when the badguy has been deactivated
void active_update (float elapsed_time)
 called each frame when the badguy is activated.
void freeze ()
 Called when hit by an ice bullet, and is_freezable() returns true.
void unfreeze ()
 Called to unfreeze the badguy.
bool is_freezable () const

Protected Member Functions

bool collision_squished (GameObject &object)
 Called when the player hit the badguy from above.
HitResponse collision (GameObject &other, const CollisionHit &hit)
 Called when a collision with another object occurred.
void launch_badguy ()

Private Attributes

float cycle
std::vector< std::string > badguys
unsigned int next_badguy
Timer dispense_timer
bool autotarget
bool swivel
bool broken
bool random
std::string type


Detailed Description

Definition at line 22 of file dispenser.hpp.


Constructor & Destructor Documentation

Dispenser::Dispenser ( const Reader reader  ) 

Definition at line 29 of file dispenser.cpp.

References AUTO, autotarget, badguys, MovingObject::bbox, broken, COLGROUP_MOVING, COLGROUP_MOVING_STATIC, BadGuy::countMe, cycle, BadGuy::dir, lisp::Lisp::get(), LEFT, next_badguy, SoundManager::preload(), random, BadGuy::set_colgroup_active(), Rectf::set_size(), sound_manager, MovingSprite::sprite, BadGuy::start_dir, swivel, and type.

00029                                          :
00030   BadGuy(reader, "images/creatures/dispenser/dispenser.sprite"),
00031   cycle(),
00032   badguys(),
00033   next_badguy(),
00034   dispense_timer(),
00035   autotarget(),
00036   swivel(),
00037   broken(),
00038   random(),
00039   type()
00040 {
00041   set_colgroup_active(COLGROUP_MOVING_STATIC);
00042   sound_manager->preload("sounds/squish.wav");
00043   reader.get("cycle", cycle);
00044   reader.get("badguy", badguys);
00045   random = false; // default
00046   reader.get("random", random);
00047   type = "dropper"; //default
00048   reader.get("type", type);
00049   next_badguy = 0;
00050   autotarget = false;
00051   swivel = false;
00052   broken = false;
00053 
00054   if (badguys.size() <= 0)
00055     throw std::runtime_error("No badguys in dispenser.");
00056 
00057   if (type == "rocketlauncher") {
00058     sprite->set_action(dir == LEFT ? "working-left" : "working-right");
00059     set_colgroup_active(COLGROUP_MOVING); //if this were COLGROUP_MOVING_STATIC MrRocket would explode on launch.
00060 
00061     if (start_dir == AUTO) {
00062       autotarget = true;
00063     }
00064   } else if (type == "cannon") {
00065     sprite->set_action("working");
00066   } else {
00067     sprite->set_action("dropper");
00068   }
00069 
00070   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00071   countMe = false;
00072 }


Member Function Documentation

void Dispenser::activate (  )  [virtual]

called when the badguy has been activated.

(As a side effect the dir variable might have been changed so that it faces towards the player.

Reimplemented from BadGuy.

Definition at line 75 of file dispenser.cpp.

References autotarget, broken, cycle, BadGuy::dir, dispense_timer, BadGuy::get_nearest_player(), MovingObject::get_pos(), launch_badguy(), LEFT, RIGHT, MovingSprite::sprite, Timer::start(), swivel, and Vector::x.

Referenced by unfreeze().

00076 {
00077   if( broken ){
00078     return;
00079   }
00080   if( autotarget && !swivel ){ // auto cannon sprite might be wrong
00081     Player* player = this->get_nearest_player();
00082     if( player ){
00083       dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
00084       sprite->set_action(dir == LEFT ? "working-left" : "working-right");
00085     }
00086   }
00087   dispense_timer.start(cycle, true);
00088   launch_badguy();
00089 }

void Dispenser::deactivate (  )  [virtual]

called when the badguy has been deactivated

Reimplemented from BadGuy.

Definition at line 92 of file dispenser.cpp.

References dispense_timer, and Timer::stop().

00093 {
00094   dispense_timer.stop();
00095 }

void Dispenser::active_update ( float  elapsed_time  )  [virtual]

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Definition at line 144 of file dispenser.cpp.

References autotarget, Timer::check(), BadGuy::dir, dispense_timer, BadGuy::get_nearest_player(), MovingObject::get_pos(), launch_badguy(), LEFT, RIGHT, MovingSprite::sprite, swivel, and Vector::x.

00145 {
00146   if (dispense_timer.check()) {
00147     // auto always shoots in Tux's direction
00148     if( autotarget ){ 
00149       if( sprite->animation_done()) {
00150         sprite->set_action(dir == LEFT ? "working-left" : "working-right");
00151         swivel = false;
00152       }
00153 
00154       Player* player = this->get_nearest_player();
00155       if( player && !swivel ){
00156         Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
00157         if( dir != targetdir ){ // no target: swivel cannon 
00158           swivel = true;
00159           dir = targetdir;
00160           sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
00161         } else { // tux in sight: shoot
00162           launch_badguy();
00163         }
00164       }
00165     } else {
00166       launch_badguy();
00167     }
00168   }
00169 }

void Dispenser::freeze (  )  [virtual]

Called when hit by an ice bullet, and is_freezable() returns true.

Reimplemented from BadGuy.

Definition at line 244 of file dispenser.cpp.

References dispense_timer, BadGuy::freeze(), and Timer::stop().

00245 {
00246   BadGuy::freeze();
00247   dispense_timer.stop();
00248 }

void Dispenser::unfreeze (  )  [virtual]

Called to unfreeze the badguy.

Reimplemented from BadGuy.

Definition at line 251 of file dispenser.cpp.

References activate(), and BadGuy::unfreeze().

Referenced by collision().

00252 {
00253   BadGuy::unfreeze();
00254   activate();
00255 }

bool Dispenser::is_freezable (  )  const [virtual]

Reimplemented from BadGuy.

Definition at line 258 of file dispenser.cpp.

00259 {
00260   return true;
00261 }

bool Dispenser::collision_squished ( GameObject object  )  [protected, virtual]

Called when the player hit the badguy from above.

You should return true if the badguy was squished, false if squishing wasn't possible

Reimplemented from BadGuy.

Definition at line 99 of file dispenser.cpp.

References Player::bounce(), broken, COLGROUP_MOVING_STATIC, BadGuy::dir, dispense_timer, MovingObject::get_pos(), LEFT, SoundManager::play(), BadGuy::set_colgroup_active(), sound_manager, MovingSprite::sprite, Timer::start(), and type.

Referenced by collision().

00100 {
00101   //Cannon launching MrRocket can be broken by jumping on it
00102   //other dispensers are not that fragile.
00103   if (broken || type != "rocketlauncher") {
00104     return false;
00105   }
00106 
00107   sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
00108   dispense_timer.start(0);
00109   set_colgroup_active(COLGROUP_MOVING_STATIC); // Tux can stand on broken cannon.
00110   Player* player = dynamic_cast<Player*>(&object);
00111   if (player){
00112     player->bounce(*this);
00113   }
00114   sound_manager->play("sounds/squish.wav", get_pos());
00115   broken = true;
00116   return true;
00117 }

HitResponse Dispenser::collision ( GameObject other,
const CollisionHit hit 
) [protected, virtual]

Called when a collision with another object occurred.

The default implementation calls collision_player, collision_solid, collision_badguy and collision_squished

Reimplemented from BadGuy.

Definition at line 120 of file dispenser.cpp.

References MovingObject::bbox, BadGuy::collision_bullet(), collision_squished(), FORCE_MOVE, BadGuy::frozen, MovingObject::get_bbox(), Rectf::p1, Rectf::p2, unfreeze(), and Vector::y.

00121 {
00122   Player* player = dynamic_cast<Player*> (&other);
00123   if(player) {
00124     // hit from above?
00125     if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
00126       collision_squished(*player);
00127       return FORCE_MOVE;
00128     }
00129     if(frozen){
00130       unfreeze();
00131     }
00132     return FORCE_MOVE;
00133   }
00134 
00135   Bullet* bullet = dynamic_cast<Bullet*> (&other);
00136   if(bullet){
00137     return collision_bullet(*bullet, hit);
00138   }
00139 
00140   return FORCE_MOVE;
00141 }

void Dispenser::launch_badguy (  )  [protected]

Definition at line 172 of file dispenser.cpp.

References Sector::add_object(), ANCHOR_BOTTOM, AUTO, autotarget, badguys, ObjectFactory::create(), Sector::current(), BadGuy::dir, gameRandom, get_anchor_pos(), MovingObject::get_bbox(), BadGuy::get_nearest_player(), MovingObject::get_pos(), Rectf::get_width(), ObjectFactory::instance(), BadGuy::is_offscreen(), LEFT, log_warning, next_badguy, RandomGenerator::rand(), random, RIGHT, MovingObject::set_pos(), BadGuy::start_dir, type, and Vector::x.

Referenced by activate(), and active_update().

00173 {
00174   //FIXME: Does is_offscreen() work right here?
00175   if (!is_offscreen()) {
00176     Direction launchdir = dir;
00177     if( !autotarget && start_dir == AUTO ){
00178       Player* player = this->get_nearest_player();
00179       if( player ){
00180         launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
00181       } 
00182     } 
00183 
00184     if (badguys.size() > 1) {
00185       if (random) {
00186         next_badguy = gameRandom.rand(badguys.size());
00187       }
00188       else {
00189         next_badguy++;
00190 
00191         if (next_badguy >= badguys.size())
00192           next_badguy = 0;
00193       }
00194     }
00195 
00196     std::string badguy = badguys[next_badguy];
00197 
00198     if(badguy == "random") {
00199       log_warning << "random is outdated; use a list of badguys to select from." << std::endl;
00200       return;
00201     }
00202 
00203     try {
00204       GameObject *game_object;
00205       MovingObject *moving_object;
00206       Vector spawnpoint;
00207       Rectf object_bbox;
00208 
00209       /* Need to allocate the badguy first to figure out its bounding box. */
00210       game_object = ObjectFactory::instance().create(badguy, get_pos(), launchdir);
00211       if (game_object == NULL)
00212         throw std::runtime_error("Creating " + badguy + " object failed.");
00213 
00214       moving_object = dynamic_cast<MovingObject *> (game_object);
00215       if (moving_object == NULL)
00216         throw std::runtime_error(badguy + " is not a moving object.");
00217 
00218       object_bbox = moving_object->get_bbox ();
00219 
00220       if (type == "dropper") {
00221         spawnpoint = get_anchor_pos (get_bbox (), ANCHOR_BOTTOM);
00222         spawnpoint.x -= 0.5 * object_bbox.get_width ();
00223       }
00224       else if ((type == "cannon") || (type == "rocketlauncher")) {
00225         spawnpoint = get_pos (); /* top-left corner of the cannon */
00226         if (launchdir == LEFT)
00227           spawnpoint.x -= object_bbox.get_width () + 1;
00228         else
00229           spawnpoint.x += get_bbox ().get_width () + 1;
00230       }
00231 
00232       /* Now we set the real spawn position */
00233       moving_object->set_pos (spawnpoint);
00234 
00235       Sector::current()->add_object(moving_object);
00236     } catch(std::exception& e) {
00237       log_warning << "Error dispensing badguy: " << e.what() << std::endl;
00238       return;
00239     }
00240   }
00241 }


Member Data Documentation

float Dispenser::cycle [private]

Definition at line 41 of file dispenser.hpp.

Referenced by activate(), and Dispenser().

std::vector<std::string> Dispenser::badguys [private]

Definition at line 42 of file dispenser.hpp.

Referenced by Dispenser(), and launch_badguy().

unsigned int Dispenser::next_badguy [private]

Definition at line 43 of file dispenser.hpp.

Referenced by Dispenser(), and launch_badguy().

Timer Dispenser::dispense_timer [private]

Definition at line 44 of file dispenser.hpp.

Referenced by activate(), active_update(), collision_squished(), deactivate(), and freeze().

bool Dispenser::autotarget [private]

Definition at line 45 of file dispenser.hpp.

Referenced by activate(), active_update(), Dispenser(), and launch_badguy().

bool Dispenser::swivel [private]

Definition at line 46 of file dispenser.hpp.

Referenced by activate(), active_update(), and Dispenser().

bool Dispenser::broken [private]

Definition at line 47 of file dispenser.hpp.

Referenced by activate(), collision_squished(), and Dispenser().

bool Dispenser::random [private]

Definition at line 48 of file dispenser.hpp.

Referenced by Dispenser(), and launch_badguy().

std::string Dispenser::type [private]

Definition at line 49 of file dispenser.hpp.

Referenced by collision_squished(), Dispenser(), and launch_badguy().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:30 2014 for SuperTux by  doxygen 1.5.1