New bonus_block content: light
[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 "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"
36
37 #include <stdexcept>
38
39 BonusBlock::BonusBlock(const Vector& pos, int data) :
40   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
41   contents(),
42   object(0),
43   hit_counter(1),
44   lightsprite(Surface::create("/images/objects/lightmap_light/bonusblock_light.png"))
45 {
46   bbox.set_pos(pos);
47   sprite->set_action("normal");
48   switch(data) {
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;
55     default:
56       log_warning << "Invalid box contents" << std::endl;
57       contents = CONTENT_COIN;
58       break;
59   }
60 }
61
62 BonusBlock::BonusBlock(const Reader& lisp) :
63   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
64   contents(),
65   object(0),
66   hit_counter(1),
67   lightsprite(Surface::create("/images/objects/lightmap_light/bonusblock_light.png"))
68 {
69   Vector pos;
70
71   contents = CONTENT_COIN;
72   lisp::ListIterator iter(&lisp);
73   while(iter.next()) {
74     const std::string& token = iter.item();
75     if(token == "x") {
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");
106       } else {
107         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
108       }
109     } else {
110       if(contents == CONTENT_CUSTOM) {
111         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
112         object = dynamic_cast<MovingObject*> (game_object);
113         if(object == 0)
114           throw std::runtime_error(
115             "Only MovingObjects are allowed inside BonusBlocks");
116       } else {
117         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
118       }
119     }
120   }
121
122   if(contents == CONTENT_CUSTOM && object == 0)
123     throw std::runtime_error("Need to specify content object for custom block");
124
125   bbox.set_pos(pos);
126 }
127
128 BonusBlock::~BonusBlock()
129 {
130   delete object;
131 }
132
133 void
134 BonusBlock::hit(Player & player)
135 {
136   try_open(&player);
137 }
138
139 HitResponse
140 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
141
142   Player* player = dynamic_cast<Player*> (&other);
143   if (player) {
144     if (player->does_buttjump)
145       try_open(player);
146   }
147
148   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
149   if(badguy) {
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 ) ){
154       try_open(player);
155     }
156   }
157   Portable* portable = dynamic_cast<Portable*> (&other);
158   if(portable) {
159     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
160     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
161       try_open(player);
162     }
163   }
164   return Block::collision(other, hit);
165 }
166
167 void
168 BonusBlock::try_open(Player *player)
169 {
170   if(sprite->get_action() == "empty") {
171     sound_manager->play("sounds/brick.wav");
172     return;
173   }
174
175   Sector* sector = Sector::current();
176   assert(sector);
177
178   if (player == NULL)
179     player = sector->player;
180   
181   if (player == NULL)
182     return;
183
184   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
185
186   switch(contents) {
187     case CONTENT_COIN:
188     {
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++;
193       break;
194     }
195
196     case CONTENT_FIREGROW:
197     {
198       if(player->get_status()->bonus == NO_BONUS) {
199         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
200         sector->add_object(riser);
201       } else {
202         SpecialRiser* riser = new SpecialRiser(
203           get_pos(), new Flower(FIRE_BONUS));
204         sector->add_object(riser);
205       }
206       sound_manager->play("sounds/upgrade.wav");
207       break;
208     }
209
210     case CONTENT_ICEGROW:
211     {
212       if(player->get_status()->bonus == NO_BONUS) {
213         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
214         sector->add_object(riser);
215       } else {
216         SpecialRiser* riser = new SpecialRiser(
217           get_pos(), new Flower(ICE_BONUS));
218         sector->add_object(riser);
219       }
220       sound_manager->play("sounds/upgrade.wav");
221       break;
222     }
223
224     case CONTENT_STAR:
225     {
226       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
227       break;
228     }
229
230     case CONTENT_1UP:
231     {
232       sector->add_object(new OneUp(get_pos(), direction));
233       break;
234     }
235
236     case CONTENT_CUSTOM:
237     {
238       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
239       object = 0;
240       sector->add_object(riser);
241       sound_manager->play("sounds/upgrade.wav");
242       break;
243     }
244
245     case CONTENT_SCRIPT:
246     {
247       if(script != "") {
248         std::istringstream stream(script);
249         Sector::current()->run_script(stream, "powerup-script");
250       }
251       break;
252     }
253     case CONTENT_LIGHT:
254     {
255       if(sprite->get_action() == "on")
256         sprite->set_action("off");
257       else
258         sprite->set_action("on");
259       sound_manager->play("sounds/switch.ogg");
260     }
261   }
262
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");
267   }else{
268     hit_counter--;
269   }
270 }
271
272 void
273 Block::break_me()
274 {
275   Sector* sector = Sector::current();
276   sector->add_object(
277     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
278   sector->add_object(
279     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
280                     Vector(-150, -300)));
281   sector->add_object(
282     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
283                     Vector(100, -400)));
284   sector->add_object(
285     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
286                     Vector(150, -300)));
287   remove_me();
288 }
289
290 void
291 BonusBlock::draw(DrawingContext& context){
292   // draw regular sprite
293   sprite->draw(context, get_pos(), 10);
294   //Draw light if on.
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();
301   }
302 }
303 /* EOF */