2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
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")),
53 sprite->set_action("normal");
55 case 1: contents = CONTENT_COIN; break;
56 case 2: contents = CONTENT_FIREGROW; break;
57 case 3: contents = CONTENT_STAR; break;
58 case 4: contents = CONTENT_1UP; break;
59 case 5: contents = CONTENT_ICEGROW; break;
60 case 6: contents = CONTENT_LIGHT;
61 sound_manager->preload("sounds/switch.ogg");
62 lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
64 case 7: contents = CONTENT_TRAMPOLINE;
65 //object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
67 case 8: contents = CONTENT_CUSTOM;
68 object = new Trampoline(get_pos(), true);
70 case 9: contents = CONTENT_CUSTOM;
71 object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
73 case 10: contents = CONTENT_RAIN; break;
74 case 11: contents = CONTENT_EXPLODE; break;
75 case 12: contents = CONTENT_CUSTOM;
76 object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
79 log_warning << "Invalid box contents" << std::endl;
80 contents = CONTENT_COIN;
85 BonusBlock::BonusBlock(const Reader& lisp) :
86 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
96 contents = CONTENT_COIN;
97 lisp::ListIterator iter(&lisp);
99 const std::string& token = iter.item();
101 iter.value()->get(pos.x);
102 } else if(token == "y") {
103 iter.value()->get(pos.y);
104 } else if(token == "sprite") {
105 iter.value()->get(sprite_name);
106 sprite = sprite_manager->create(sprite_name);
107 } else if(token == "count") {
108 iter.value()->get(hit_counter);
109 } else if(token == "script") {
110 iter.value()->get(script);
111 } else if(token == "contents") {
112 std::string contentstring;
113 iter.value()->get(contentstring);
114 if(contentstring == "coin") {
115 contents = CONTENT_COIN;
116 } else if(contentstring == "firegrow") {
117 contents = CONTENT_FIREGROW;
118 } else if(contentstring == "icegrow") {
119 contents = CONTENT_ICEGROW;
120 } else if(contentstring == "star") {
121 contents = CONTENT_STAR;
122 } else if(contentstring == "1up") {
123 contents = CONTENT_1UP;
124 } else if(contentstring == "custom") {
125 contents = CONTENT_CUSTOM;
126 } else if(contentstring == "script") { // use when bonusblock is to contain ONLY a script
127 contents = CONTENT_SCRIPT;
128 } else if(contentstring == "light") {
129 contents = CONTENT_LIGHT;
130 sound_manager->preload("sounds/switch.ogg");
131 } else if(contentstring == "trampoline") {
132 contents = CONTENT_TRAMPOLINE;
133 } else if(contentstring == "rain") {
134 contents = CONTENT_RAIN;
135 } else if(contentstring == "explode") {
136 contents = CONTENT_EXPLODE;
138 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
141 if(contents == CONTENT_CUSTOM) {
142 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
143 object = dynamic_cast<MovingObject*> (game_object);
145 throw std::runtime_error(
146 "Only MovingObjects are allowed inside BonusBlocks");
148 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
153 if(contents == CONTENT_CUSTOM && object == 0)
154 throw std::runtime_error("Need to specify content object for custom block");
155 if(contents == CONTENT_LIGHT)
156 lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
161 BonusBlock::~BonusBlock()
167 BonusBlock::hit(Player & player)
173 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
175 Player* player = dynamic_cast<Player*> (&other);
177 if (player->does_buttjump)
181 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
183 // hit contains no information for collisions with blocks.
184 // Badguy's bottom has to be below the top of the block
185 // SHIFT_DELTA is required to slide over one tile gaps.
186 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
190 Portable* portable = dynamic_cast<Portable*> (&other);
192 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
193 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
197 return Block::collision(other, hit);
201 BonusBlock::try_open(Player *player)
203 if(sprite->get_action() == "empty") {
204 sound_manager->play("sounds/brick.wav");
208 Sector* sector = Sector::current();
212 player = sector->player;
217 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
222 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
223 player->get_status()->add_coins(1);
224 if (hit_counter != 0)
225 Sector::current()->get_level()->stats.coins++;
229 case CONTENT_FIREGROW:
231 if(player->get_status()->bonus == NO_BONUS) {
232 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
233 sector->add_object(riser);
235 SpecialRiser* riser = new SpecialRiser(
236 get_pos(), new Flower(FIRE_BONUS));
237 sector->add_object(riser);
239 sound_manager->play("sounds/upgrade.wav");
243 case CONTENT_ICEGROW:
245 if(player->get_status()->bonus == NO_BONUS) {
246 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
247 sector->add_object(riser);
249 SpecialRiser* riser = new SpecialRiser(
250 get_pos(), new Flower(ICE_BONUS));
251 sector->add_object(riser);
253 sound_manager->play("sounds/upgrade.wav");
259 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
260 sound_manager->play("sounds/upgrade.wav");
266 sector->add_object(new OneUp(get_pos(), direction));
267 sound_manager->play("sounds/upgrade.wav");
273 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
275 sector->add_object(riser);
276 sound_manager->play("sounds/upgrade.wav");
281 { break; } // because scripts always run, this prevents default contents from being assumed
285 if(sprite->get_action() == "on")
286 sprite->set_action("off");
288 sprite->set_action("on");
289 sound_manager->play("sounds/switch.ogg");
292 case CONTENT_TRAMPOLINE:
294 SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
295 sector->add_object(riser);
296 sound_manager->play("sounds/upgrade.wav");
301 hit_counter = 1; // multiple hits of coin rain is not allowed
302 Sector::current()->add_object(new CoinRain(get_pos(), true));
303 sound_manager->play("sounds/upgrade.wav");
306 case CONTENT_EXPLODE:
308 hit_counter = 1; // multiple hits of coin explode is not allowed
309 Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
310 sound_manager->play("sounds/upgrade.wav");
315 if(script != "") { // scripts always run if defined
316 std::istringstream stream(script);
317 Sector::current()->run_script(stream, "BonusBlockScript");
320 start_bounce(player);
321 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
322 }else if(hit_counter == 1){
323 sprite->set_action("empty");
330 BonusBlock::try_drop(Player *player)
332 if(sprite->get_action() == "empty") {
333 sound_manager->play("sounds/brick.wav");
337 Sector* sector = Sector::current();
340 // First what's below the bonus block, if solid send it up anyway (excepting doll)
342 dest.p1.x = bbox.get_left() + 1;
343 dest.p1.y = bbox.get_bottom() + 1;
344 dest.p2.x = bbox.get_right() - 1;
345 dest.p2.y = dest.p1.y + 30;
346 if (!Sector::current()->is_free_of_statics(dest, this, true) && !(contents == CONTENT_1UP)) {
352 player = sector->player;
357 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
359 bool countdown = false;
368 case CONTENT_FIREGROW:
370 sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
371 sound_manager->play("sounds/upgrade.wav");
376 case CONTENT_ICEGROW:
378 sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
379 sound_manager->play("sounds/upgrade.wav");
386 sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
387 sound_manager->play("sounds/upgrade.wav");
394 sector->add_object(new OneUp(get_pos(), DOWN));
395 sound_manager->play("sounds/upgrade.wav");
402 //NOTE: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
403 object->set_pos(get_pos() + Vector(0, 32));
404 sector->add_object(object);
406 sound_manager->play("sounds/upgrade.wav");
412 { break; } // because scripts always run, this prevents default contents from being assumed
419 case CONTENT_TRAMPOLINE:
429 case CONTENT_EXPLODE:
431 hit_counter = 1; // multiple hits of coin explode is not allowed
432 Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
433 sound_manager->play("sounds/upgrade.wav");
439 if(script != "") { // scripts always run if defined
440 std::istringstream stream(script);
441 Sector::current()->run_script(stream, "powerup-script");
444 if(countdown){ // only decrease hit counter if try_open was not called
445 if(hit_counter == 1){
446 sprite->set_action("empty");
456 Sector* sector = Sector::current();
458 new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
460 new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
461 Vector(-150, -300)));
463 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
466 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
472 BonusBlock::draw(DrawingContext& context){
473 // do the regular drawing first
474 Block::draw(context);
475 // then Draw the light if on.
476 if(sprite->get_action() == "on") {
477 Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
478 context.push_target();
479 context.set_target(DrawingContext::LIGHTMAP);
480 context.draw_surface(lightsprite, pos, 10);
481 context.pop_target();