dd57c25c1cd787d1ef6257158faa09592d73f60f
[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   contents(),
163   object(0)
164 {
165   bbox.set_pos(pos);
166   sprite->set_action("normal");
167   switch(data) {
168     case 1: contents = CONTENT_COIN; break;
169     case 2: contents = CONTENT_FIREGROW; break;
170     case 3: contents = CONTENT_STAR; break;
171     case 4: contents = CONTENT_1UP; break;
172     case 5: contents = CONTENT_ICEGROW; break;
173     default:
174       log_warning << "Invalid box contents" << std::endl;
175       contents = CONTENT_COIN;
176       break;
177   }
178 }
179
180 BonusBlock::BonusBlock(const Reader& lisp) :
181   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
182   contents(),
183   object(0)
184 {
185   Vector pos;
186
187   contents = CONTENT_COIN;
188   lisp::ListIterator iter(&lisp);
189   while(iter.next()) {
190     const std::string& token = iter.item();
191     if(token == "x") {
192       iter.value()->get(pos.x);
193     } else if(token == "y") {
194       iter.value()->get(pos.y);
195     } else if(token == "contents") {
196       std::string contentstring;
197       iter.value()->get(contentstring);
198       if(contentstring == "coin") {
199         contents = CONTENT_COIN;
200       } else if(contentstring == "firegrow") {
201         contents = CONTENT_FIREGROW;
202       } else if(contentstring == "icegrow") {
203         contents = CONTENT_ICEGROW;
204       } else if(contentstring == "star") {
205         contents = CONTENT_STAR;
206       } else if(contentstring == "1up") {
207         contents = CONTENT_1UP;
208       } else if(contentstring == "custom") {
209         contents = CONTENT_CUSTOM;
210       } else {
211         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
212       }
213     } else {
214       if(contents == CONTENT_CUSTOM) {
215         GameObject* game_object = create_object(token, *(iter.lisp()));
216         object = dynamic_cast<MovingObject*> (game_object);
217         if(object == 0)
218           throw std::runtime_error(
219             "Only MovingObjects are allowed inside BonusBlocks");
220       } else {
221         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
222       }
223     }
224   }
225
226   if(contents == CONTENT_CUSTOM && object == 0)
227     throw std::runtime_error("Need to specify content object for custom block");
228
229   bbox.set_pos(pos);
230 }
231
232 BonusBlock::~BonusBlock()
233 {
234   delete object;
235 }
236
237 void
238 BonusBlock::hit(Player& )
239 {
240   try_open();
241 }
242
243 HitResponse
244 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
245
246   Player* player = dynamic_cast<Player*> (&other);
247   if (player) {
248     if (player->does_buttjump) try_open();
249   }
250
251   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
252   if(badguy) {
253     // hit contains no information for collisions with blocks.
254     // Badguy's bottom has to be below the top of the block
255     // SHIFT_DELTA is required to slide over one tile gaps.
256     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
257       try_open();
258     }
259   }
260   Portable* portable = dynamic_cast<Portable*> (&other);
261   if(portable) {
262     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
263     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
264       try_open();
265     }
266   }
267   return Block::collision(other, hit);
268 }
269
270 void
271 BonusBlock::try_open()
272 {
273   if(sprite->get_action() == "empty") {
274     sound_manager->play("sounds/brick.wav");
275     return;
276   }
277
278   Sector* sector = Sector::current();
279   assert(sector);
280   assert(sector->player);
281   Player& player = *(sector->player);
282   Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
283
284   switch(contents) {
285     case CONTENT_COIN:
286       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
287       player.get_status()->add_coins(1);
288       Sector::current()->get_level()->stats.coins++;
289       break;
290
291     case CONTENT_FIREGROW:
292       if(player.get_status()->bonus == NO_BONUS) {
293         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
294         sector->add_object(riser);
295       } else {
296         SpecialRiser* riser = new SpecialRiser(
297           get_pos(), new Flower(FIRE_BONUS));
298         sector->add_object(riser);
299       }
300       sound_manager->play("sounds/upgrade.wav");
301       break;
302
303     case CONTENT_ICEGROW:
304       if(player.get_status()->bonus == NO_BONUS) {
305         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
306         sector->add_object(riser);
307       } else {
308         SpecialRiser* riser = new SpecialRiser(
309           get_pos(), new Flower(ICE_BONUS));
310         sector->add_object(riser);
311       }
312       sound_manager->play("sounds/upgrade.wav");
313       break;
314
315     case CONTENT_STAR:
316       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
317       break;
318
319     case CONTENT_1UP:
320       sector->add_object(new OneUp(get_pos(), direction));
321       break;
322
323     case CONTENT_CUSTOM:
324       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
325       object = 0;
326       sector->add_object(riser);
327       sound_manager->play("sounds/upgrade.wav");
328       break;
329   }
330
331   start_bounce(&player);
332   sprite->set_action("empty");
333 }
334
335 void
336 Block::break_me()
337 {
338   Sector* sector = Sector::current();
339   sector->add_object(
340     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos(), Vector(-100, -400)));
341   sector->add_object(
342     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(0, 16),
343                     Vector(-150, -300)));
344   sector->add_object(
345     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 0),
346                     Vector(100, -400)));
347   sector->add_object(
348     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 16),
349                     Vector(150, -300)));
350   remove_me();
351 }
352
353 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
354
355 //---------------------------------------------------------------------------
356
357 Brick::Brick(const Vector& pos, int data)
358   : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
359     coin_counter(0)
360 {
361   bbox.set_pos(pos);
362   if(data == 1)
363     coin_counter = 5;
364   else
365     breakable = true;
366 }
367
368 void
369 Brick::hit(Player& player)
370 {
371   if(sprite->get_action() == "empty")
372     return;
373
374   try_break(&player);
375 }
376
377 HitResponse
378 Brick::collision(GameObject& other, const CollisionHit& hit){
379
380   Player* player = dynamic_cast<Player*> (&other);
381   if (player) {
382     if (player->does_buttjump) try_break();
383   }
384
385   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
386   if(badguy) {
387     // hit contains no information for collisions with blocks.
388     // Badguy's bottom has to be below the top of the brick
389     // SHIFT_DELTA is required to slide over one tile gaps.
390     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
391       try_break();
392     }
393   }
394   Portable* portable = dynamic_cast<Portable*> (&other);
395   if(portable) {
396     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
397     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
398       try_break();
399     }
400   }
401   return Block::collision(other, hit);
402 }
403
404 void
405 Brick::try_break(Player* player)
406 {
407   if(sprite->get_action() == "empty")
408     return;
409
410   sound_manager->play("sounds/brick.wav");
411   Sector* sector = Sector::current();
412   Player& player_one = *(sector->player);
413   if(coin_counter > 0) {
414     sector->add_object(new BouncyCoin(get_pos(),true));
415     coin_counter--;
416     player_one.get_status()->add_coins(1);
417     if(coin_counter == 0)
418       sprite->set_action("empty");
419     start_bounce(player);
420   } else if(breakable) {
421     if(player){
422       if(player->is_big()){
423         start_break(player);
424         return;
425       } else {
426         start_bounce(player);
427         return;
428       }
429     }
430     break_me();
431   }
432 }
433
434 //IMPLEMENT_FACTORY(Brick, "brick");
435
436 /* EOF */