8b53826ec73efc51e87643bdd3e72d928d8eae55
[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/powerup.hpp"
32 #include "object/specialriser.hpp"
33 #include "object/star.hpp"
34 #include "object/trampoline.hpp"
35 #include "sprite/sprite_manager.hpp"
36 #include "supertux/constants.hpp"
37 #include "supertux/level.hpp"
38 #include "supertux/object_factory.hpp"
39 #include "supertux/sector.hpp"
40
41 #include <stdexcept>
42
43 BonusBlock::BonusBlock(const Vector& pos, int data) :
44   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
45   contents(),
46   object(0),
47   hit_counter(1),
48   sprite_name(),
49   script(),
50   lightsprite()
51 {
52   bbox.set_pos(pos);
53   sprite->set_action("normal");
54   switch(data) {
55     case 1: contents = CONTENT_COIN; break;
56     case 2: contents = CONTENT_FIREGROW; break;
57     case 3: contents = CONTENT_STAR; break;
58     case 4: contents = CONTENT_1UP; break;
59     case 5: contents = CONTENT_ICEGROW; break;
60     case 6: contents = CONTENT_LIGHT;
61       sound_manager->preload("sounds/switch.ogg");
62       lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
63       break;
64     case 7: contents = CONTENT_TRAMPOLINE;
65       //object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
66       break;
67     case 8: contents = CONTENT_CUSTOM;
68       object = new Trampoline(get_pos(), true);
69       break;
70     case 9: contents = CONTENT_CUSTOM;
71       object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
72       break;
73     case 10: contents = CONTENT_RAIN; break;
74     case 11: contents = CONTENT_EXPLODE; break;
75     case 12: contents = CONTENT_CUSTOM;
76       object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
77       break;
78     default:
79       log_warning << "Invalid box contents" << std::endl;
80       contents = CONTENT_COIN;
81       break;
82   }
83 }
84
85 BonusBlock::BonusBlock(const Reader& lisp) :
86   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
87   contents(),
88   object(0),
89   hit_counter(1),
90   sprite_name(),
91   script(),
92   lightsprite()
93 {
94   Vector pos;
95
96   contents = CONTENT_COIN;
97   lisp::ListIterator iter(&lisp);
98   while(iter.next()) {
99     const std::string& token = iter.item();
100     if(token == "x") {
101       iter.value()->get(pos.x);
102     } else if(token == "y") {
103       iter.value()->get(pos.y);
104     } else if(token == "sprite") {
105       iter.value()->get(sprite_name);
106       sprite = sprite_manager->create(sprite_name);
107     } else if(token == "count") {
108       iter.value()->get(hit_counter);
109     } else if(token == "script") {
110       iter.value()->get(script);
111     } else if(token == "contents") {
112       std::string contentstring;
113       iter.value()->get(contentstring);
114       if(contentstring == "coin") {
115         contents = CONTENT_COIN;
116       } else if(contentstring == "firegrow") {
117         contents = CONTENT_FIREGROW;
118       } else if(contentstring == "icegrow") {
119         contents = CONTENT_ICEGROW;
120       } else if(contentstring == "star") {
121         contents = CONTENT_STAR;
122       } else if(contentstring == "1up") {
123         contents = CONTENT_1UP;
124       } else if(contentstring == "custom") {
125         contents = CONTENT_CUSTOM;
126       } else if(contentstring == "script") { // use when bonusblock is to contain ONLY a script
127         contents = CONTENT_SCRIPT;
128       } else if(contentstring == "light") {
129         contents = CONTENT_LIGHT;
130         sound_manager->preload("sounds/switch.ogg");
131       } else if(contentstring == "trampoline") {
132         contents = CONTENT_TRAMPOLINE;
133       } else if(contentstring == "rain") {
134         contents = CONTENT_RAIN;
135       } else if(contentstring == "explode") {
136         contents = CONTENT_EXPLODE;
137       } else {
138         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
139       }
140     } else {
141       if(contents == CONTENT_CUSTOM) {
142         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
143         object = dynamic_cast<MovingObject*> (game_object);
144         if(object == 0)
145           throw std::runtime_error(
146             "Only MovingObjects are allowed inside BonusBlocks");
147       } else {
148         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
149       }
150     }
151   }
152
153   if(contents == CONTENT_CUSTOM && object == 0)
154     throw std::runtime_error("Need to specify content object for custom block");
155   if(contents == CONTENT_LIGHT)
156     lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
157
158   bbox.set_pos(pos);
159 }
160
161 BonusBlock::~BonusBlock()
162 {
163   delete object;
164 }
165
166 void
167 BonusBlock::hit(Player & player)
168 {
169   try_open(&player);
170 }
171
172 HitResponse
173 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
174
175   Player* player = dynamic_cast<Player*> (&other);
176   if (player) {
177     if (player->does_buttjump)
178       try_drop(player);
179   }
180
181   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
182   if(badguy) {
183     // hit contains no information for collisions with blocks.
184     // Badguy's bottom has to be below the top of the block
185     // SHIFT_DELTA is required to slide over one tile gaps.
186     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
187       try_open(player);
188     }
189   }
190   Portable* portable = dynamic_cast<Portable*> (&other);
191   if(portable) {
192     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
193     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
194       try_open(player);
195     }
196   }
197   return Block::collision(other, hit);
198 }
199
200 void
201 BonusBlock::try_open(Player *player)
202 {
203   if(sprite->get_action() == "empty") {
204     sound_manager->play("sounds/brick.wav");
205     return;
206   }
207
208   Sector* sector = Sector::current();
209   assert(sector);
210
211   if (player == NULL)
212     player = sector->player;
213
214   if (player == NULL)
215     return;
216
217   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
218
219   switch(contents) {
220     case CONTENT_COIN:
221     {
222       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
223       player->get_status()->add_coins(1);
224       if (hit_counter != 0)
225         Sector::current()->get_level()->stats.coins++;
226       break;
227     }
228
229     case CONTENT_FIREGROW:
230     {
231       if(player->get_status()->bonus == NO_BONUS) {
232         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
233         sector->add_object(riser);
234       } else {
235         SpecialRiser* riser = new SpecialRiser(
236           get_pos(), new Flower(FIRE_BONUS));
237         sector->add_object(riser);
238       }
239       sound_manager->play("sounds/upgrade.wav");
240       break;
241     }
242
243     case CONTENT_ICEGROW:
244     {
245       if(player->get_status()->bonus == NO_BONUS) {
246         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
247         sector->add_object(riser);
248       } else {
249         SpecialRiser* riser = new SpecialRiser(
250           get_pos(), new Flower(ICE_BONUS));
251         sector->add_object(riser);
252       }
253       sound_manager->play("sounds/upgrade.wav");
254       break;
255     }
256
257     case CONTENT_STAR:
258     {
259       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
260       sound_manager->play("sounds/upgrade.wav");
261       break;
262     }
263
264     case CONTENT_1UP:
265     {
266       sector->add_object(new OneUp(get_pos(), direction));
267       sound_manager->play("sounds/upgrade.wav");
268       break;
269     }
270
271     case CONTENT_CUSTOM:
272     {
273       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
274       object = 0;
275       sector->add_object(riser);
276       sound_manager->play("sounds/upgrade.wav");
277       break;
278     }
279
280     case CONTENT_SCRIPT:
281     { break; } // because scripts always run, this prevents default contents from being assumed
282
283     case CONTENT_LIGHT:
284     {
285       if(sprite->get_action() == "on")
286         sprite->set_action("off");
287       else
288         sprite->set_action("on");
289       sound_manager->play("sounds/switch.ogg");
290       break;
291     }
292     case CONTENT_TRAMPOLINE:
293     {
294       SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
295       sector->add_object(riser);
296       sound_manager->play("sounds/upgrade.wav");
297       break;
298     }
299     case CONTENT_RAIN:
300     {
301       hit_counter = 1; // multiple hits of coin rain is not allowed
302       Sector::current()->add_object(new CoinRain(get_pos(), true));
303       sound_manager->play("sounds/upgrade.wav");
304       break;
305     }
306     case CONTENT_EXPLODE:
307     {
308       hit_counter = 1; // multiple hits of coin explode is not allowed
309       Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
310       sound_manager->play("sounds/upgrade.wav");
311       break;
312     }
313   }
314
315   if(script != "") { // scripts always run if defined
316     std::istringstream stream(script);
317     Sector::current()->run_script(stream, "BonusBlockScript");
318   }
319
320   start_bounce(player);
321   if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
322   }else if(hit_counter == 1){
323     sprite->set_action("empty");
324   }else{
325     hit_counter--;
326   }
327 }
328
329 void
330 BonusBlock::try_drop(Player *player)
331 {
332   if(sprite->get_action() == "empty") {
333     sound_manager->play("sounds/brick.wav");
334     return;
335   }
336
337   Sector* sector = Sector::current();
338   assert(sector);
339
340   // First what's below the bonus block, if solid send it up anyway (excepting doll)
341   Rectf dest;
342   dest.p1.x = bbox.get_left() + 1;
343   dest.p1.y = bbox.get_bottom() + 1;
344   dest.p2.x = bbox.get_right() - 1;
345   dest.p2.y = dest.p1.y + 30;
346   if (!Sector::current()->is_free_of_statics(dest, this, true) && !(contents == CONTENT_1UP)) {
347     try_open(player);
348     return;
349   }
350
351   if (player == NULL)
352     player = sector->player;
353
354   if (player == NULL)
355     return;
356
357   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
358
359   bool countdown = false;
360
361   switch(contents) {
362     case CONTENT_COIN:
363     {
364       try_open(player);
365       break;
366     }
367
368     case CONTENT_FIREGROW:
369     {
370       sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
371       sound_manager->play("sounds/upgrade.wav");
372       countdown = true;
373       break;
374     }
375
376     case CONTENT_ICEGROW:
377     {
378       sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
379       sound_manager->play("sounds/upgrade.wav");
380       countdown = true;
381       break;
382     }
383
384     case CONTENT_STAR:
385     {
386       sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
387       sound_manager->play("sounds/upgrade.wav");
388       countdown = true;
389       break;
390     }
391
392     case CONTENT_1UP:
393     {
394       sector->add_object(new OneUp(get_pos(), DOWN));
395       sound_manager->play("sounds/upgrade.wav");
396       countdown = true;
397       break;
398     }
399
400     case CONTENT_CUSTOM:
401     {
402       //NOTE: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
403       object->set_pos(get_pos() +  Vector(0, 32));
404       sector->add_object(object);
405       object = 0;
406       sound_manager->play("sounds/upgrade.wav");
407       countdown = true;
408       break;
409     }
410
411     case CONTENT_SCRIPT:
412     { break; } // because scripts always run, this prevents default contents from being assumed
413
414     case CONTENT_LIGHT:
415     {
416       try_open(player);
417       break;
418     }
419     case CONTENT_TRAMPOLINE:
420     {
421       try_open(player);
422       break;
423     }
424     case CONTENT_RAIN:
425     {
426       try_open(player);
427       break;
428     }
429     case CONTENT_EXPLODE:
430     {
431       hit_counter = 1; // multiple hits of coin explode is not allowed
432       Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
433       sound_manager->play("sounds/upgrade.wav");
434       countdown = true;
435       break;
436     }
437   }
438
439   if(script != "") { // scripts always run if defined
440     std::istringstream stream(script);
441     Sector::current()->run_script(stream, "powerup-script");
442   }
443
444   if(countdown){ // only decrease hit counter if try_open was not called
445     if(hit_counter == 1){
446       sprite->set_action("empty");
447     }else{
448       hit_counter--;
449     }
450   }
451 }
452
453 void
454 Block::break_me()
455 {
456   Sector* sector = Sector::current();
457   sector->add_object(
458     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
459   sector->add_object(
460     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
461                     Vector(-150, -300)));
462   sector->add_object(
463     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
464                     Vector(100, -400)));
465   sector->add_object(
466     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
467                     Vector(150, -300)));
468   remove_me();
469 }
470
471 void
472 BonusBlock::draw(DrawingContext& context){
473   // do the regular drawing first
474   Block::draw(context);
475   // then Draw the light if on.
476   if(sprite->get_action() == "on") {
477     Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
478     context.push_target();
479     context.set_target(DrawingContext::LIGHTMAP);
480     context.draw_surface(lightsprite, pos, 10);
481     context.pop_target();
482   }
483 }
484 /* EOF */