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