Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / block.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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/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/coin.hpp"
24 #include "object/flower.hpp"
25 #include "object/bouncy_coin.hpp"
26 #include "object/growup.hpp"
27 #include "object/oneup.hpp"
28 #include "object/player.hpp"
29 #include "object/portable.hpp"
30 #include "object/specialriser.hpp"
31 #include "object/star.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 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
39 static const float BOUNCY_BRICK_SPEED = 90;
40 static const float EPSILON = .0001f;
41 static const float BUMP_ROTATION_ANGLE = 10;
42
43 Block::Block(std::auto_ptr<Sprite> newsprite) :
44   sprite(newsprite), 
45   bouncing(false), 
46   breaking(false), 
47   bounce_dir(0), 
48   bounce_offset(0), 
49   original_y(-1)
50 {
51   bbox.set_size(32, 32.1f);
52   set_group(COLGROUP_STATIC);
53   sound_manager->preload("sounds/upgrade.wav");
54   sound_manager->preload("sounds/brick.wav");
55 }
56
57 Block::~Block()
58 {
59 }
60
61 HitResponse
62 Block::collision(GameObject& other, const CollisionHit& )
63 {
64   Player* player = dynamic_cast<Player*> (&other);
65   if(player) {
66     if(player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
67       hit(*player);
68     }
69   }
70
71   // only interact with other objects if...
72   //   1) we are bouncing
73   //   2) the object is not portable (either never or not currently)
74   //   3) the object is being hit from below (baguys don't get killed for activating boxes)
75   Portable* portable = dynamic_cast<Portable*> (&other);
76   MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
77   bool is_portable = ((portable != 0) && portable->is_portable());
78   bool hit_mo_from_below = ((moving_object == 0) || (moving_object->get_bbox().get_bottom() < (get_bbox().get_top() + SHIFT_DELTA)));
79   if(bouncing && !is_portable && hit_mo_from_below) {
80
81     // Badguys get killed
82     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
83     if(badguy) {
84       badguy->kill_fall();
85     }
86
87     // Coins get collected
88     Coin* coin = dynamic_cast<Coin*> (&other);
89     if(coin) {
90       coin->collect();
91     }
92     
93     //Eggs get jumped
94     GrowUp* growup = dynamic_cast<GrowUp*> (&other);
95     if(growup) {
96       growup->do_jump();
97     }
98
99   }
100
101   return SOLID;
102 }
103
104 void
105 Block::update(float elapsed_time)
106 {
107   if(!bouncing)
108     return;
109
110   float offset = original_y - get_pos().y;
111   if(offset > BOUNCY_BRICK_MAX_OFFSET) {
112     bounce_dir = BOUNCY_BRICK_SPEED;
113     movement = Vector(0, bounce_dir * elapsed_time);
114     if(breaking){
115       break_me();
116     }
117   } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
118     movement = Vector(0, offset);
119     bounce_dir = 0;
120     bouncing = false;
121     sprite->set_angle(0);
122   } else {
123     movement = Vector(0, bounce_dir * elapsed_time);
124   }
125 }
126
127 void
128 Block::draw(DrawingContext& context)
129 {
130   sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
131 }
132
133 void
134 Block::start_bounce(GameObject* hitter)
135 {
136   if(original_y == -1){
137     original_y = bbox.p1.y;
138   }
139   bouncing = true;
140   bounce_dir = -BOUNCY_BRICK_SPEED;
141   bounce_offset = 0;
142
143   MovingObject* hitter_mo = dynamic_cast<MovingObject*>(hitter);
144   if (hitter_mo) {
145     float center_of_hitter = hitter_mo->get_bbox().get_middle().x;
146     float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
147     sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
148   }
149 }
150
151 void
152 Block::start_break(GameObject* hitter)
153 {
154   start_bounce(hitter);
155   breaking = true;
156 }
157
158 //---------------------------------------------------------------------------
159
160 BonusBlock::BonusBlock(const Vector& pos, int data) :
161   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
162   object(0)
163 {
164   bbox.set_pos(pos);
165   sprite->set_action("normal");
166   switch(data) {
167     case 1: contents = CONTENT_COIN; break;
168     case 2: contents = CONTENT_FIREGROW; break;
169     case 3: contents = CONTENT_STAR; break;
170     case 4: contents = CONTENT_1UP; break;
171     case 5: contents = CONTENT_ICEGROW; break;
172     default:
173       log_warning << "Invalid box contents" << std::endl;
174       contents = CONTENT_COIN;
175       break;
176   }
177 }
178
179 BonusBlock::BonusBlock(const Reader& lisp) :
180   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
181 {
182   Vector pos;
183
184   contents = CONTENT_COIN;
185   lisp::ListIterator iter(&lisp);
186   while(iter.next()) {
187     const std::string& token = iter.item();
188     if(token == "x") {
189       iter.value()->get(pos.x);
190     } else if(token == "y") {
191       iter.value()->get(pos.y);
192     } else if(token == "contents") {
193       std::string contentstring;
194       iter.value()->get(contentstring);
195       if(contentstring == "coin") {
196         contents = CONTENT_COIN;
197       } else if(contentstring == "firegrow") {
198         contents = CONTENT_FIREGROW;
199       } else if(contentstring == "icegrow") {
200         contents = CONTENT_ICEGROW;
201       } else if(contentstring == "star") {
202         contents = CONTENT_STAR;
203       } else if(contentstring == "1up") {
204         contents = CONTENT_1UP;
205       } else if(contentstring == "custom") {
206         contents = CONTENT_CUSTOM;
207       } else {
208         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
209       }
210     } else {
211       if(contents == CONTENT_CUSTOM) {
212         GameObject* game_object = create_object(token, *(iter.lisp()));
213         object = dynamic_cast<MovingObject*> (game_object);
214         if(object == 0)
215           throw std::runtime_error(
216             "Only MovingObjects are allowed inside BonusBlocks");
217       } else {
218         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
219       }
220     }
221   }
222
223   if(contents == CONTENT_CUSTOM && object == 0)
224     throw std::runtime_error("Need to specify content object for custom block");
225
226   bbox.set_pos(pos);
227 }
228
229 BonusBlock::~BonusBlock()
230 {
231   delete object;
232 }
233
234 void
235 BonusBlock::hit(Player& )
236 {
237   try_open();
238 }
239
240 HitResponse
241 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
242
243   Player* player = dynamic_cast<Player*> (&other);
244   if (player) {
245     if (player->does_buttjump) try_open();
246   }
247
248   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
249   if(badguy) {
250     // hit contains no information for collisions with blocks.
251     // Badguy's bottom has to be below the top of the block
252     // SHIFT_DELTA is required to slide over one tile gaps.
253     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
254       try_open();
255     }
256   }
257   Portable* portable = dynamic_cast<Portable*> (&other);
258   if(portable) {
259     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
260     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
261       try_open();
262     }
263   }
264   return Block::collision(other, hit);
265 }
266
267 void
268 BonusBlock::try_open()
269 {
270   if(sprite->get_action() == "empty") {
271     sound_manager->play("sounds/brick.wav");
272     return;
273   }
274
275   Sector* sector = Sector::current();
276   assert(sector);
277   assert(sector->player);
278   Player& player = *(sector->player);
279   Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
280
281   switch(contents) {
282     case CONTENT_COIN:
283       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
284       player.get_status()->add_coins(1);
285       Sector::current()->get_level()->stats.coins++;
286       break;
287
288     case CONTENT_FIREGROW:
289       if(player.get_status()->bonus == NO_BONUS) {
290         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
291         sector->add_object(riser);
292       } else {
293         SpecialRiser* riser = new SpecialRiser(
294           get_pos(), new Flower(FIRE_BONUS));
295         sector->add_object(riser);
296       }
297       sound_manager->play("sounds/upgrade.wav");
298       break;
299
300     case CONTENT_ICEGROW:
301       if(player.get_status()->bonus == NO_BONUS) {
302         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
303         sector->add_object(riser);
304       } else {
305         SpecialRiser* riser = new SpecialRiser(
306           get_pos(), new Flower(ICE_BONUS));
307         sector->add_object(riser);
308       }
309       sound_manager->play("sounds/upgrade.wav");
310       break;
311
312     case CONTENT_STAR:
313       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
314       break;
315
316     case CONTENT_1UP:
317       sector->add_object(new OneUp(get_pos(), direction));
318       break;
319
320     case CONTENT_CUSTOM:
321       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
322       object = 0;
323       sector->add_object(riser);
324       sound_manager->play("sounds/upgrade.wav");
325       break;
326   }
327
328   start_bounce(&player);
329   sprite->set_action("empty");
330 }
331
332 void
333 Block::break_me()
334 {
335   Sector* sector = Sector::current();
336   sector->add_object(
337     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos(), Vector(-100, -400)));
338   sector->add_object(
339     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(0, 16),
340                     Vector(-150, -300)));
341   sector->add_object(
342     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 0),
343                     Vector(100, -400)));
344   sector->add_object(
345     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 16),
346                     Vector(150, -300)));
347   remove_me();
348 }
349
350 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
351
352 //---------------------------------------------------------------------------
353
354 Brick::Brick(const Vector& pos, int data)
355   : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
356     coin_counter(0)
357 {
358   bbox.set_pos(pos);
359   if(data == 1)
360     coin_counter = 5;
361   else
362     breakable = true;
363 }
364
365 void
366 Brick::hit(Player& player)
367 {
368   if(sprite->get_action() == "empty")
369     return;
370
371   try_break(&player);
372 }
373
374 HitResponse
375 Brick::collision(GameObject& other, const CollisionHit& hit){
376
377   Player* player = dynamic_cast<Player*> (&other);
378   if (player) {
379     if (player->does_buttjump) try_break();
380   }
381
382   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
383   if(badguy) {
384     // hit contains no information for collisions with blocks.
385     // Badguy's bottom has to be below the top of the brick
386     // SHIFT_DELTA is required to slide over one tile gaps.
387     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
388       try_break();
389     }
390   }
391   Portable* portable = dynamic_cast<Portable*> (&other);
392   if(portable) {
393     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
394     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
395       try_break();
396     }
397   }
398   return Block::collision(other, hit);
399 }
400
401 void
402 Brick::try_break(Player* player)
403 {
404   if(sprite->get_action() == "empty")
405     return;
406
407   sound_manager->play("sounds/brick.wav");
408   Sector* sector = Sector::current();
409   Player& player_one = *(sector->player);
410   if(coin_counter > 0) {
411     sector->add_object(new BouncyCoin(get_pos(),true));
412     coin_counter--;
413     player_one.get_status()->add_coins(1);
414     if(coin_counter == 0)
415       sprite->set_action("empty");
416     start_bounce(player);
417   } else if(breakable) {
418     if(player){
419       if(player->is_big()){
420         start_break(player);
421         return;
422       } else {
423         start_bounce(player);
424         return;
425       }
426     }
427     break_me();
428   }
429 }
430
431 //IMPLEMENT_FACTORY(Brick, "brick");
432
433 /* EOF */