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"
39 BonusBlock::BonusBlock(const Vector& pos, int data) :
40 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
46 sprite->set_action("normal");
48 case 1: contents = CONTENT_COIN; break;
49 case 2: contents = CONTENT_FIREGROW; break;
50 case 3: contents = CONTENT_STAR; break;
51 case 4: contents = CONTENT_1UP; break;
52 case 5: contents = CONTENT_ICEGROW; break;
54 log_warning << "Invalid box contents" << std::endl;
55 contents = CONTENT_COIN;
60 BonusBlock::BonusBlock(const Reader& lisp) :
61 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
68 contents = CONTENT_COIN;
69 lisp::ListIterator iter(&lisp);
71 const std::string& token = iter.item();
73 iter.value()->get(pos.x);
74 } else if(token == "y") {
75 iter.value()->get(pos.y);
76 } else if(token == "sprite") {
77 iter.value()->get(sprite_name);
78 sprite = sprite_manager->create(sprite_name);
79 } else if(token == "count") {
80 iter.value()->get(hit_counter);
81 } else if(token == "script") {
82 iter.value()->get(script);
83 } else if(token == "contents") {
84 std::string contentstring;
85 iter.value()->get(contentstring);
86 if(contentstring == "coin") {
87 contents = CONTENT_COIN;
88 } else if(contentstring == "firegrow") {
89 contents = CONTENT_FIREGROW;
90 } else if(contentstring == "icegrow") {
91 contents = CONTENT_ICEGROW;
92 } else if(contentstring == "star") {
93 contents = CONTENT_STAR;
94 } else if(contentstring == "1up") {
95 contents = CONTENT_1UP;
96 } else if(contentstring == "custom") {
97 contents = CONTENT_CUSTOM;
98 } else if(contentstring == "script") {
99 contents = CONTENT_SCRIPT;
101 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
104 if(contents == CONTENT_CUSTOM) {
105 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
106 object = dynamic_cast<MovingObject*> (game_object);
108 throw std::runtime_error(
109 "Only MovingObjects are allowed inside BonusBlocks");
111 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
116 if(contents == CONTENT_CUSTOM && object == 0)
117 throw std::runtime_error("Need to specify content object for custom block");
122 BonusBlock::~BonusBlock()
128 BonusBlock::hit(Player & player)
134 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
136 Player* player = dynamic_cast<Player*> (&other);
138 if (player->does_buttjump)
142 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
144 // hit contains no information for collisions with blocks.
145 // Badguy's bottom has to be below the top of the block
146 // SHIFT_DELTA is required to slide over one tile gaps.
147 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
151 Portable* portable = dynamic_cast<Portable*> (&other);
153 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
154 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
158 return Block::collision(other, hit);
162 BonusBlock::try_open(Player *player)
164 if(sprite->get_action() == "empty") {
165 sound_manager->play("sounds/brick.wav");
169 Sector* sector = Sector::current();
173 player = sector->player;
178 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
182 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
183 player->get_status()->add_coins(1);
184 Sector::current()->get_level()->stats.coins++;
187 case CONTENT_FIREGROW:
188 if(player->get_status()->bonus == NO_BONUS) {
189 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
190 sector->add_object(riser);
192 SpecialRiser* riser = new SpecialRiser(
193 get_pos(), new Flower(FIRE_BONUS));
194 sector->add_object(riser);
196 sound_manager->play("sounds/upgrade.wav");
199 case CONTENT_ICEGROW:
200 if(player->get_status()->bonus == NO_BONUS) {
201 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
202 sector->add_object(riser);
204 SpecialRiser* riser = new SpecialRiser(
205 get_pos(), new Flower(ICE_BONUS));
206 sector->add_object(riser);
208 sound_manager->play("sounds/upgrade.wav");
212 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
216 sector->add_object(new OneUp(get_pos(), direction));
221 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
223 sector->add_object(riser);
224 sound_manager->play("sounds/upgrade.wav");
230 std::istringstream stream(script);
231 Sector::current()->run_script(stream, "powerup-script");
236 start_bounce(player);
237 if(hit_counter <= 0){ //use 0 to allow infinite hits
238 }else if(hit_counter == 1){
239 sprite->set_action("empty");
248 Sector* sector = Sector::current();
250 new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
252 new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
253 Vector(-150, -300)));
255 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
258 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),