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 "object/trampoline.hpp"
32 #include "sprite/sprite_manager.hpp"
33 #include "supertux/constants.hpp"
34 #include "supertux/level.hpp"
35 #include "supertux/object_factory.hpp"
36 #include "supertux/sector.hpp"
40 BonusBlock::BonusBlock(const Vector& pos, int data) :
41 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
48 sprite->set_action("normal");
50 case 1: contents = CONTENT_COIN; break;
51 case 2: contents = CONTENT_FIREGROW; break;
52 case 3: contents = CONTENT_STAR; break;
53 case 4: contents = CONTENT_1UP; break;
54 case 5: contents = CONTENT_ICEGROW; break;
55 case 6: contents = CONTENT_LIGHT;
56 sound_manager->preload("sounds/switch.ogg");
57 lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
59 case 7: contents = CONTENT_TRAMPOLINE; break;
60 case 8: contents = CONTENT_PORTTRAMPOLINE; break;
61 case 9: contents = CONTENT_ROCK; break;
63 log_warning << "Invalid box contents" << std::endl;
64 contents = CONTENT_COIN;
69 BonusBlock::BonusBlock(const Reader& lisp) :
70 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
78 contents = CONTENT_COIN;
79 lisp::ListIterator iter(&lisp);
81 const std::string& token = iter.item();
83 iter.value()->get(pos.x);
84 } else if(token == "y") {
85 iter.value()->get(pos.y);
86 } else if(token == "sprite") {
87 iter.value()->get(sprite_name);
88 sprite = sprite_manager->create(sprite_name);
89 } else if(token == "count") {
90 iter.value()->get(hit_counter);
91 } else if(token == "script") {
92 iter.value()->get(script);
93 } else if(token == "contents") {
94 std::string contentstring;
95 iter.value()->get(contentstring);
96 if(contentstring == "coin") {
97 contents = CONTENT_COIN;
98 } else if(contentstring == "firegrow") {
99 contents = CONTENT_FIREGROW;
100 } else if(contentstring == "icegrow") {
101 contents = CONTENT_ICEGROW;
102 } else if(contentstring == "star") {
103 contents = CONTENT_STAR;
104 } else if(contentstring == "1up") {
105 contents = CONTENT_1UP;
106 } else if(contentstring == "custom") {
107 contents = CONTENT_CUSTOM;
108 } else if(contentstring == "script") {
109 contents = CONTENT_SCRIPT;
110 } else if(contentstring == "light") {
111 contents = CONTENT_LIGHT;
112 sound_manager->preload("sounds/switch.ogg");
113 } else if(contentstring == "trampoline") {
114 contents = CONTENT_TRAMPOLINE;
115 } else if(contentstring == "porttrampoline") {
116 contents = CONTENT_PORTTRAMPOLINE;
117 } else if(contentstring == "rock") {
118 contents = CONTENT_ROCK;
120 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
123 if(contents == CONTENT_CUSTOM) {
124 GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
125 object = dynamic_cast<MovingObject*> (game_object);
127 throw std::runtime_error(
128 "Only MovingObjects are allowed inside BonusBlocks");
130 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
135 if(contents == CONTENT_CUSTOM && object == 0)
136 throw std::runtime_error("Need to specify content object for custom block");
137 if(contents == CONTENT_LIGHT)
138 lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
143 BonusBlock::~BonusBlock()
149 BonusBlock::hit(Player & player)
155 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
157 Player* player = dynamic_cast<Player*> (&other);
159 if (player->does_buttjump)
163 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
165 // hit contains no information for collisions with blocks.
166 // Badguy's bottom has to be below the top of the block
167 // SHIFT_DELTA is required to slide over one tile gaps.
168 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
172 Portable* portable = dynamic_cast<Portable*> (&other);
174 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
175 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
179 return Block::collision(other, hit);
183 BonusBlock::try_open(Player *player)
185 if(sprite->get_action() == "empty") {
186 sound_manager->play("sounds/brick.wav");
190 Sector* sector = Sector::current();
194 player = sector->player;
199 Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
204 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
205 player->get_status()->add_coins(1);
206 if (hit_counter != 0)
207 Sector::current()->get_level()->stats.coins++;
211 case CONTENT_FIREGROW:
213 if(player->get_status()->bonus == NO_BONUS) {
214 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
215 sector->add_object(riser);
217 SpecialRiser* riser = new SpecialRiser(
218 get_pos(), new Flower(FIRE_BONUS));
219 sector->add_object(riser);
221 sound_manager->play("sounds/upgrade.wav");
225 case CONTENT_ICEGROW:
227 if(player->get_status()->bonus == NO_BONUS) {
228 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
229 sector->add_object(riser);
231 SpecialRiser* riser = new SpecialRiser(
232 get_pos(), new Flower(ICE_BONUS));
233 sector->add_object(riser);
235 sound_manager->play("sounds/upgrade.wav");
241 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
247 sector->add_object(new OneUp(get_pos(), direction));
253 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
255 sector->add_object(riser);
256 sound_manager->play("sounds/upgrade.wav");
263 std::istringstream stream(script);
264 Sector::current()->run_script(stream, "powerup-script");
270 if(sprite->get_action() == "on")
271 sprite->set_action("off");
273 sprite->set_action("on");
274 sound_manager->play("sounds/switch.ogg");
277 case CONTENT_TRAMPOLINE:
279 SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
280 sector->add_object(riser);
281 sound_manager->play("sounds/upgrade.wav");
284 case CONTENT_PORTTRAMPOLINE:
286 SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), true));
287 sector->add_object(riser);
288 sound_manager->play("sounds/upgrade.wav");
293 SpecialRiser* riser = new SpecialRiser(get_pos(),
294 new Rock(get_pos(), "images/objects/rock/rock.sprite"));
295 sector->add_object(riser);
296 sound_manager->play("sounds/upgrade.wav");
301 start_bounce(player);
302 if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
303 }else if(hit_counter == 1){
304 sprite->set_action("empty");
313 Sector* sector = Sector::current();
315 new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
317 new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
318 Vector(-150, -300)));
320 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
323 new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
329 BonusBlock::draw(DrawingContext& context){
330 // draw regular sprite
331 sprite->draw(context, get_pos(), 10);
333 if(sprite->get_action() == "on") {
334 Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
335 context.push_target();
336 context.set_target(DrawingContext::LIGHTMAP);
337 context.draw_surface(lightsprite, pos, 10);
338 context.pop_target();