src/object/bonus_block.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/bonus_block.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "badguy/badguy.hpp"
00021 #include "lisp/list_iterator.hpp"
00022 #include "object/broken_brick.hpp"
00023 #include "object/flower.hpp"
00024 #include "object/bouncy_coin.hpp"
00025 #include "object/growup.hpp"
00026 #include "object/oneup.hpp"
00027 #include "object/player.hpp"
00028 #include "object/portable.hpp"
00029 #include "object/specialriser.hpp"
00030 #include "object/star.hpp"
00031 #include "sprite/sprite_manager.hpp"
00032 #include "supertux/constants.hpp"
00033 #include "supertux/level.hpp"
00034 #include "supertux/object_factory.hpp"
00035 #include "supertux/sector.hpp"
00036 
00037 #include <stdexcept>
00038 
00039 BonusBlock::BonusBlock(const Vector& pos, int data) :
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 }
00058 
00059 BonusBlock::BonusBlock(const Reader& lisp) :
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 }
00110 
00111 BonusBlock::~BonusBlock()
00112 {
00113   delete object;
00114 }
00115 
00116 void
00117 BonusBlock::hit(Player & player)
00118 {
00119   try_open(&player);
00120 }
00121 
00122 HitResponse
00123 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
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 }
00149 
00150 void
00151 BonusBlock::try_open(Player *player)
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 }
00219 
00220 void
00221 Block::break_me()
00222 {
00223   Sector* sector = Sector::current();
00224   sector->add_object(
00225     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
00226   sector->add_object(
00227     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
00228                     Vector(-150, -300)));
00229   sector->add_object(
00230     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
00231                     Vector(100, -400)));
00232   sector->add_object(
00233     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
00234                     Vector(150, -300)));
00235   remove_me();
00236 }
00237 
00238 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1