New BonusBlock content: 'explode' - a coin explosion.
[supertux.git] / src / object / bonus_block.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "object/bonus_block.hpp"
18
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/coin_explode.hpp"
26 #include "object/coin_rain.hpp"
27 #include "object/growup.hpp"
28 #include "object/oneup.hpp"
29 #include "object/player.hpp"
30 #include "object/portable.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"
39
40 #include <stdexcept>
41
42 BonusBlock::BonusBlock(const Vector& pos, int data) :
43   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
44   contents(),
45   object(0),
46   hit_counter(1),
47   lightsprite()
48 {
49   bbox.set_pos(pos);
50   sprite->set_action("normal");
51   switch(data) {
52     case 1: contents = CONTENT_COIN; break;
53     case 2: contents = CONTENT_FIREGROW; break;
54     case 3: contents = CONTENT_STAR; break;
55     case 4: contents = CONTENT_1UP; break;
56     case 5: contents = CONTENT_ICEGROW; break;
57     case 6: contents = CONTENT_LIGHT; 
58       sound_manager->preload("sounds/switch.ogg"); 
59       lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
60       break;
61     case 7: contents = CONTENT_TRAMPOLINE; break;
62     case 8: contents = CONTENT_PORTTRAMPOLINE; break;
63     case 9: contents = CONTENT_ROCK; break;
64     case 10: contents = CONTENT_RAIN; break;
65     case 11: contents = CONTENT_EXPLODE; break;
66     default:
67       log_warning << "Invalid box contents" << std::endl;
68       contents = CONTENT_COIN;
69       break;
70   }
71 }
72
73 BonusBlock::BonusBlock(const Reader& lisp) :
74   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
75   contents(),
76   object(0),
77   hit_counter(1),
78   lightsprite()
79 {
80   Vector pos;
81
82   contents = CONTENT_COIN;
83   lisp::ListIterator iter(&lisp);
84   while(iter.next()) {
85     const std::string& token = iter.item();
86     if(token == "x") {
87       iter.value()->get(pos.x);
88     } else if(token == "y") {
89       iter.value()->get(pos.y);
90     } else if(token == "sprite") {
91       iter.value()->get(sprite_name);
92       sprite = sprite_manager->create(sprite_name);
93     } else if(token == "count") {
94       iter.value()->get(hit_counter);
95     } else if(token == "script") { // use when bonusblock is to contain ONLY a script
96       iter.value()->get(script);
97     } else if(token == "contents") {
98       std::string contentstring;
99       iter.value()->get(contentstring);
100       if(contentstring == "coin") {
101         contents = CONTENT_COIN;
102       } else if(contentstring == "firegrow") {
103         contents = CONTENT_FIREGROW;
104       } else if(contentstring == "icegrow") {
105         contents = CONTENT_ICEGROW;
106       } else if(contentstring == "star") {
107         contents = CONTENT_STAR;
108       } else if(contentstring == "1up") {
109         contents = CONTENT_1UP;
110       } else if(contentstring == "custom") {
111         contents = CONTENT_CUSTOM;
112       } else if(contentstring == "script") {
113         contents = CONTENT_SCRIPT;
114       } else if(contentstring == "light") {
115         contents = CONTENT_LIGHT;
116         sound_manager->preload("sounds/switch.ogg");
117       } else if(contentstring == "trampoline") {
118         contents = CONTENT_TRAMPOLINE;
119       } else if(contentstring == "porttrampoline") {
120         contents = CONTENT_PORTTRAMPOLINE;
121       } else if(contentstring == "rock") {
122         contents = CONTENT_ROCK;
123       } else if(contentstring == "rain") {
124         contents = CONTENT_RAIN;
125       } else if(contentstring == "explode") {
126         contents = CONTENT_EXPLODE;
127       } else {
128         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
129       }
130     } else {
131       if(contents == CONTENT_CUSTOM) {
132         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
133         object = dynamic_cast<MovingObject*> (game_object);
134         if(object == 0)
135           throw std::runtime_error(
136             "Only MovingObjects are allowed inside BonusBlocks");
137       } else {
138         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
139       }
140     }
141   }
142
143   if(contents == CONTENT_CUSTOM && object == 0)
144     throw std::runtime_error("Need to specify content object for custom block");
145   if(contents == CONTENT_LIGHT)
146     lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
147
148   bbox.set_pos(pos);
149 }
150
151 BonusBlock::~BonusBlock()
152 {
153   delete object;
154 }
155
156 void
157 BonusBlock::hit(Player & player)
158 {
159   try_open(&player);
160 }
161
162 HitResponse
163 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
164
165   Player* player = dynamic_cast<Player*> (&other);
166   if (player) {
167     if (player->does_buttjump)
168       try_open(player);
169   }
170
171   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
172   if(badguy) {
173     // hit contains no information for collisions with blocks.
174     // Badguy's bottom has to be below the top of the block
175     // SHIFT_DELTA is required to slide over one tile gaps.
176     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
177       try_open(player);
178     }
179   }
180   Portable* portable = dynamic_cast<Portable*> (&other);
181   if(portable) {
182     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
183     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
184       try_open(player);
185     }
186   }
187   return Block::collision(other, hit);
188 }
189
190 void
191 BonusBlock::try_open(Player *player)
192 {
193   if(sprite->get_action() == "empty") {
194     sound_manager->play("sounds/brick.wav");
195     return;
196   }
197
198   Sector* sector = Sector::current();
199   assert(sector);
200
201   if (player == NULL)
202     player = sector->player;
203   
204   if (player == NULL)
205     return;
206
207   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
208
209   switch(contents) {
210     case CONTENT_COIN:
211     {
212       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
213       player->get_status()->add_coins(1);
214       if (hit_counter != 0)
215         Sector::current()->get_level()->stats.coins++;
216       break;
217     }
218
219     case CONTENT_FIREGROW:
220     {
221       if(player->get_status()->bonus == NO_BONUS) {
222         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
223         sector->add_object(riser);
224       } else {
225         SpecialRiser* riser = new SpecialRiser(
226           get_pos(), new Flower(FIRE_BONUS));
227         sector->add_object(riser);
228       }
229       sound_manager->play("sounds/upgrade.wav");
230       break;
231     }
232
233     case CONTENT_ICEGROW:
234     {
235       if(player->get_status()->bonus == NO_BONUS) {
236         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
237         sector->add_object(riser);
238       } else {
239         SpecialRiser* riser = new SpecialRiser(
240           get_pos(), new Flower(ICE_BONUS));
241         sector->add_object(riser);
242       }
243       sound_manager->play("sounds/upgrade.wav");
244       break;
245     }
246
247     case CONTENT_STAR:
248     {
249       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
250       break;
251     }
252
253     case CONTENT_1UP:
254     {
255       sector->add_object(new OneUp(get_pos(), direction));
256       break;
257     }
258
259     case CONTENT_CUSTOM:
260     {
261       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
262       object = 0;
263       sector->add_object(riser);
264       sound_manager->play("sounds/upgrade.wav");
265       break;
266     }
267
268     case CONTENT_SCRIPT:
269     { break; } // because scripts always run, this prevents default contents from being assumed
270
271     case CONTENT_LIGHT:
272     {
273       if(sprite->get_action() == "on")
274         sprite->set_action("off");
275       else
276         sprite->set_action("on");
277       sound_manager->play("sounds/switch.ogg");
278       break;
279     }
280     case CONTENT_TRAMPOLINE:
281     {
282       SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
283       sector->add_object(riser);
284       sound_manager->play("sounds/upgrade.wav");
285       break;
286     }
287     case CONTENT_PORTTRAMPOLINE:
288     {
289       SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), true));
290       sector->add_object(riser);
291       sound_manager->play("sounds/upgrade.wav");
292       break;
293     }
294     case CONTENT_ROCK:
295     {
296       SpecialRiser* riser = new SpecialRiser(get_pos(), 
297         new Rock(get_pos(), "images/objects/rock/rock.sprite"));
298       sector->add_object(riser);
299       sound_manager->play("sounds/upgrade.wav");
300       break;
301     }
302
303     case CONTENT_RAIN:
304     {
305       hit_counter = 1; // multiple hits of coin rain is not allowed
306       Sector::current()->add_object(new CoinRain(get_pos(), true));
307       sound_manager->play("sounds/upgrade.wav");
308       break;
309     }
310     case CONTENT_EXPLODE:
311     {
312       hit_counter = 1; // multiple hits of coin explode is not allowed
313       Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40), 1));
314       sound_manager->play("sounds/upgrade.wav");
315       break;
316     }
317   }
318
319   if(script != "") { // scripts always run if defined
320     std::istringstream stream(script);
321     Sector::current()->run_script(stream, "powerup-script");
322   }
323
324   start_bounce(player);
325   if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
326   }else if(hit_counter == 1){
327     sprite->set_action("empty");
328   }else{
329     hit_counter--;
330   }
331 }
332
333 void
334 Block::break_me()
335 {
336   Sector* sector = Sector::current();
337   sector->add_object(
338     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
339   sector->add_object(
340     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
341                     Vector(-150, -300)));
342   sector->add_object(
343     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
344                     Vector(100, -400)));
345   sector->add_object(
346     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
347                     Vector(150, -300)));
348   remove_me();
349 }
350
351 void
352 BonusBlock::draw(DrawingContext& context){
353   // draw regular sprite
354   sprite->draw(context, get_pos(), 10);
355   //Draw light if on.
356   if(sprite->get_action() == "on") {
357     Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
358     context.push_target();
359     context.set_target(DrawingContext::LIGHTMAP);
360     context.draw_surface(lightsprite, pos, 10);
361     context.pop_target();
362   }
363 }
364 /* EOF */