New bonus_block contnet: trampolines & rock
[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/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"
37
38 #include <stdexcept>
39
40 BonusBlock::BonusBlock(const Vector& pos, int data) :
41   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
42   contents(),
43   object(0),
44   hit_counter(1),
45   lightsprite()
46 {
47   bbox.set_pos(pos);
48   sprite->set_action("normal");
49   switch(data) {
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");
58       break;
59     case 7: contents = CONTENT_TRAMPOLINE; break;
60     case 8: contents = CONTENT_PORTTRAMPOLINE; break;
61     case 9: contents = CONTENT_ROCK; break;
62     default:
63       log_warning << "Invalid box contents" << std::endl;
64       contents = CONTENT_COIN;
65       break;
66   }
67 }
68
69 BonusBlock::BonusBlock(const Reader& lisp) :
70   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
71   contents(),
72   object(0),
73   hit_counter(1),
74   lightsprite()
75 {
76   Vector pos;
77
78   contents = CONTENT_COIN;
79   lisp::ListIterator iter(&lisp);
80   while(iter.next()) {
81     const std::string& token = iter.item();
82     if(token == "x") {
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;
119       } else {
120         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
121       }
122     } else {
123       if(contents == CONTENT_CUSTOM) {
124         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
125         object = dynamic_cast<MovingObject*> (game_object);
126         if(object == 0)
127           throw std::runtime_error(
128             "Only MovingObjects are allowed inside BonusBlocks");
129       } else {
130         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
131       }
132     }
133   }
134
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");
139
140   bbox.set_pos(pos);
141 }
142
143 BonusBlock::~BonusBlock()
144 {
145   delete object;
146 }
147
148 void
149 BonusBlock::hit(Player & player)
150 {
151   try_open(&player);
152 }
153
154 HitResponse
155 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
156
157   Player* player = dynamic_cast<Player*> (&other);
158   if (player) {
159     if (player->does_buttjump)
160       try_open(player);
161   }
162
163   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
164   if(badguy) {
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 ) ){
169       try_open(player);
170     }
171   }
172   Portable* portable = dynamic_cast<Portable*> (&other);
173   if(portable) {
174     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
175     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
176       try_open(player);
177     }
178   }
179   return Block::collision(other, hit);
180 }
181
182 void
183 BonusBlock::try_open(Player *player)
184 {
185   if(sprite->get_action() == "empty") {
186     sound_manager->play("sounds/brick.wav");
187     return;
188   }
189
190   Sector* sector = Sector::current();
191   assert(sector);
192
193   if (player == NULL)
194     player = sector->player;
195   
196   if (player == NULL)
197     return;
198
199   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
200
201   switch(contents) {
202     case CONTENT_COIN:
203     {
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++;
208       break;
209     }
210
211     case CONTENT_FIREGROW:
212     {
213       if(player->get_status()->bonus == NO_BONUS) {
214         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
215         sector->add_object(riser);
216       } else {
217         SpecialRiser* riser = new SpecialRiser(
218           get_pos(), new Flower(FIRE_BONUS));
219         sector->add_object(riser);
220       }
221       sound_manager->play("sounds/upgrade.wav");
222       break;
223     }
224
225     case CONTENT_ICEGROW:
226     {
227       if(player->get_status()->bonus == NO_BONUS) {
228         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
229         sector->add_object(riser);
230       } else {
231         SpecialRiser* riser = new SpecialRiser(
232           get_pos(), new Flower(ICE_BONUS));
233         sector->add_object(riser);
234       }
235       sound_manager->play("sounds/upgrade.wav");
236       break;
237     }
238
239     case CONTENT_STAR:
240     {
241       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
242       break;
243     }
244
245     case CONTENT_1UP:
246     {
247       sector->add_object(new OneUp(get_pos(), direction));
248       break;
249     }
250
251     case CONTENT_CUSTOM:
252     {
253       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
254       object = 0;
255       sector->add_object(riser);
256       sound_manager->play("sounds/upgrade.wav");
257       break;
258     }
259
260     case CONTENT_SCRIPT:
261     {
262       if(script != "") {
263         std::istringstream stream(script);
264         Sector::current()->run_script(stream, "powerup-script");
265       }
266       break;
267     }
268     case CONTENT_LIGHT:
269     {
270       if(sprite->get_action() == "on")
271         sprite->set_action("off");
272       else
273         sprite->set_action("on");
274       sound_manager->play("sounds/switch.ogg");
275       break;
276     }
277     case CONTENT_TRAMPOLINE:
278     {
279       SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
280       sector->add_object(riser);
281       sound_manager->play("sounds/upgrade.wav");
282       break;
283     }
284     case CONTENT_PORTTRAMPOLINE:
285     {
286       SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), true));
287       sector->add_object(riser);
288       sound_manager->play("sounds/upgrade.wav");
289       break;
290     }
291     case CONTENT_ROCK:
292     {
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");
297       break;
298     }
299   }
300
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");
305   }else{
306     hit_counter--;
307   }
308 }
309
310 void
311 Block::break_me()
312 {
313   Sector* sector = Sector::current();
314   sector->add_object(
315     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
316   sector->add_object(
317     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
318                     Vector(-150, -300)));
319   sector->add_object(
320     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
321                     Vector(100, -400)));
322   sector->add_object(
323     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
324                     Vector(150, -300)));
325   remove_me();
326 }
327
328 void
329 BonusBlock::draw(DrawingContext& context){
330   // draw regular sprite
331   sprite->draw(context, get_pos(), 10);
332   //Draw light if on.
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();
339   }
340 }
341 /* EOF */