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 = std::make_shared<Trampoline>(get_pos(), true);
69 case 9: contents = CONTENT_CUSTOM;
70 object = std::make_shared<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 = std::make_shared<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 GameObjectPtr game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
142 object = std::dynamic_pointer_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()
165 BonusBlock::hit(Player & player)
171 BonusBlock::collision(GameObject& other, const CollisionHit& hit_){
173 Player* player = dynamic_cast<Player*> (&other);
175 if (player->does_buttjump)
179 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
181 // hit contains no information for collisions with blocks.
182 // Badguy's bottom has to be below the top of the block
183 // SHIFT_DELTA is required to slide over one tile gaps.
184 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
188 Portable* portable = dynamic_cast<Portable*> (&other);
190 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
191 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
195 return Block::collision(other, hit_);
199 BonusBlock::try_open(Player *player)
201 if(sprite->get_action() == "empty") {
202 SoundManager::current()->play("sounds/brick.wav");
206 Sector* sector = Sector::current();
210 player = sector->player;
215 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
220 Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
221 player->get_status()->add_coins(1);
222 if (hit_counter != 0)
223 Sector::current()->get_level()->stats.coins++;
227 case CONTENT_FIREGROW:
229 if(player->get_status()->bonus == NO_BONUS) {
230 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
231 sector->add_object(riser);
233 auto riser = std::make_shared<SpecialRiser>(
234 get_pos(), std::make_shared<Flower>(FIRE_BONUS));
235 sector->add_object(riser);
237 SoundManager::current()->play("sounds/upgrade.wav");
241 case CONTENT_ICEGROW:
243 if(player->get_status()->bonus == NO_BONUS) {
244 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
245 sector->add_object(riser);
247 auto riser = std::make_shared<SpecialRiser>(
248 get_pos(), std::make_shared<Flower>(ICE_BONUS));
249 sector->add_object(riser);
251 SoundManager::current()->play("sounds/upgrade.wav");
257 sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, -32), direction));
258 SoundManager::current()->play("sounds/upgrade.wav");
264 sector->add_object(std::make_shared<OneUp>(get_pos(), direction));
265 SoundManager::current()->play("sounds/upgrade.wav");
271 auto riser = std::make_shared<SpecialRiser>(get_pos(), object);
273 sector->add_object(riser);
274 SoundManager::current()->play("sounds/upgrade.wav");
279 { break; } // because scripts always run, this prevents default contents from being assumed
283 if(sprite->get_action() == "on")
284 sprite->set_action("off");
286 sprite->set_action("on");
287 SoundManager::current()->play("sounds/switch.ogg");
290 case CONTENT_TRAMPOLINE:
292 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<Trampoline>(get_pos(), false));
293 sector->add_object(riser);
294 SoundManager::current()->play("sounds/upgrade.wav");
299 hit_counter = 1; // multiple hits of coin rain is not allowed
300 Sector::current()->add_object(std::make_shared<CoinRain>(get_pos(), true));
301 SoundManager::current()->play("sounds/upgrade.wav");
304 case CONTENT_EXPLODE:
306 hit_counter = 1; // multiple hits of coin explode is not allowed
307 Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, -40)));
308 SoundManager::current()->play("sounds/upgrade.wav");
313 if(script != "") { // scripts always run if defined
314 std::istringstream stream(script);
315 Sector::current()->run_script(stream, "BonusBlockScript");
318 start_bounce(player);
319 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
320 }else if(hit_counter == 1){
321 sprite->set_action("empty");
328 BonusBlock::try_drop(Player *player)
330 if(sprite->get_action() == "empty") {
331 SoundManager::current()->play("sounds/brick.wav");
335 Sector* sector = Sector::current();
338 // First what's below the bonus block, if solid send it up anyway (excepting doll)
340 dest_.p1.x = bbox.get_left() + 1;
341 dest_.p1.y = bbox.get_bottom() + 1;
342 dest_.p2.x = bbox.get_right() - 1;
343 dest_.p2.y = dest_.p1.y + 30;
344 if (!Sector::current()->is_free_of_statics(dest_, this, true) && !(contents == CONTENT_1UP)) {
350 player = sector->player;
355 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
357 bool countdown = false;
366 case CONTENT_FIREGROW:
368 sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
369 SoundManager::current()->play("sounds/upgrade.wav");
374 case CONTENT_ICEGROW:
376 sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
377 SoundManager::current()->play("sounds/upgrade.wav");
384 sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, 32), direction));
385 SoundManager::current()->play("sounds/upgrade.wav");
392 sector->add_object(std::make_shared<OneUp>(get_pos(), DOWN));
393 SoundManager::current()->play("sounds/upgrade.wav");
400 //NOTE: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
401 object->set_pos(get_pos() + Vector(0, 32));
402 sector->add_object(object);
404 SoundManager::current()->play("sounds/upgrade.wav");
410 { break; } // because scripts always run, this prevents default contents from being assumed
417 case CONTENT_TRAMPOLINE:
427 case CONTENT_EXPLODE:
429 hit_counter = 1; // multiple hits of coin explode is not allowed
430 Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, 40)));
431 SoundManager::current()->play("sounds/upgrade.wav");
437 if(script != "") { // scripts always run if defined
438 std::istringstream stream(script);
439 Sector::current()->run_script(stream, "powerup-script");
442 if(countdown){ // only decrease hit counter if try_open was not called
443 if(hit_counter == 1){
444 sprite->set_action("empty");
452 BonusBlock::draw(DrawingContext& context){
453 // do the regular drawing first
454 Block::draw(context);
455 // then Draw the light if on.
456 if(sprite->get_action() == "on") {
457 Vector pos = get_pos() + (bbox.get_size().as_vector() - lightsprite->get_size()) / 2;
458 context.push_target();
459 context.set_target(DrawingContext::LIGHTMAP);
460 context.draw_surface(lightsprite, pos, 10);
461 context.pop_target();