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/growup.hpp"
26 #include "object/oneup.hpp"
27 #include "object/player.hpp"
28 #include "object/portable.hpp"
29 #include "object/specialriser.hpp"
30 #include "object/star.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "supertux/constants.hpp"
33 #include "supertux/level.hpp"
34 #include "supertux/object_factory.hpp"
35 #include "supertux/sector.hpp"
39 BonusBlock::BonusBlock(const Vector& pos, int data) :
40 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
44 lightsprite(Surface::create("/images/objects/lightmap_light/bonusblock_light.png"))
47 sprite->set_action("normal");
49 case 1: contents = CONTENT_COIN; break;
50 case 2: contents = CONTENT_FIREGROW; break;
51 case 3: contents = CONTENT_STAR; break;
52 case 4: contents = CONTENT_1UP; break;
53 case 5: contents = CONTENT_ICEGROW; break;
54 case 6: contents = CONTENT_LIGHT; sound_manager->preload("sounds/switch.ogg"); break;
56 log_warning << "Invalid box contents" << std::endl;
57 contents = CONTENT_COIN;
62 BonusBlock::BonusBlock(const Reader& lisp) :
63 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
67 lightsprite(Surface::create("/images/objects/lightmap_light/bonusblock_light.png"))
71 contents = CONTENT_COIN;
72 lisp::ListIterator iter(&lisp);
74 const std::string& token = iter.item();
76 iter.value()->get(pos.x);
77 } else if(token == "y") {
78 iter.value()->get(pos.y);
79 } else if(token == "sprite") {
80 iter.value()->get(sprite_name);
81 sprite = sprite_manager->create(sprite_name);
82 } else if(token == "count") {
83 iter.value()->get(hit_counter);
84 } else if(token == "script") {
85 iter.value()->get(script);
86 } else if(token == "contents") {
87 std::string contentstring;
88 iter.value()->get(contentstring);
89 if(contentstring == "coin") {
90 contents = CONTENT_COIN;
91 } else if(contentstring == "firegrow") {
92 contents = CONTENT_FIREGROW;
93 } else if(contentstring == "icegrow") {
94 contents = CONTENT_ICEGROW;
95 } else if(contentstring == "star") {
96 contents = CONTENT_STAR;
97 } else if(contentstring == "1up") {
98 contents = CONTENT_1UP;
99 } else if(contentstring == "custom") {
100 contents = CONTENT_CUSTOM;
101 } else if(contentstring == "script") {
102 contents = CONTENT_SCRIPT;
103 } else if(contentstring == "light") {
104 contents = CONTENT_LIGHT;
105 sound_manager->preload("sounds/switch.ogg");
107 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
110 if(contents == CONTENT_CUSTOM) {
111 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
112 object = dynamic_cast<MovingObject*> (game_object);
114 throw std::runtime_error(
115 "Only MovingObjects are allowed inside BonusBlocks");
117 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
122 if(contents == CONTENT_CUSTOM && object == 0)
123 throw std::runtime_error("Need to specify content object for custom block");
128 BonusBlock::~BonusBlock()
134 BonusBlock::hit(Player & player)
140 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
142 Player* player = dynamic_cast<Player*> (&other);
144 if (player->does_buttjump)
148 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
150 // hit contains no information for collisions with blocks.
151 // Badguy's bottom has to be below the top of the block
152 // SHIFT_DELTA is required to slide over one tile gaps.
153 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
157 Portable* portable = dynamic_cast<Portable*> (&other);
159 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
160 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
164 return Block::collision(other, hit);
168 BonusBlock::try_open(Player *player)
170 if(sprite->get_action() == "empty") {
171 sound_manager->play("sounds/brick.wav");
175 Sector* sector = Sector::current();
179 player = sector->player;
184 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
189 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
190 player->get_status()->add_coins(1);
191 if (hit_counter != 0)
192 Sector::current()->get_level()->stats.coins++;
196 case CONTENT_FIREGROW:
198 if(player->get_status()->bonus == NO_BONUS) {
199 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
200 sector->add_object(riser);
202 SpecialRiser* riser = new SpecialRiser(
203 get_pos(), new Flower(FIRE_BONUS));
204 sector->add_object(riser);
206 sound_manager->play("sounds/upgrade.wav");
210 case CONTENT_ICEGROW:
212 if(player->get_status()->bonus == NO_BONUS) {
213 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
214 sector->add_object(riser);
216 SpecialRiser* riser = new SpecialRiser(
217 get_pos(), new Flower(ICE_BONUS));
218 sector->add_object(riser);
220 sound_manager->play("sounds/upgrade.wav");
226 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
232 sector->add_object(new OneUp(get_pos(), direction));
238 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
240 sector->add_object(riser);
241 sound_manager->play("sounds/upgrade.wav");
248 std::istringstream stream(script);
249 Sector::current()->run_script(stream, "powerup-script");
255 if(sprite->get_action() == "on")
256 sprite->set_action("off");
258 sprite->set_action("on");
259 sound_manager->play("sounds/switch.ogg");
263 start_bounce(player);
264 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
265 }else if(hit_counter == 1){
266 sprite->set_action("empty");
275 Sector* sector = Sector::current();
277 new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
279 new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
280 Vector(-150, -300)));
282 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
285 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
291 BonusBlock::draw(DrawingContext& context){
292 // draw regular sprite
293 sprite->draw(context, get_pos(), 10);
295 if(sprite->get_action() == "on") {
296 Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
297 context.push_target();
298 context.set_target(DrawingContext::LIGHTMAP);
299 context.draw_surface(lightsprite, pos, 10);
300 context.pop_target();