BonusBlock Class Reference

#include <bonus_block.hpp>

Inherits Block.

List of all members.

Public Types

enum  Contents {
  CONTENT_COIN, CONTENT_FIREGROW, CONTENT_ICEGROW, CONTENT_STAR,
  CONTENT_1UP, CONTENT_CUSTOM
}

Public Member Functions

 BonusBlock (const Vector &pos, int data)
 BonusBlock (const Reader &lisp)
virtual ~BonusBlock ()
HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
void try_open (Player *player)

Public Attributes

Contents contents
MovingObjectobject

Protected Member Functions

virtual void hit (Player &player)

Private Member Functions

 BonusBlock (const BonusBlock &)
BonusBlockoperator= (const BonusBlock &)


Detailed Description

Definition at line 22 of file bonus_block.hpp.


Member Enumeration Documentation

enum BonusBlock::Contents

Enumerator:
CONTENT_COIN 
CONTENT_FIREGROW 
CONTENT_ICEGROW 
CONTENT_STAR 
CONTENT_1UP 
CONTENT_CUSTOM 

Definition at line 32 of file bonus_block.hpp.

00032                 {
00033     CONTENT_COIN,
00034     CONTENT_FIREGROW,
00035     CONTENT_ICEGROW,
00036     CONTENT_STAR,
00037     CONTENT_1UP,
00038     CONTENT_CUSTOM
00039   };


Constructor & Destructor Documentation

BonusBlock::BonusBlock ( const Vector pos,
int  data 
)

Definition at line 39 of file bonus_block.cpp.

References MovingObject::bbox, CONTENT_1UP, CONTENT_COIN, CONTENT_FIREGROW, CONTENT_ICEGROW, CONTENT_STAR, contents, log_warning, Rectf::set_pos(), and Block::sprite.

00039                                                   :
00040   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
00041   contents(),
00042   object(0)
00043 {
00044   bbox.set_pos(pos);
00045   sprite->set_action("normal");
00046   switch(data) {
00047     case 1: contents = CONTENT_COIN; break;
00048     case 2: contents = CONTENT_FIREGROW; break;
00049     case 3: contents = CONTENT_STAR; break;
00050     case 4: contents = CONTENT_1UP; break;
00051     case 5: contents = CONTENT_ICEGROW; break;
00052     default:
00053       log_warning << "Invalid box contents" << std::endl;
00054       contents = CONTENT_COIN;
00055       break;
00056   }
00057 }

BonusBlock::BonusBlock ( const Reader lisp  ) 

Definition at line 59 of file bonus_block.cpp.

References MovingObject::bbox, CONTENT_1UP, CONTENT_COIN, CONTENT_CUSTOM, CONTENT_FIREGROW, CONTENT_ICEGROW, CONTENT_STAR, contents, ObjectFactory::create(), lisp::Lisp::get(), ObjectFactory::instance(), lisp::ListIterator::item(), lisp::ListIterator::lisp(), log_warning, lisp::ListIterator::next(), object, Rectf::set_pos(), lisp::ListIterator::value(), Vector::x, and Vector::y.

00059                                          :
00060   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
00061   contents(),
00062   object(0)
00063 {
00064   Vector pos;
00065 
00066   contents = CONTENT_COIN;
00067   lisp::ListIterator iter(&lisp);
00068   while(iter.next()) {
00069     const std::string& token = iter.item();
00070     if(token == "x") {
00071       iter.value()->get(pos.x);
00072     } else if(token == "y") {
00073       iter.value()->get(pos.y);
00074     } else if(token == "contents") {
00075       std::string contentstring;
00076       iter.value()->get(contentstring);
00077       if(contentstring == "coin") {
00078         contents = CONTENT_COIN;
00079       } else if(contentstring == "firegrow") {
00080         contents = CONTENT_FIREGROW;
00081       } else if(contentstring == "icegrow") {
00082         contents = CONTENT_ICEGROW;
00083       } else if(contentstring == "star") {
00084         contents = CONTENT_STAR;
00085       } else if(contentstring == "1up") {
00086         contents = CONTENT_1UP;
00087       } else if(contentstring == "custom") {
00088         contents = CONTENT_CUSTOM;
00089       } else {
00090         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
00091       }
00092     } else {
00093       if(contents == CONTENT_CUSTOM) {
00094         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
00095         object = dynamic_cast<MovingObject*> (game_object);
00096         if(object == 0)
00097           throw std::runtime_error(
00098             "Only MovingObjects are allowed inside BonusBlocks");
00099       } else {
00100         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
00101       }
00102     }
00103   }
00104 
00105   if(contents == CONTENT_CUSTOM && object == 0)
00106     throw std::runtime_error("Need to specify content object for custom block");
00107 
00108   bbox.set_pos(pos);
00109 }

