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/flower.hpp"
23 #include "object/bouncy_coin.hpp"
24 #include "object/coin_explode.hpp"
25 #include "object/coin_rain.hpp"
26 #include "object/growup.hpp"
27 #include "object/oneup.hpp"
28 #include "object/player.hpp"
29 #include "object/portable.hpp"
30 #include "object/powerup.hpp"
31 #include "object/specialriser.hpp"
32 #include "object/star.hpp"
33 #include "object/trampoline.hpp"
34 #include "sprite/sprite_manager.hpp"
35 #include "supertux/constants.hpp"
36 #include "supertux/level.hpp"
37 #include "supertux/object_factory.hpp"
38 #include "supertux/sector.hpp"
42 BonusBlock::BonusBlock(const Vector& pos, int data) :
43 Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
52 sprite->set_action("normal");
54 case 1: contents = CONTENT_COIN; break;
55 case 2: contents = CONTENT_FIREGROW; break;
56 case 3: contents = CONTENT_STAR; break;
57 case 4: contents = CONTENT_1UP; break;
58 case 5: contents = CONTENT_ICEGROW; break;
59 case 6: contents = CONTENT_LIGHT;
60 SoundManager::current()->preload("sounds/switch.ogg");
61 lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
63 case 7: contents = CONTENT_TRAMPOLINE;
64 //object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
66 case 8: contents = CONTENT_CUSTOM;
67 object = new Trampoline(get_pos(), true);
69 case 9: contents = CONTENT_CUSTOM;
70 object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
72 case 10: contents = CONTENT_RAIN; break;
73 case 11: contents = CONTENT_EXPLODE; break;
74 case 12: contents = CONTENT_CUSTOM;
75 object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
78 log_warning << "Invalid box contents" << std::endl;
79 contents = CONTENT_COIN;
84 BonusBlock::BonusBlock(const Reader& lisp) :
85 Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
95 contents = CONTENT_COIN;
96 lisp::ListIterator iter(&lisp);
98 const std::string& token = iter.item();
100 iter.value()->get(pos.x);
101 } else if(token == "y") {
102 iter.value()->get(pos.y);
103 } else if(token == "sprite") {
104 iter.value()->get(sprite_name);
105 sprite = SpriteManager::current()->create(sprite_name);
106 } else if(token == "count") {
107 iter.value()->get(hit_counter);
108 } else if(token == "script") {
109 iter.value()->get(script);
110 } else if(token == "contents") {
111 std::string contentstring;
112 iter.value()->get(contentstring);
113 if(contentstring == "coin") {
114 contents = CONTENT_COIN;
115 } else if(contentstring == "firegrow") {
116 contents = CONTENT_FIREGROW;
117 } else if(contentstring == "icegrow") {
118 contents = CONTENT_ICEGROW;
119 } else if(contentstring == "star") {
120 contents = CONTENT_STAR;
121 } else if(contentstring == "1up") {
122 contents = CONTENT_1UP;
123 } else if(contentstring == "custom") {
124 contents = CONTENT_CUSTOM;
125 } else if(contentstring == "script") { // use when bonusblock is to contain ONLY a script
126 contents = CONTENT_SCRIPT;
127 } else if(contentstring == "light") {
128 contents = CONTENT_LIGHT;
129 SoundManager::current()->preload("sounds/switch.ogg");
130 } else if(contentstring == "trampoline") {
131 contents = CONTENT_TRAMPOLINE;
132 } else if(contentstring == "rain") {
133 contents = CONTENT_RAIN;
134 } else if(contentstring == "explode") {
135 contents = CONTENT_EXPLODE;
137 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
140 if(contents == CONTENT_CUSTOM) {
141 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
142 object = dynamic_cast<MovingObject*> (game_object);
144 throw std::runtime_error(
145 "Only MovingObjects are allowed inside BonusBlocks");
147 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
152 if(contents == CONTENT_CUSTOM && object == 0)
153 throw std::runtime_error("Need to specify content object for custom block");
154 if(contents == CONTENT_LIGHT)
155 lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
160 BonusBlock::~BonusBlock()
166 BonusBlock::hit(Player & player)
172 BonusBlock::collision(GameObject& other, const CollisionHit& hit_){
174 Player* player = dynamic_cast<Player*> (&other);
176 if (player->does_buttjump)
180 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
182 // hit contains no information for collisions with blocks.
183 // Badguy's bottom has to be below the top of the block
184 // SHIFT_DELTA is required to slide over one tile gaps.
185 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
189 Portable* portable = dynamic_cast<Portable*> (&other);
191 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
192 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
196 return Block::collision(other, hit_);
200 BonusBlock::try_open(Player *player)
202 if(sprite->get_action() == "empty") {
203 SoundManager::current()->play("sounds/brick.wav");
207 Sector* sector = Sector::current();
211 player = sector->player;
216 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
221 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
222 player->get_status()->add_coins(1);
223 if (hit_counter != 0)
224 Sector::current()->get_level()->stats.coins++;
228 case CONTENT_FIREGROW:
230 if(player->get_status()->bonus == NO_BONUS) {
231 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
232 sector->add_object(riser);
234 SpecialRiser* riser = new SpecialRiser(
235 get_pos(), new Flower(FIRE_BONUS));
236 sector->add_object(riser);
238 SoundManager::current()->play("sounds/upgrade.wav");
242 case CONTENT_ICEGROW:
244 if(player->get_status()->bonus == NO_BONUS) {
245 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
246 sector->add_object(riser);
248 SpecialRiser* riser = new SpecialRiser(
249 get_pos(), new Flower(ICE_BONUS));
250 sector->add_object(riser);
252 SoundManager::current()->play("sounds/upgrade.wav");
258 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
259 SoundManager::current()->play("sounds/upgrade.wav");
265 sector->add_object(new OneUp(get_pos(), direction));
266 SoundManager::current()->play("sounds/upgrade.wav");
272 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
274 sector->add_object(riser);
275 SoundManager::current()->play("sounds/upgrade.wav");
280 { break; } // because scripts always run, this prevents default contents from being assumed
284 if(sprite->get_action() == "on")
285 sprite->set_action("off");
287 sprite->set_action("on");
288 SoundManager::current()->play("sounds/switch.ogg");
291 case CONTENT_TRAMPOLINE:
293 SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
294 sector->add_object(riser);
295 SoundManager::current()->play("sounds/upgrade.wav");
300 hit_counter = 1; // multiple hits of coin rain is not allowed
301 Sector::current()->add_object(new CoinRain(get_pos(), true));
302 SoundManager::current()->play("sounds/upgrade.wav");
305 case CONTENT_EXPLODE:
307 hit_counter = 1; // multiple hits of coin explode is not allowed
308 Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
309 SoundManager::current()->play("sounds/upgrade.wav");
314 if(script != "") { // scripts always run if defined
315 std::istringstream stream(script);
316 Sector::current()->run_script(stream, "BonusBlockScript");
319 start_bounce(player);
320 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
321 }else if(hit_counter == 1){
322 sprite->set_action("empty");
329 BonusBlock::try_drop(Player *player)
331 if(sprite->get_action() == "empty") {
332 SoundManager::current()->play("sounds/brick.wav");
336 Sector* sector = Sector::current();
339 // First what's below the bonus block, if solid send it up anyway (excepting doll)
341 dest_.p1.x = bbox.get_left() + 1;
342 dest_.p1.y = bbox.get_bottom() + 1;
343 dest_.p2.x = bbox.get_right() - 1;
344 dest_.p2.y = dest_.p1.y + 30;
345 if (!Sector::current()->is_free_of_statics(dest_, this, true) && !(contents == CONTENT_1UP)) {
351 player = sector->player;
356 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
358 bool countdown = false;
367 case CONTENT_FIREGROW:
369 sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
370 SoundManager::current()->play("sounds/upgrade.wav");
375 case CONTENT_ICEGROW:
377 sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
378 SoundManager::current()->play("sounds/upgrade.wav");
385 sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
386 SoundManager::current()->play("sounds/upgrade.wav");
393 sector->add_object(new OneUp(get_pos(), DOWN));
394 SoundManager::current()->play("sounds/upgrade.wav");
401 //NOTE: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
402 object->set_pos(get_pos() + Vector(0, 32));
403 sector->add_object(object);
405 SoundManager::current()->play("sounds/upgrade.wav");
411 { break; } // because scripts always run, this prevents default contents from being assumed
418 case CONTENT_TRAMPOLINE:
428 case CONTENT_EXPLODE:
430 hit_counter = 1; // multiple hits of coin explode is not allowed
431 Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
432 SoundManager::current()->play("sounds/upgrade.wav");
438 if(script != "") { // scripts always run if defined
439 std::istringstream stream(script);
440 Sector::current()->run_script(stream, "powerup-script");
443 if(countdown){ // only decrease hit counter if try_open was not called
444 if(hit_counter == 1){
445 sprite->set_action("empty");
453 BonusBlock::draw(DrawingContext& context){
454 // do the regular drawing first
455 Block::draw(context);
456 // then Draw the light if on.
457 if(sprite->get_action() == "on") {
458 Vector pos = get_pos() + (bbox.get_size().as_vector() - lightsprite->get_size()) / 2;
459 context.push_target();
460 context.set_target(DrawingContext::LIGHTMAP);
461 context.draw_surface(lightsprite, pos, 10);
462 context.pop_target();