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/coin_explode.hpp"
26 #include "object/coin_rain.hpp"
27 #include "object/growup.hpp"
28 #include "object/oneup.hpp"
29 #include "object/player.hpp"
30 #include "object/portable.hpp"
31 #include "object/powerup.hpp"
32 #include "object/specialriser.hpp"
33 #include "object/star.hpp"
34 #include "object/trampoline.hpp"
35 #include "sprite/sprite_manager.hpp"
36 #include "supertux/constants.hpp"
37 #include "supertux/level.hpp"
38 #include "supertux/object_factory.hpp"
39 #include "supertux/sector.hpp"
43 BonusBlock::BonusBlock(const Vector& pos, int data) :
44 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
51 sprite->set_action("normal");
53 case 1: contents = CONTENT_COIN; break;
54 case 2: contents = CONTENT_FIREGROW; break;
55 case 3: contents = CONTENT_STAR; break;
56 case 4: contents = CONTENT_1UP; break;
57 case 5: contents = CONTENT_ICEGROW; break;
58 case 6: contents = CONTENT_LIGHT;
59 sound_manager->preload("sounds/switch.ogg");
60 lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
62 case 7: contents = CONTENT_TRAMPOLINE;
63 //object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
65 case 8: contents = CONTENT_CUSTOM;
66 object = new Trampoline(get_pos(), true);
68 case 9: contents = CONTENT_CUSTOM;
69 object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
71 case 10: contents = CONTENT_RAIN; break;
72 case 11: contents = CONTENT_EXPLODE; break;
73 case 12: contents = CONTENT_CUSTOM;
74 object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
77 log_warning << "Invalid box contents" << std::endl;
78 contents = CONTENT_COIN;
83 BonusBlock::BonusBlock(const Reader& lisp) :
84 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
92 contents = CONTENT_COIN;
93 lisp::ListIterator iter(&lisp);
95 const std::string& token = iter.item();
97 iter.value()->get(pos.x);
98 } else if(token == "y") {
99 iter.value()->get(pos.y);
100 } else if(token == "sprite") {
101 iter.value()->get(sprite_name);
102 sprite = sprite_manager->create(sprite_name);
103 } else if(token == "count") {
104 iter.value()->get(hit_counter);
105 } else if(token == "script") {
106 iter.value()->get(script);
107 } else if(token == "contents") {
108 std::string contentstring;
109 iter.value()->get(contentstring);
110 if(contentstring == "coin") {
111 contents = CONTENT_COIN;
112 } else if(contentstring == "firegrow") {
113 contents = CONTENT_FIREGROW;
114 } else if(contentstring == "icegrow") {
115 contents = CONTENT_ICEGROW;
116 } else if(contentstring == "star") {
117 contents = CONTENT_STAR;
118 } else if(contentstring == "1up") {
119 contents = CONTENT_1UP;
120 } else if(contentstring == "custom") {
121 contents = CONTENT_CUSTOM;
122 } else if(contentstring == "script") { // use when bonusblock is to contain ONLY a script
123 contents = CONTENT_SCRIPT;
124 } else if(contentstring == "light") {
125 contents = CONTENT_LIGHT;
126 sound_manager->preload("sounds/switch.ogg");
127 } else if(contentstring == "trampoline") {
128 contents = CONTENT_TRAMPOLINE;
129 } else if(contentstring == "rain") {
130 contents = CONTENT_RAIN;
131 } else if(contentstring == "explode") {
132 contents = CONTENT_EXPLODE;
134 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
137 if(contents == CONTENT_CUSTOM) {
138 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
139 object = dynamic_cast<MovingObject*> (game_object);
141 throw std::runtime_error(
142 "Only MovingObjects are allowed inside BonusBlocks");
144 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
149 if(contents == CONTENT_CUSTOM && object == 0)
150 throw std::runtime_error("Need to specify content object for custom block");
151 if(contents == CONTENT_LIGHT)
152 lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
157 BonusBlock::~BonusBlock()
163 BonusBlock::hit(Player & player)
169 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
171 Player* player = dynamic_cast<Player*> (&other);
173 if (player->does_buttjump)
177 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
179 // hit contains no information for collisions with blocks.
180 // Badguy's bottom has to be below the top of the block
181 // SHIFT_DELTA is required to slide over one tile gaps.
182 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
186 Portable* portable = dynamic_cast<Portable*> (&other);
188 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
189 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
193 return Block::collision(other, hit);
197 BonusBlock::try_open(Player *player)
199 if(sprite->get_action() == "empty") {
200 sound_manager->play("sounds/brick.wav");
204 Sector* sector = Sector::current();
208 player = sector->player;
213 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
218 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
219 player->get_status()->add_coins(1);
220 if (hit_counter != 0)
221 Sector::current()->get_level()->stats.coins++;
225 case CONTENT_FIREGROW:
227 if(player->get_status()->bonus == NO_BONUS) {
228 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
229 sector->add_object(riser);
231 SpecialRiser* riser = new SpecialRiser(
232 get_pos(), new Flower(FIRE_BONUS));
233 sector->add_object(riser);
235 sound_manager->play("sounds/upgrade.wav");
239 case CONTENT_ICEGROW:
241 if(player->get_status()->bonus == NO_BONUS) {
242 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
243 sector->add_object(riser);
245 SpecialRiser* riser = new SpecialRiser(
246 get_pos(), new Flower(ICE_BONUS));
247 sector->add_object(riser);
249 sound_manager->play("sounds/upgrade.wav");
255 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
256 sound_manager->play("sounds/upgrade.wav");
262 sector->add_object(new OneUp(get_pos(), direction));
263 sound_manager->play("sounds/upgrade.wav");
269 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
271 sector->add_object(riser);
272 sound_manager->play("sounds/upgrade.wav");
277 { break; } // because scripts always run, this prevents default contents from being assumed
281 if(sprite->get_action() == "on")
282 sprite->set_action("off");
284 sprite->set_action("on");
285 sound_manager->play("sounds/switch.ogg");
288 case CONTENT_TRAMPOLINE:
290 SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
291 sector->add_object(riser);
292 sound_manager->play("sounds/upgrade.wav");
297 hit_counter = 1; // multiple hits of coin rain is not allowed
298 Sector::current()->add_object(new CoinRain(get_pos(), true));
299 sound_manager->play("sounds/upgrade.wav");
302 case CONTENT_EXPLODE:
304 hit_counter = 1; // multiple hits of coin explode is not allowed
305 Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
306 sound_manager->play("sounds/upgrade.wav");
311 if(script != "") { // scripts always run if defined
312 std::istringstream stream(script);
313 Sector::current()->run_script(stream, "BonusBlockScript");
316 start_bounce(player);
317 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
318 }else if(hit_counter == 1){
319 sprite->set_action("empty");
326 BonusBlock::try_drop(Player *player)
328 if(sprite->get_action() == "empty") {
329 sound_manager->play("sounds/brick.wav");
333 Sector* sector = Sector::current();
336 // First what's below the bonus block, if solid send it up anyway (excepting doll)
338 dest.p1.x = bbox.get_left() + 1;
339 dest.p1.y = bbox.get_bottom() + 1;
340 dest.p2.x = bbox.get_right() - 1;
341 dest.p2.y = dest.p1.y + 30;
342 if (!Sector::current()->is_free_of_statics(dest, this, true) && !(contents == CONTENT_1UP)) {
348 player = sector->player;
353 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
362 case CONTENT_FIREGROW:
364 sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
365 sound_manager->play("sounds/upgrade.wav");
369 case CONTENT_ICEGROW:
371 sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
372 sound_manager->play("sounds/upgrade.wav");
378 sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
379 sound_manager->play("sounds/upgrade.wav");
385 sector->add_object(new OneUp(get_pos(), DOWN));
386 sound_manager->play("sounds/upgrade.wav");
392 //TODO: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
393 object->set_pos(get_pos() + Vector(0, 32));
394 sector->add_object(object);
396 sound_manager->play("sounds/upgrade.wav");
401 { break; } // because scripts always run, this prevents default contents from being assumed
408 case CONTENT_TRAMPOLINE:
418 case CONTENT_EXPLODE:
420 hit_counter = 1; // multiple hits of coin explode is not allowed
421 Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
422 sound_manager->play("sounds/upgrade.wav");
427 if(script != "") { // scripts always run if defined
428 std::istringstream stream(script);
429 Sector::current()->run_script(stream, "powerup-script");
432 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
433 }else if(hit_counter == 1){
434 sprite->set_action("empty");
443 Sector* sector = Sector::current();
445 new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
447 new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
448 Vector(-150, -300)));
450 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
453 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
459 BonusBlock::draw(DrawingContext& context){
460 // do the regular drawing first
461 Block::draw(context);
462 // then Draw the light if on.
463 if(sprite->get_action() == "on") {
464 Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
465 context.push_target();
466 context.set_target(DrawingContext::LIGHTMAP);
467 context.draw_surface(lightsprite, pos, 10);
468 context.pop_target();