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");
77 case 13: contents = CONTENT_AIRGROW; break;
78 case 14: contents = CONTENT_EARTHGROW; break;
80 log_warning << "Invalid box contents" << std::endl;
81 contents = CONTENT_COIN;
86 BonusBlock::BonusBlock(const Reader& lisp) :
87 Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
97 contents = CONTENT_COIN;
98 lisp::ListIterator iter(&lisp);
100 const std::string& token = iter.item();
102 iter.value()->get(pos.x);
103 } else if(token == "y") {
104 iter.value()->get(pos.y);
105 } else if(token == "sprite") {
106 iter.value()->get(sprite_name);
107 sprite = SpriteManager::current()->create(sprite_name);
108 } else if(token == "count") {
109 iter.value()->get(hit_counter);
110 } else if(token == "script") {
111 iter.value()->get(script);
112 } else if(token == "contents") {
113 std::string contentstring;
114 iter.value()->get(contentstring);
115 if(contentstring == "coin") {
116 contents = CONTENT_COIN;
117 } else if(contentstring == "firegrow") {
118 contents = CONTENT_FIREGROW;
119 } else if(contentstring == "icegrow") {
120 contents = CONTENT_ICEGROW;
121 } else if(contentstring == "airgrow") {
122 contents = CONTENT_AIRGROW;
123 } else if(contentstring == "earthgrow") {
124 contents = CONTENT_EARTHGROW;
125 } else if(contentstring == "star") {
126 contents = CONTENT_STAR;
127 } else if(contentstring == "1up") {
128 contents = CONTENT_1UP;
129 } else if(contentstring == "custom") {
130 contents = CONTENT_CUSTOM;
131 } else if(contentstring == "script") { // use when bonusblock is to contain ONLY a script
132 contents = CONTENT_SCRIPT;
133 } else if(contentstring == "light") {
134 contents = CONTENT_LIGHT;
135 SoundManager::current()->preload("sounds/switch.ogg");
136 } else if(contentstring == "trampoline") {
137 contents = CONTENT_TRAMPOLINE;
138 } else if(contentstring == "rain") {
139 contents = CONTENT_RAIN;
140 } else if(contentstring == "explode") {
141 contents = CONTENT_EXPLODE;
143 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
146 if(contents == CONTENT_CUSTOM) {
147 GameObjectPtr game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
148 object = std::dynamic_pointer_cast<MovingObject>(game_object);
150 throw std::runtime_error(
151 "Only MovingObjects are allowed inside BonusBlocks");
153 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
158 if(contents == CONTENT_CUSTOM && object == 0)
159 throw std::runtime_error("Need to specify content object for custom block");
160 if(contents == CONTENT_LIGHT)
161 lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
166 BonusBlock::~BonusBlock()
171 BonusBlock::hit(Player & player)
177 BonusBlock::collision(GameObject& other, const CollisionHit& hit_){
179 Player* player = dynamic_cast<Player*> (&other);
181 if (player->does_buttjump)
185 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
187 // hit contains no information for collisions with blocks.
188 // Badguy's bottom has to be below the top of the block
189 // SHIFT_DELTA is required to slide over one tile gaps.
190 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
194 Portable* portable = dynamic_cast<Portable*> (&other);
196 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
197 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
201 return Block::collision(other, hit_);
205 BonusBlock::try_open(Player *player)
207 if(sprite->get_action() == "empty") {
208 SoundManager::current()->play("sounds/brick.wav");
212 Sector* sector = Sector::current();
216 player = sector->player;
221 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
226 Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
227 player->get_status()->add_coins(1);
228 if (hit_counter != 0)
229 Sector::current()->get_level()->stats.coins++;
233 case CONTENT_FIREGROW:
235 if(player->get_status()->bonus == NO_BONUS) {
236 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
237 sector->add_object(riser);
239 auto riser = std::make_shared<SpecialRiser>(
240 get_pos(), std::make_shared<Flower>(FIRE_BONUS));
241 sector->add_object(riser);
243 SoundManager::current()->play("sounds/upgrade.wav");
247 case CONTENT_ICEGROW:
249 if(player->get_status()->bonus == NO_BONUS) {
250 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
251 sector->add_object(riser);
253 auto riser = std::make_shared<SpecialRiser>(
254 get_pos(), std::make_shared<Flower>(ICE_BONUS));
255 sector->add_object(riser);
257 SoundManager::current()->play("sounds/upgrade.wav");
261 case CONTENT_AIRGROW:
263 if(player->get_status()->bonus == NO_BONUS) {
264 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
265 sector->add_object(riser);
267 auto riser = std::make_shared<SpecialRiser>(
268 get_pos(), std::make_shared<Flower>(AIR_BONUS));
269 sector->add_object(riser);
271 SoundManager::current()->play("sounds/upgrade.wav");
275 case CONTENT_EARTHGROW:
277 if(player->get_status()->bonus == NO_BONUS) {
278 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
279 sector->add_object(riser);
281 auto riser = std::make_shared<SpecialRiser>(
282 get_pos(), std::make_shared<Flower>(EARTH_BONUS));
283 sector->add_object(riser);
285 SoundManager::current()->play("sounds/upgrade.wav");
291 sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, -32), direction));
292 SoundManager::current()->play("sounds/upgrade.wav");
298 sector->add_object(std::make_shared<OneUp>(get_pos(), direction));
299 SoundManager::current()->play("sounds/upgrade.wav");
305 auto riser = std::make_shared<SpecialRiser>(get_pos(), object);
307 sector->add_object(riser);
308 SoundManager::current()->play("sounds/upgrade.wav");
313 { break; } // because scripts always run, this prevents default contents from being assumed
317 if(sprite->get_action() == "on")
318 sprite->set_action("off");
320 sprite->set_action("on");
321 SoundManager::current()->play("sounds/switch.ogg");
324 case CONTENT_TRAMPOLINE:
326 auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<Trampoline>(get_pos(), false));
327 sector->add_object(riser);
328 SoundManager::current()->play("sounds/upgrade.wav");
333 hit_counter = 1; // multiple hits of coin rain is not allowed
334 Sector::current()->add_object(std::make_shared<CoinRain>(get_pos(), true));
335 SoundManager::current()->play("sounds/upgrade.wav");
338 case CONTENT_EXPLODE:
340 hit_counter = 1; // multiple hits of coin explode is not allowed
341 Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, -40)));
342 SoundManager::current()->play("sounds/upgrade.wav");
347 if(script != "") { // scripts always run if defined
348 std::istringstream stream(script);
349 Sector::current()->run_script(stream, "BonusBlockScript");
352 start_bounce(player);
353 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
354 }else if(hit_counter == 1){
355 sprite->set_action("empty");
362 BonusBlock::try_drop(Player *player)
364 if(sprite->get_action() == "empty") {
365 SoundManager::current()->play("sounds/brick.wav");
369 Sector* sector = Sector::current();
372 // First what's below the bonus block, if solid send it up anyway (excepting doll)
374 dest_.p1.x = bbox.get_left() + 1;
375 dest_.p1.y = bbox.get_bottom() + 1;
376 dest_.p2.x = bbox.get_right() - 1;
377 dest_.p2.y = dest_.p1.y + 30;
378 if (!Sector::current()->is_free_of_statics(dest_, this, true) && !(contents == CONTENT_1UP)) {
384 player = sector->player;
389 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
391 bool countdown = false;
400 case CONTENT_FIREGROW:
402 sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
403 SoundManager::current()->play("sounds/upgrade.wav");
408 case CONTENT_ICEGROW:
410 sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
411 SoundManager::current()->play("sounds/upgrade.wav");
416 case CONTENT_AIRGROW:
418 sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/airflower/airflower.sprite"));
419 SoundManager::current()->play("sounds/upgrade.wav");
424 case CONTENT_EARTHGROW:
426 sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/earthflower/earthflower.sprite"));
427 SoundManager::current()->play("sounds/upgrade.wav");
434 sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, 32), direction));
435 SoundManager::current()->play("sounds/upgrade.wav");
442 sector->add_object(std::make_shared<OneUp>(get_pos(), DOWN));
443 SoundManager::current()->play("sounds/upgrade.wav");
450 //NOTE: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
451 object->set_pos(get_pos() + Vector(0, 32));
452 sector->add_object(object);
454 SoundManager::current()->play("sounds/upgrade.wav");
460 { break; } // because scripts always run, this prevents default contents from being assumed
467 case CONTENT_TRAMPOLINE:
477 case CONTENT_EXPLODE:
479 hit_counter = 1; // multiple hits of coin explode is not allowed
480 Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, 40)));
481 SoundManager::current()->play("sounds/upgrade.wav");
487 if(script != "") { // scripts always run if defined
488 std::istringstream stream(script);
489 Sector::current()->run_script(stream, "powerup-script");
492 if(countdown){ // only decrease hit counter if try_open was not called
493 if(hit_counter == 1){
494 sprite->set_action("empty");
502 BonusBlock::draw(DrawingContext& context){
503 // do the regular drawing first
504 Block::draw(context);
505 // then Draw the light if on.
506 if(sprite->get_action() == "on") {
507 Vector pos = get_pos() + (bbox.get_size().as_vector() - lightsprite->get_size()) / 2;
508 context.push_target();
509 context.set_target(DrawingContext::LIGHTMAP);
510 context.draw_surface(lightsprite, pos, 10);
511 context.pop_target();