BonusBlock::~BonusBlock (  )  [virtual]

Definition at line 111 of file bonus_block.cpp.

References object.

00112 {
00113   delete object;
00114 }

BonusBlock::BonusBlock ( const BonusBlock  )  [private]


Member Function Documentation

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

this function is called when the object collided with any other object

Reimplemented from Block.

Definition at line 123 of file bonus_block.cpp.

References BadGuy::can_break(), Block::collision(), Player::does_buttjump, MovingObject::get_bbox(), Rectf::get_bottom(), Rectf::get_top(), hit(), SHIFT_DELTA, and try_open().

00123                                                                {
00124 
00125   Player* player = dynamic_cast<Player*> (&other);
00126   if (player) {
00127     if (player->does_buttjump)
00128       try_open(player);
00129   }
00130 
00131   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
00132   if(badguy) {
00133     // hit contains no information for collisions with blocks.
00134     // Badguy's bottom has to be below the top of the block
00135     // SHIFT_DELTA is required to slide over one tile gaps.
00136     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
00137       try_open(player);
00138     }
00139   }
00140   Portable* portable = dynamic_cast<Portable*> (&other);
00141   if(portable) {
00142     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
00143     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
00144       try_open(player);
00145     }
00146   }
00147   return Block::collision(other, hit);
00148 }

void BonusBlock::try_open ( Player player  ) 

Definition at line 151 of file bonus_block.cpp.

References PlayerStatus::add_coins(), Sector::add_object(), PlayerStatus::bonus, Statistics::coins, CONTENT_1UP, CONTENT_COIN, CONTENT_CUSTOM, CONTENT_FIREGROW, CONTENT_ICEGROW, CONTENT_STAR, contents, Sector::current(), FIRE_BONUS, MovingObject::get_bbox(), Sector::get_level(), Rectf::get_middle(), MovingObject::get_pos(), Player::get_status(), ICE_BONUS, LEFT, NO_BONUS, object, SoundManager::play(), Sector::player, RIGHT, sound_manager, Block::sprite, Block::start_bounce(), Level::stats, and Vector::x.

Referenced by collision(), and hit().

00152 {
00153   if(sprite->get_action() == "empty") {
00154     sound_manager->play("sounds/brick.wav");
00155     return;
00156   }
00157 
00158   Sector* sector = Sector::current();
00159   assert(sector);
00160 
00161   if (player == NULL)
00162     player = sector->player;
00163   
00164   if (player == NULL)
00165     return;
00166 
00167   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
00168 
00169   switch(contents) {
00170     case CONTENT_COIN:
00171       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
00172       player->get_status()->add_coins(1);
00173       Sector::current()->get_level()->stats.coins++;
00174       break;
00175 
00176     case CONTENT_FIREGROW:
00177       if(player->get_status()->bonus == NO_BONUS) {
00178         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
00179         sector->add_object(riser);
00180       } else {
00181         SpecialRiser* riser = new SpecialRiser(
00182           get_pos(), new Flower(FIRE_BONUS));
00183         sector->add_object(riser);
00184       }
00185       sound_manager->play("sounds/upgrade.wav");
00186       break;
00187 
00188     case CONTENT_ICEGROW:
00189       if(player->get_status()->bonus == NO_BONUS) {
00190         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
00191         sector->add_object(riser);
00192       } else {
00193         SpecialRiser* riser = new SpecialRiser(
00194           get_pos(), new Flower(ICE_BONUS));
00195         sector->add_object(riser);
00196       }
00197       sound_manager->play("sounds/upgrade.wav");
00198       break;
00199 
00200     case CONTENT_STAR:
00201       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
00202       break;
00203 
00204     case CONTENT_1UP:
00205       sector->add_object(new OneUp(get_pos(), direction));
00206       break;
00207 
00208     case CONTENT_CUSTOM:
00209       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
00210       object = 0;
00211       sector->add_object(riser);
00212       sound_manager->play("sounds/upgrade.wav");
00213       break;
00214   }
00215 
00216   start_bounce(player);
00217   sprite->set_action("empty");
00218 }

void BonusBlock::hit ( Player player  )  [protected, virtual]

Implements Block.

Definition at line 117 of file bonus_block.cpp.

References try_open().

Referenced by collision().

00118 {
00119   try_open(&player);
00120 }

BonusBlock& BonusBlock::operator= ( const BonusBlock  )  [private]


Member Data Documentation

Contents BonusBlock::contents

Definition at line 45 of file bonus_block.hpp.

Referenced by BonusBlock(), Level::get_total_coins(), and try_open().

MovingObject* BonusBlock::object

Definition at line 46 of file bonus_block.hpp.

Referenced by BonusBlock(), try_open(), and ~BonusBlock().


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