2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/bonus_block.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "badguy/badguy.hpp"
21 #include "lisp/list_iterator.hpp"
22 #include "object/broken_brick.hpp"
23 #include "object/coin.hpp"
24 #include "object/flower.hpp"
25 #include "object/bouncy_coin.hpp"
26 #include "object/growup.hpp"
27 #include "object/oneup.hpp"
28 #include "object/player.hpp"
29 #include "object/portable.hpp"
30 #include "object/specialriser.hpp"
31 #include "object/star.hpp"
32 #include "sprite/sprite_manager.hpp"
33 #include "supertux/constants.hpp"
34 #include "supertux/level.hpp"
35 #include "supertux/object_factory.hpp"
36 #include "supertux/sector.hpp"
38 BonusBlock::BonusBlock(const Vector& pos, int data) :
39 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
44 sprite->set_action("normal");
46 case 1: contents = CONTENT_COIN; break;
47 case 2: contents = CONTENT_FIREGROW; break;
48 case 3: contents = CONTENT_STAR; break;
49 case 4: contents = CONTENT_1UP; break;
50 case 5: contents = CONTENT_ICEGROW; break;
52 log_warning << "Invalid box contents" << std::endl;
53 contents = CONTENT_COIN;
58 BonusBlock::BonusBlock(const Reader& lisp) :
59 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
65 contents = CONTENT_COIN;
66 lisp::ListIterator iter(&lisp);
68 const std::string& token = iter.item();
70 iter.value()->get(pos.x);
71 } else if(token == "y") {
72 iter.value()->get(pos.y);
73 } else if(token == "contents") {
74 std::string contentstring;
75 iter.value()->get(contentstring);
76 if(contentstring == "coin") {
77 contents = CONTENT_COIN;
78 } else if(contentstring == "firegrow") {
79 contents = CONTENT_FIREGROW;
80 } else if(contentstring == "icegrow") {
81 contents = CONTENT_ICEGROW;
82 } else if(contentstring == "star") {
83 contents = CONTENT_STAR;
84 } else if(contentstring == "1up") {
85 contents = CONTENT_1UP;
86 } else if(contentstring == "custom") {
87 contents = CONTENT_CUSTOM;
89 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
92 if(contents == CONTENT_CUSTOM) {
93 GameObject* game_object = create_object(token, *(iter.lisp()));
94 object = dynamic_cast<MovingObject*> (game_object);
96 throw std::runtime_error(
97 "Only MovingObjects are allowed inside BonusBlocks");
99 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
104 if(contents == CONTENT_CUSTOM && object == 0)
105 throw std::runtime_error("Need to specify content object for custom block");
110 BonusBlock::~BonusBlock()
116 BonusBlock::hit(Player& )
122 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
124 Player* player = dynamic_cast<Player*> (&other);
126 if (player->does_buttjump) try_open();
129 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
131 // hit contains no information for collisions with blocks.
132 // Badguy's bottom has to be below the top of the block
133 // SHIFT_DELTA is required to slide over one tile gaps.
134 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
138 Portable* portable = dynamic_cast<Portable*> (&other);
140 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
141 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
145 return Block::collision(other, hit);
149 BonusBlock::try_open()
151 if(sprite->get_action() == "empty") {
152 sound_manager->play("sounds/brick.wav");
156 Sector* sector = Sector::current();
158 assert(sector->player);
159 Player& player = *(sector->player);
160 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
164 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
165 player.get_status()->add_coins(1);
166 Sector::current()->get_level()->stats.coins++;
169 case CONTENT_FIREGROW:
170 if(player.get_status()->bonus == NO_BONUS) {
171 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
172 sector->add_object(riser);
174 SpecialRiser* riser = new SpecialRiser(
175 get_pos(), new Flower(FIRE_BONUS));
176 sector->add_object(riser);
178 sound_manager->play("sounds/upgrade.wav");
181 case CONTENT_ICEGROW:
182 if(player.get_status()->bonus == NO_BONUS) {
183 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
184 sector->add_object(riser);
186 SpecialRiser* riser = new SpecialRiser(
187 get_pos(), new Flower(ICE_BONUS));
188 sector->add_object(riser);
190 sound_manager->play("sounds/upgrade.wav");
194 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
198 sector->add_object(new OneUp(get_pos(), direction));
202 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
204 sector->add_object(riser);
205 sound_manager->play("sounds/upgrade.wav");
209 start_bounce(&player);
210 sprite->set_action("empty");
216 Sector* sector = Sector::current();
218 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos(), Vector(-100, -400)));
220 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(0, 16),
221 Vector(-150, -300)));
223 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 0),
226 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 16),
231 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");