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/flower.hpp"
24 #include "object/bouncy_coin.hpp"
25 #include "object/growup.hpp"
26 #include "object/oneup.hpp"
27 #include "object/player.hpp"
28 #include "object/portable.hpp"
29 #include "object/specialriser.hpp"
30 #include "object/star.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "supertux/constants.hpp"
33 #include "supertux/level.hpp"
34 #include "supertux/object_factory.hpp"
35 #include "supertux/sector.hpp"
37 BonusBlock::BonusBlock(const Vector& pos, int data) :
38 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
43 sprite->set_action("normal");
45 case 1: contents = CONTENT_COIN; break;
46 case 2: contents = CONTENT_FIREGROW; break;
47 case 3: contents = CONTENT_STAR; break;
48 case 4: contents = CONTENT_1UP; break;
49 case 5: contents = CONTENT_ICEGROW; break;
51 log_warning << "Invalid box contents" << std::endl;
52 contents = CONTENT_COIN;
57 BonusBlock::BonusBlock(const Reader& lisp) :
58 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
64 contents = CONTENT_COIN;
65 lisp::ListIterator iter(&lisp);
67 const std::string& token = iter.item();
69 iter.value()->get(pos.x);
70 } else if(token == "y") {
71 iter.value()->get(pos.y);
72 } else if(token == "contents") {
73 std::string contentstring;
74 iter.value()->get(contentstring);
75 if(contentstring == "coin") {
76 contents = CONTENT_COIN;
77 } else if(contentstring == "firegrow") {
78 contents = CONTENT_FIREGROW;
79 } else if(contentstring == "icegrow") {
80 contents = CONTENT_ICEGROW;
81 } else if(contentstring == "star") {
82 contents = CONTENT_STAR;
83 } else if(contentstring == "1up") {
84 contents = CONTENT_1UP;
85 } else if(contentstring == "custom") {
86 contents = CONTENT_CUSTOM;
88 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
91 if(contents == CONTENT_CUSTOM) {
92 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
93 object = dynamic_cast<MovingObject*> (game_object);
95 throw std::runtime_error(
96 "Only MovingObjects are allowed inside BonusBlocks");
98 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
103 if(contents == CONTENT_CUSTOM && object == 0)
104 throw std::runtime_error("Need to specify content object for custom block");
109 BonusBlock::~BonusBlock()
115 BonusBlock::hit(Player& )
121 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
123 Player* player = dynamic_cast<Player*> (&other);
125 if (player->does_buttjump) try_open();
128 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
130 // hit contains no information for collisions with blocks.
131 // Badguy's bottom has to be below the top of the block
132 // SHIFT_DELTA is required to slide over one tile gaps.
133 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
137 Portable* portable = dynamic_cast<Portable*> (&other);
139 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
140 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
144 return Block::collision(other, hit);
148 BonusBlock::try_open()
150 if(sprite->get_action() == "empty") {
151 sound_manager->play("sounds/brick.wav");
155 Sector* sector = Sector::current();
157 assert(sector->player);
158 Player& player = *(sector->player);
159 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
163 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
164 player.get_status()->add_coins(1);
165 Sector::current()->get_level()->stats.coins++;
168 case CONTENT_FIREGROW:
169 if(player.get_status()->bonus == NO_BONUS) {
170 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
171 sector->add_object(riser);
173 SpecialRiser* riser = new SpecialRiser(
174 get_pos(), new Flower(FIRE_BONUS));
175 sector->add_object(riser);
177 sound_manager->play("sounds/upgrade.wav");
180 case CONTENT_ICEGROW:
181 if(player.get_status()->bonus == NO_BONUS) {
182 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
183 sector->add_object(riser);
185 SpecialRiser* riser = new SpecialRiser(
186 get_pos(), new Flower(ICE_BONUS));
187 sector->add_object(riser);
189 sound_manager->play("sounds/upgrade.wav");
193 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
197 sector->add_object(new OneUp(get_pos(), direction));
201 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
203 sector->add_object(riser);
204 sound_manager->play("sounds/upgrade.wav");
208 start_bounce(&player);
209 sprite->set_action("empty");
215 Sector* sector = Sector::current();
217 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos(), Vector(-100, -400)));
219 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(0, 16),
220 Vector(-150, -300)));
222 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 0),
225 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 16),