* Coins inside boxes are now being counted. (We'll have to decide if we want the...
[supertux.git] / src / object / block.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "block.hpp"
23 #include "log.hpp"
24
25 #include <stdexcept>
26
27 #include "resources.hpp"
28 #include "player.hpp"
29 #include "sector.hpp"
30 #include "sprite/sprite.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "video/drawing_context.hpp"
33 #include "lisp/lisp.hpp"
34 #include "gameobjs.hpp"
35 #include "specialriser.hpp"
36 #include "growup.hpp"
37 #include "flower.hpp"
38 #include "oneup.hpp"
39 #include "star.hpp"
40 #include "player_status.hpp"
41 #include "badguy/badguy.hpp"
42 #include "coin.hpp"
43 #include "object_factory.hpp"
44 #include "lisp/list_iterator.hpp"
45 #include "object_factory.hpp"
46 #include "level.hpp"
47
48 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
49 static const float BOUNCY_BRICK_SPEED = 90;
50 static const float EPSILON = .0001f;
51
52 Block::Block(Sprite* newsprite)
53   : sprite(newsprite), bouncing(false), breaking(false), bounce_dir(0), bounce_offset(0)
54 {
55   bbox.set_size(32, 32.1f);
56   set_group(COLGROUP_STATIC);
57   sound_manager->preload("sounds/upgrade.wav");
58   sound_manager->preload("sounds/brick.wav");
59 }
60
61 Block::~Block()
62 {
63   delete sprite;
64 }
65
66 HitResponse
67 Block::collision(GameObject& other, const CollisionHit& )
68 {
69   Player* player = dynamic_cast<Player*> (&other);
70   if(player) {
71     if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
72       hit(*player);
73     }
74   }
75
76   if(bouncing) {
77     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
78     if(badguy) {
79       badguy->kill_fall();
80     }
81     Coin* coin = dynamic_cast<Coin*> (&other);
82     if(coin) {
83       coin->collect();
84     }
85   }
86
87   return SOLID;
88 }
89
90 void
91 Block::update(float elapsed_time)
92 {
93   if(!bouncing)
94     return;
95
96   float offset = original_y - get_pos().y;
97   if(offset > BOUNCY_BRICK_MAX_OFFSET) {
98     bounce_dir = BOUNCY_BRICK_SPEED;
99     movement = Vector(0, bounce_dir * elapsed_time);
100     if(breaking){
101       break_me();
102     }
103   } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
104     movement = Vector(0, offset);
105     bounce_dir = 0;
106     bouncing = false;
107   } else {
108     movement = Vector(0, bounce_dir * elapsed_time);
109   }
110 }
111
112 void
113 Block::draw(DrawingContext& context)
114 {
115   sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
116 }
117
118 void
119 Block::start_bounce()
120 {
121   original_y = bbox.p1.y;
122   bouncing = true;
123   bounce_dir = -BOUNCY_BRICK_SPEED;
124   bounce_offset = 0;
125 }
126
127 void
128 Block::start_break()
129 {
130   start_bounce();
131   breaking = true;
132 }
133
134 //---------------------------------------------------------------------------
135
136 BonusBlock::BonusBlock(const Vector& pos, int data)
137   : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
138 {
139   bbox.set_pos(pos);
140   sprite->set_action("normal");
141   switch(data) {
142     case 1: contents = CONTENT_COIN; break;
143     case 2: contents = CONTENT_FIREGROW; break;
144     case 3: contents = CONTENT_STAR; break;
145     case 4: contents = CONTENT_1UP; break;
146     case 5: contents = CONTENT_ICEGROW; break;
147     default:
148       log_warning << "Invalid box contents" << std::endl;
149       contents = CONTENT_COIN;
150       break;
151   }
152 }
153
154 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
155   : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
156 {
157   Vector pos;
158
159   contents = CONTENT_COIN;
160   lisp::ListIterator iter(&lisp);
161   while(iter.next()) {
162     const std::string& token = iter.item();
163     if(token == "x") {
164       iter.value()->get(pos.x);
165     } else if(token == "y") {
166       iter.value()->get(pos.y);
167     } else if(token == "contents") {
168       std::string contentstring;
169       iter.value()->get(contentstring);
170       if(contentstring == "coin") {
171         contents = CONTENT_COIN;
172       } else if(contentstring == "firegrow") {
173         contents = CONTENT_FIREGROW;
174       } else if(contentstring == "icegrow") {
175         contents = CONTENT_ICEGROW;
176       } else if(contentstring == "star") {
177         contents = CONTENT_STAR;
178       } else if(contentstring == "1up") {
179         contents = CONTENT_1UP;
180       } else if(contentstring == "custom") {
181         contents = CONTENT_CUSTOM;
182       } else {
183         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
184       }
185     } else {
186       if(contents == CONTENT_CUSTOM) {
187         GameObject* game_object = create_object(token, *(iter.lisp()));
188         object = dynamic_cast<MovingObject*> (game_object);
189         if(object == 0)
190           throw std::runtime_error(
191             "Only MovingObjects are allowed inside BonusBlocks");
192       } else {
193         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
194       }
195     }
196   }
197
198   if(contents == CONTENT_CUSTOM && object == 0)
199     throw std::runtime_error("Need to specify content object for custom block");
200
201   bbox.set_pos(pos);
202 }
203
204 BonusBlock::~BonusBlock()
205 {
206   delete object;
207 }
208
209 void
210 BonusBlock::hit(Player& )
211 {
212   try_open();
213 }
214
215 HitResponse
216 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
217     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
218     if(badguy) {
219       // hit contains no information for collisions with blocks.
220       // Badguy's bottom has to be below the top of the bonusblock
221       // +7 is required to slide over one tile gaps.
222       if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
223         try_open();
224       }
225     }
226     return Block::collision(other, hit);
227 }
228
229 void
230 BonusBlock::try_open()
231 {
232   if(sprite->get_action() == "empty") {
233     sound_manager->play("sounds/brick.wav");
234     return;
235   }
236
237   Sector* sector = Sector::current();
238   assert(sector);
239   assert(sector->player);
240   Player& player = *(sector->player);
241   Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
242
243   switch(contents) {
244     case CONTENT_COIN:
245       Sector::current()->add_object(new BouncyCoin(get_pos()));
246       player.get_status()->add_coins(1);
247       Sector::current()->get_level()->stats.coins++;
248       break;
249
250     case CONTENT_FIREGROW:
251       if(player.get_status()->bonus == NO_BONUS) {
252         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
253         sector->add_object(riser);
254       } else {
255         SpecialRiser* riser = new SpecialRiser(
256             get_pos(), new Flower(FIRE_BONUS));
257         sector->add_object(riser);
258       }
259       sound_manager->play("sounds/upgrade.wav");
260       break;
261
262     case CONTENT_ICEGROW:
263       if(player.get_status()->bonus == NO_BONUS) {
264         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
265         sector->add_object(riser);
266       } else {
267         SpecialRiser* riser = new SpecialRiser(
268             get_pos(), new Flower(ICE_BONUS));
269         sector->add_object(riser);
270       }
271       sound_manager->play("sounds/upgrade.wav");
272       break;
273
274     case CONTENT_STAR:
275       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
276       break;
277
278     case CONTENT_1UP:
279       sector->add_object(new OneUp(get_pos(), direction));
280       break;
281
282     case CONTENT_CUSTOM:
283       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
284       object = 0;
285       sector->add_object(riser);
286       sound_manager->play("sounds/upgrade.wav");
287       break;
288   }
289
290   start_bounce();
291   sprite->set_action("empty");
292 }
293
294 void
295 Block::break_me()
296 {
297   Sector* sector = Sector::current();
298   sector->add_object(
299       new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
300   sector->add_object(
301       new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
302         Vector(-150, -300)));
303   sector->add_object(
304       new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
305         Vector(100, -400)));
306   sector->add_object(
307       new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
308         Vector(150, -300)));
309   remove_me();
310 }
311
312 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
313
314 //---------------------------------------------------------------------------
315
316 Brick::Brick(const Vector& pos, int data)
317   : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
318     coin_counter(0)
319 {
320   bbox.set_pos(pos);
321   if(data == 1)
322     coin_counter = 5;
323   else
324     breakable = true;
325 }
326
327 void
328 Brick::hit(Player& )
329 {
330   if(sprite->get_action() == "empty")
331     return;
332
333   try_break(true);
334 }
335
336 HitResponse
337 Brick::collision(GameObject& other, const CollisionHit& hit){
338     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
339     if(badguy) {
340       // hit contains no information for collisions with blocks.
341       // Badguy's bottom has to be below the top of the brick
342       // +7 is required to slide over one tile gaps.
343       if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
344         try_break(false);
345       }
346     }
347    return Block::collision(other, hit);
348 }
349
350 void
351 Brick::try_break(bool playerhit)
352 {
353   if(sprite->get_action() == "empty")
354     return;
355
356   sound_manager->play("sounds/brick.wav");
357   Sector* sector = Sector::current();
358   Player& player = *(sector->player);
359   if(coin_counter > 0) {
360     sector->add_object(new BouncyCoin(get_pos()));
361     coin_counter--;
362     player.get_status()->add_coins(1);
363     if(coin_counter == 0)
364       sprite->set_action("empty");
365     start_bounce();
366   } else if(breakable) {
367     if(playerhit){
368       if(player.is_big()){
369         start_break();
370         return;
371       } else {
372         start_bounce();
373         return;
374       }
375     }
376    break_me();
377   }
378 }
379
380 //IMPLEMENT_FACTORY(Brick, "brick");